在合并多个数据集的过程中,我正在尝试删除一个特定变量缺少值的数据帧的所有行(我想暂时将其保留在其他一些列中) )。我使用了以下行:
data.frame <- data.frame[!is.na(data.frame$year),]
这成功删除了year
的所有行(并且没有其他行),但之前有数据的其他列现在完全是NA。换句话说,非缺失值正在转换为NA。关于这里发生了什么的任何想法?我尝试过这些替代方案并得到了同样的结果:
data.frame <- subset(data.frame, !is.na(year))
data.frame$x <- ifelse(is.na(data.frame$year) == T, 1, 0);
data.frame <- subset(data.frame, x == 0)
我是否错误地使用is.na
?在这种情况下,is.na
还有其他选择吗?任何帮助将不胜感激!
修改 以下是应该重现此问题的代码:
#data
tc <- read.csv("http://dl.dropbox.com/u/4115584/tc2008.csv")
frame <- read.csv("http://dl.dropbox.com/u/4115584/frame.csv")
#standardize NA codes
tc[tc == "."] <- NA
tc[tc == -9] <- NA
#standardize spatial units
colnames(frame)[1] <- "loser"
colnames(frame)[2] <- "gainer"
frame$dyad <- paste(frame$loser,frame$gainer,sep="")
tc$dyad <- paste(tc$loser,tc$gainer,sep="")
drops <- c("loser","gainer")
tc <- tc[,!names(tc) %in% drops]
frame <- frame[,!names(frame) %in% drops]
rm(drops)
#merge tc into frame
data <- merge(tc, frame, by.x = "year", by.y = "dyad", all.x=T, all.y=T) #year column is duplicated in this process. I haven't had this problem with nearly identical code using other data.
rm(tc,frame)
#the first column in the new data frame is the duplicate year, which does not actually contain years. I'll rename it.
colnames(data)[1] <- "double"
summary(data$year) #shows 833 NA's
summary(data$procedur) #note that at this point there are non-NA values
#later, I want to create 20 year windows following the events in the tc data. For simplicity, I want to remove cases with NA in the year column.
new.data <- data[!is.na(data$year),]
#now let's see what the above operation did
summary(new.data$year) #missing years were successfully removed
summary(new.data$procedur) #this variable is now entirely NA's
答案 0 :(得分:2)
我认为实际问题出在您的merge
上。
合并并拥有data
中的数据后,如果您这样做:
# > table(data$procedur, useNA="always")
# 1 2 3 4 5 6 <NA>
# 122 112 356 59 39 19 192258
您看到122+112...+19
有很多(data$procedur
)个值。但是,所有这些值都与data$year = NA
相对应。
> all(is.na(data$year[!is.na(data$procedur)]))
# [1] TRUE # every value of procedur occurs where year = NA
因此,基本上,procedur
的所有值也会被移除,因为您删除了那些检查NA
中year
的行。
要解决此问题,我认为您应该使用merge
作为:
merge(tc, frame, all=T) # it'll automatically calculate common columns
# also this will not result in duplicated year column.
检查此合并是否为您提供了所需的结果。
答案 1 :(得分:0)
尝试complete.cases
:
data.frame.clean <- data.frame[complete.cases(data.frame$year),]
...但是,如上所述,您可能想要选择一个更具描述性的名称。