R通过查找条件替换更多列
假设我们在dataframedf1中有很多数据列(名称mycols,还有一些在本例中不应处理的未命名数据列),还有一列Sub,它也是另一个dataframedf2的索引,其中列repl和Sub(在第二个数据框中是subu唯一)和许多其他不重要的列(它们在其中的唯一作用是,我们不能假设只有2列)R通过查找条件替换更多列,r,dataframe,lookup,na,R,Dataframe,Lookup,Na,假设我们在dataframedf1中有很多数据列(名称mycols,还有一些在本例中不应处理的未命名数据列),还有一列Sub,它也是另一个dataframedf2的索引,其中列repl和Sub(在第二个数据框中是subu唯一)和许多其他不重要的列(它们在其中的唯一作用是,我们不能假设只有2列) 我希望以这样的方式替换列的子集(df1[,mycols]),即如果存在NA(df1[,mycols][is.NA(df1[,mycols])],则可以使用以下方法解决此问题: mycols使用您的代码,我
我希望以这样的方式替换列的子集(df1[,mycols]),即如果存在NA(df1[,mycols][is.NA(df1[,mycols])],则可以使用以下方法解决此问题:
mycols使用您的代码,我们需要复制“repl”列,使两个子集数据集相等,然后像您那样分配值
val <- df2$repl[match(df1$subj, df2$subj)][row(df1[mycols])][is.na(df1[mycols])]
df1[mycols][is.na(df1[mycols])] <- val
df1
# subj a b c
#1 1 5 5 1
#2 1 2 3 5
#3 2 0 6 2
#4 3 8 8 8
或使用dplyr
library(dplyr)
left_join(df1, df2, by='subj') %>%
mutate_each_(funs(ifelse(is.na(.),repl,.)), mycols) %>%
select(a:c)
# a b c
#1 5 5 1
#2 2 3 5
#3 0 6 2
#4 8 8 8
数据
df1使用base R执行此操作的一种方法:
mycols = c("a","b")
df1 <- read.table(text="subj a b c
1 NA NA 1
1 2 3 5
2 0 NA 2
3 8 8 8", header = TRUE)
df2 <- read.table(text="subj repl notinterested
1 5 1000
2 6 0
3 40 10", header = TRUE)
df1[mycols] <- lapply(df1[mycols], function(x) {
x[is.na(x)] <- df2$repl[match(df1$subj[is.na(x)], df2$subj)]; x})
mycols=c(“a”、“b”)
df1添加了数据处理的示例,供评论用的thx!@akrun Iam是R中的一个noob,所以很遗憾我无法理解它是如何工作的,你能给我一个答案吗?我根据你的代码添加了一个解决方案并更正了它。谢谢!顺便说一句,所有的回答都很好,伙计们,所以我接受了lapply:)编辑-akrun won-最长最酷的答案:)
function(x){ *x[is.na(*x)] }
function(& df1[,mycols])
mycols <- c('a','b');
df1 <- data.frame(subj=c(1,1,2,3), a=c(NA,2,0,8), b=c(NA,3,NA,8), c=c(1,5,2,8) );
df2 <- data.frame(subj=c(1,2,3), repl=c(5,6,40), notinterested=c(1000,0,10) );
df1[mycols] <- ifelse(is.na(df1[mycols]),matrix(df2[match(df1$subj,df2$subj),'repl'],nrow(df1),length(mycols)),as.matrix(df1[mycols]));
df1;
## subj a b c
## 1 1 5 5 1
## 2 1 2 3 5
## 3 2 0 6 2
## 4 3 8 8 8
val <- df2$repl[match(df1$subj, df2$subj)][row(df1[mycols])][is.na(df1[mycols])]
df1[mycols][is.na(df1[mycols])] <- val
df1
# subj a b c
#1 1 5 5 1
#2 1 2 3 5
#3 2 0 6 2
#4 3 8 8 8
library(data.table)#v1.9.5+
DT <- setDT(df1, key='subj')[df2[c('subj', 'repl')]]
for(j in mycols){
i1 <- which(is.na(DT[[j]]))
set(DT, i=i1, j=j, value= DT[['repl']][i1])
}
DT[,repl:= NULL]
# subj a b c
#1: 1 5 5 1
#2: 1 2 3 5
#3: 2 0 6 2
#4: 3 8 8 8
library(dplyr)
left_join(df1, df2, by='subj') %>%
mutate_each_(funs(ifelse(is.na(.),repl,.)), mycols) %>%
select(a:c)
# a b c
#1 5 5 1
#2 2 3 5
#3 0 6 2
#4 8 8 8
df1 <- structure(list(subj = c(1L, 1L, 2L, 3L), a = c(NA, 2L, 0L, 8L
), b = c(NA, 3L, NA, 8L), c = c(1L, 5L, 2L, 8L)), .Names = c("subj",
"a", "b", "c"), class = "data.frame", row.names = c(NA, -4L))
df2 <- structure(list(subj = 1:3, repl = c(5L, 6L, 40L),
notinterested = c(1000L,
0L, 10L)), .Names = c("subj", "repl", "notinterested"),
class = "data.frame", row.names = c(NA, -3L))
mycols = c("a","b")
df1 <- read.table(text="subj a b c
1 NA NA 1
1 2 3 5
2 0 NA 2
3 8 8 8", header = TRUE)
df2 <- read.table(text="subj repl notinterested
1 5 1000
2 6 0
3 40 10", header = TRUE)
df1[mycols] <- lapply(df1[mycols], function(x) {
x[is.na(x)] <- df2$repl[match(df1$subj[is.na(x)], df2$subj)]; x})