我有一个数据框:
df<- data.frame(region= c("1", "1", "1","1","1","1","1","1","2","2"),
plot=c("1", "1", "1","2","2","2", "3","3","3","3"),
interact=c("A_B", "C_D","C_D", "E_F","C_D","C_D", "D_E",
"D_E","C_B","A_B"))
我想计算每个情节子集的interact
的所有唯一级别。最终的数据框架如下:
result<-
Plot freq
1 2
2 2
3 3
我想使用dplyr并且已经走到了这一步:
df2 <-df %>% group_by(plot) %>%mutate(freq=length(unique((interact))))
但是根据上面的代码,我还没有想出一种方法,即每个绘图只表示一个值(即,删除每个唯一绘图的freq
中的重复值)。
答案 0 :(得分:2)
试试这个。
df%>%group_by(plot)%>%summarise(n=length(unique(interact)))
plot n
1 1 2
2 2 2
3 3 3
或以您自己的方式。
df2 <-df %>% group_by(plot) %>%mutate(freq=length(unique((interact))))
df2=df2[!duplicated(df2$plot),]
region plot interact freq
<fctr> <fctr> <fctr> <int>
1 1 1 A_B 2
2 1 2 E_F 2
3 1 3 D_E 3