【问题标题】:!is.na creates NAs in other columns!is.na 在其他列中创建 NA
【发布时间】:2013-02-19 21:19:40
【问题描述】:

在合并多个数据集的过程中,我正在尝试删除数据框中所有缺少某个特定变量值的行(我想暂时将 NA 保留在其他一些列中)。我使用了以下行:

data.frame <- data.frame[!is.na(data.frame$year),]

这成功地删除了所有带有 year 的 NA 的行(并且没有其他行),但是以前有数据的其他列现在完全是 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

【问题讨论】:

  • 请给我们一个可重复的数据。请不要将您的data.frame 命名为data.frame。因为已经有一个名为 data.frame 的函数。
  • @Arun 但是他可以将他的data.frame 命名为function,还是已经有一个data.frame 称为function? :)
  • :) 我头晕目眩。大声笑。
  • 对不起,我认为这可能是可以从概念上回答的问题。我使用应该重现问题的代码和数据进行了编辑。
  • @davy,你在merge 步骤之后检查了你的data 吗?

标签: r dataframe na


【解决方案1】:

我认为真正的问题在于您的merge

合并并拥有data 中的数据后,如果这样做:

# > table(data$procedur, useNA="always")

#   1      2      3      4      5      6   <NA> 
# 122    112    356     59     39     19 192258 

您会看到data$procedur 有这么多 (122+112...+19) 值。但是,所有这些值都对应于data$year = NA

> all(is.na(data$year[!is.na(data$procedur)]))
# [1] TRUE # every value of procedur occurs where year = NA

因此,基本上,procedur 的所有值也会被删除,因为您删除了在 year 中检查 NA 的那些行。

要解决这个问题,我认为你应该使用merge作为:

merge(tc, frame, all=T) # it'll automatically calculate common columns
# also this will not result in duplicated year column.

检查此合并是否为您提供所需的结果。

【讨论】:

    【解决方案2】:

    试试complete.cases:

    data.frame.clean <- data.frame[complete.cases(data.frame$year),]
    

    ...不过,如上所述,您可能想选择一个更具描述性的名称。

    【讨论】:

    • is.na的用法是对的。所以,我怀疑这会有所不同。
    • 感谢您的建议。但是是的,结果是完全一样的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 2023-03-13
    • 2018-02-18
    • 1970-01-01
    • 2016-11-17
    • 2022-12-05
    相关资源
    最近更新 更多