我有一天的旅行信息和一个勾选框,如果他们没有做任何事情,他们应该打勾。
所以我需要清理这个人提供信息的任何日子,但也勾选了“什么也没做”的框。
以下是两天(周一和周二)的一些示例数据:
test <- structure(list(Mon_a = c(NA, 5L, NA, NA, 3L), Mon_b = c(NA, 5L,
NA, NA, 3L), Mon_c = c(NA, 2L, NA, NA, 1L), Mon_d = c(NA, 2L,
NA, NA, 1L), Mon_Nada_e = c(1L, 1L, 1L, 1L, NA), Tue_a = c(NA,
4L, 5L, NA, 3L), Tue_b = c(NA, 3L, 5L, NA, 3L), Tue_c = c(NA,
2L, 2L, NA, 1L), Tue_d = c(NA, 1L, 2L, NA, 1L), Tue_Nada_e = c(1L,
NA, 1L, 1L, NA)), .Names = c("Mon_a", "Mon_b", "Mon_c", "Mon_d",
"Mon_Nada_e", "Tue_a", "Tue_b", "Tue_c", "Tue_d", "Tue_Nada_e"
), class = "data.frame", row.names = c(NA, -5L))
所以,我想写一个我可以适用于每一天的功能。我提出了以下代码,几乎做了我想做的事,虽然它可能不是很好或整齐:
day <- "^Mon" # (this would be passed as an argument in the function)
daynadacol <- colnames(test)[grepl(paste(day,"_Nada_e", sep = ""), names(test))] # gives the name of the column for that day which is 'did not travel'
daycols <- colnames(test)[(grepl(day, names(test)))] # can probably do these two steps with a -grep?
daynotnada <- daycols[!(daycols%in%daynadacol)]
test[,daynotnada][test[,daynadacol]==1 & !is.na(test[,daynotnada])] <- NA
但是,我也希望“无所事事”专栏成为NA,因为我不相信当天那个人所放下的任何东西 - 我不想把它算作什么都没做,我想算数吧作为缺失的数据。 所以我想,嘿,我可以告诉它替换像这样的所有日期列:
test[,daycols][test[,daynadacol]==1 & !is.na(test[,daynotnada])] <- NA
但是我得到了一个错误:Error in
[&lt; - .data.frame (
tmp , test[, daynadacol] == 1 & !is.na(test[, : unsupported matrix index in replacement
我做错了是否真的很明显?有没有办法解决这个问题呢?可能这只是一个非常丑陋的代码:(而且还有更好的方法来完成整个事情..
答案 0 :(得分:1)
这是解决问题的更简单方法:
day <- "^Mon"
daycols <- grep(day, colnames(test), value = TRUE)
daynadacol <- grep("_Nada_e", daycols, value = TRUE)
daynotnada <- setdiff(daycols, daynadacol)
idx1 <- !is.na(test[[daynadacol]])
idx2 <- !rowSums(is.na(test[daynotnada]))
test[idx1, daynotnada] <- NA
test[idx2, daynadacol] <- NA
# Mon_a Mon_b Mon_c Mon_d Mon_Nada_e Tue_a Tue_b Tue_c Tue_d Tue_Nada_e
# 1 NA NA NA NA 1 NA NA NA NA 1
# 2 NA NA NA NA NA 4 3 2 1 NA
# 3 NA NA NA NA 1 5 5 2 2 1
# 4 NA NA NA NA 1 NA NA NA NA 1
# 5 3 3 1 1 NA 3 3 1 1 NA