创建简单的功能来清理数据(在没有NA的情况下有条件地删除行)

时间:2017-03-07 18:02:43

标签: r

嗨我有从每个站点有4个样方的样方收集的动物丰度数据。

在现场,错过了一些样方。

St/ Q /Anim1 abundance /Anim 2 abundance/....etc
1 /1 /
1 /2 /
1 /3 /
1 /4 /
2 /1 /
2 /2 /
2 /4 /
3 /1 /
3 /2 /
3 /3 /
3 /4 /

第2站缺少样方3.我想从进一步分析中删除与第2站相关的所有行(包括动物丰度数据)。我想在函数中执行此操作,因为我需要清理多个大型csv文件。

我尝试了子集和for循环,但两个都在努力

感谢您的时间

******更新我正在使用此qc_Large29< - Large29 [Large29 [,5]> = 4,]

给了我每个站的所有第4个样方。有没有办法为它添加一个length(),以便新数据帧只是与具有4个样方的工作站相关联的数据?

**********更新

 dput(Large29[1:30,1:5])
structure(list(FID = 652:681, areaContro = c(29L, 29L, 29L, 29L, 
29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 
29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L
), areaShortN = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L), .Label = "CAIIN", class = "factor"), station = c(1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 
5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L), quadrat = c(1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L)), .Names = c("FID", 
"areaContro", "areaShortN", "station", "quadrat"), row.names = c(NA, 
30L), class = "data.frame")
> 

1 个答案:

答案 0 :(得分:0)

这会选择除“2”站之外的所有内容:

Large29[Large29$station!=2,]

对于你的第二个问题(编辑),我建议使用dplyr,你可以按站分组:

library(dplyr)
Large29 %>% group_by(station) %>% filter(n()>=4) %>% as.data.frame()