Why does tapply take the subset as NA and not exclude them totally

萝らか妹 提交于 2019-11-29 17:36:54

Without a sample of your data, I'll just wager a guess:

your column plant is a factor. And while you have dropped the rows that have that value, the "level" FS still exists. Use levels(data$plant) to see. You can then use droplevels to get rid of it.

dat <- data.frame(x=1:15, y=factor(letters[1:3]))

> levels(dat$y)
[1] "a" "b" "c"

dat <- dat[dat$y != 'a',]
> levels(dat$y)
[1] "a" "b" "c"
> 

> tapply(dat$x, dat$y, sum)
 a  b  c 
NA 40 45 
> 

> droplevels(dat$y)
 [1] b c b c b c b c b c
Levels: b c
> dat$y <- droplevels(dat$y)

> tapply(dat$x, dat$y, sum)
 b  c 
40 45 
> 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!