lam*_*nes 3 oop r tidyverse tibble
所以,我检查了多个帖子,但没有发现任何内容。根据this,我的代码应该可以工作,但事实并非如此。
目标:我想基本上打印出主题的数量——在本例中也是该小标题中的行数。
代码:
data<-read.csv("advanced_r_programming/data/MIE.csv")
make_LD<-function(x){
LongitudinalData<-x%>%
group_by(id)%>%
nest()
structure(list(LongitudinalData), class = "LongitudinalData")
}
print.LongitudinalData<-function(x){
paste("Longitudinal dataset with", x[["id"]], "subjects")
}
x<-make_LD(data)
print(x)
Run Code Online (Sandbox Code Playgroud)
这是我正在处理的数据集的头部:
> head(x)
[[1]]
# A tibble: 10 x 2
id data
<int> <list>
1 14 <tibble [11,945 x 4]>
2 20 <tibble [11,497 x 4]>
3 41 <tibble [11,636 x 4]>
4 44 <tibble [13,104 x 4]>
5 46 <tibble [13,812 x 4]>
6 54 <tibble [10,944 x 4]>
7 64 <tibble [11,367 x 4]>
8 74 <tibble [11,517 x 4]>
9 104 <tibble [11,232 x 4]>
10 106 <tibble [13,823 x 4]>
Run Code Online (Sandbox Code Playgroud)
输出:
[1] "Longitudinal dataset with subjects"
Run Code Online (Sandbox Code Playgroud)
我已经尝试了上述 stackoverflow 帖子中的所有可能的组合,但似乎都不起作用。
这里有两个选项:
library(tidyverse)
# Create a nested data frame
dat = mtcars %>%
group_by(cyl) %>%
nest %>% as.tibble
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)cyl data 1 6 <tibble [7 x 10]> 2 4 <tibble [11 x 10]> 3 8 <tibble [14 x 10]>
dat %>%
mutate(nrow=map_dbl(data, nrow))
dat %>%
group_by(cyl) %>%
mutate(nrow = nrow(data.frame(data)))
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)cyl data nrow 1 6 <tibble [7 x 10]> 7 2 4 <tibble [11 x 10]> 11 3 8 <tibble [14 x 10]> 14