如何用相应列的值替换多列中的 NA

JPe*_*ete 6 r na dplyr

我正在尝试清理一个数据框,我想用另一列中的相应值替换一列中的 NA。我还想一次为多个列执行此操作。

示例数据框。

set.seed(123) 

dates <- seq(as.Date("2016-01-01"), by = "day", length = 10)  
names <- rep(c("John Doe", "Jane Smith"), each = 5)  
var1_group <- runif(10)  
var2_group <- runif(10)  
var1_person <- runif(10)  
var2_person <- runif(10)  

myDF <- data.frame(names, var1_group, var2_group, var1_person, var2_person)  
myDF <- cbind(dates, myDF)  
Run Code Online (Sandbox Code Playgroud)

使用 dplyr 进行一些操作后...

myDF <- myDF %>% mutate_each(funs(lag), contains("group"))  
myDF <- myDF %>% group_by(names) %>% mutate_each(funs(lag), contains("person"))  
Run Code Online (Sandbox Code Playgroud)

我得到了一堆 NA...

        dates      names var1_group var2_group var1_person var2_person  
1  2016-01-01   John Doe         NA         NA          NA          NA  
2  2016-01-02   John Doe  0.2875775 0.95683335   0.8895393   0.9630242  
3  2016-01-03   John Doe  0.7883051 0.45333416   0.6928034   0.9022990  
4  2016-01-04   John Doe  0.4089769 0.67757064   0.6405068   0.6907053  
5  2016-01-05   John Doe  0.8830174 0.57263340   0.9942698   0.7954674  
6  2016-01-06 Jane Smith  0.9404673 0.10292468          NA          NA  
7  2016-01-07 Jane Smith  0.0455565 0.89982497   0.7085305   0.4777960  
8  2016-01-08 Jane Smith  0.5281055 0.24608773   0.5440660   0.7584595  
9  2016-01-09 Jane Smith  0.8924190 0.04205953   0.5941420   0.2164079  
10 2016-01-10 Jane Smith  0.5514350 0.32792072   0.2891597   0.3181810  
Run Code Online (Sandbox Code Playgroud)

我现在想做的是将 *_person 列中的 NA 替换为 *_group 列中的相应值。(见第 6 行)

        dates      names var1_group var2_group var1_person var2_person  
1  2016-01-01   John Doe         NA         NA          NA          NA  
2  2016-01-02   John Doe  0.2875775 0.95683335   0.8895393   0.9630242  
3  2016-01-03   John Doe  0.7883051 0.45333416   0.6928034   0.9022990  
4  2016-01-04   John Doe  0.4089769 0.67757064   0.6405068   0.6907053  
5  2016-01-05   John Doe  0.8830174 0.57263340   0.9942698   0.7954674  
6  2016-01-06 Jane Smith  0.9404673 0.10292468   0.9404673   0.1029246     
7  2016-01-07 Jane Smith  0.0455565 0.89982497   0.7085305   0.4777960  
8  2016-01-08 Jane Smith  0.5281055 0.24608773   0.5440660   0.7584595  
9  2016-01-09 Jane Smith  0.8924190 0.04205953   0.5941420   0.2164079  
10 2016-01-10 Jane Smith  0.5514350 0.32792072   0.2891597   0.3181810 
Run Code Online (Sandbox Code Playgroud)

这适用于一列...

myDF$var1_person <- ifelse(is.na(myDF$var1_person), myDF$var1_group, myDF$var1_person)  
Run Code Online (Sandbox Code Playgroud)

但我想一次对所有列执行此操作。在我的实际数据框中,每组大约有 20 列。我已经尝试了很多其他的东西,但我不想用我的废话把这篇文章弄得一团糟。

*如果您可以根据列前缀获得匹配 n 个变量的代码,则加分。

var1_group > var1_person  
var2_group > var2_person
...
varn_group > varn_person   
Run Code Online (Sandbox Code Playgroud)

akr*_*run 3

这是一个选项,可以使用setdata.table进行替换

library(data.table)
#convert the data.frame to data.table
setDT(myDF)
#get the column name of 'group' and 'person' columns
nm1 <-  grep("group", names(myDF), value = TRUE)
nm2 <-  grep("person", names(myDF), value = TRUE)
#loop through the sequence of 'nm1'
for(j in seq_along(nm1)){
#set the elements in the row that are NA for each 'period' column
#with the corresponding row from 'group' column specified in the "value"
    set(myDF, i = which(is.na(myDF[[nm2[j]]])), j = nm2[j],
                    value = myDF[[nm1[j]]][is.na(myDF[[nm2[j]]])])
}

 myDF
 #        dates      names var1_group var2_group var1_person var2_person
 #1: 2016-01-01   John Doe         NA         NA          NA          NA
 #2: 2016-01-02   John Doe  0.2875775 0.95683335   0.8895393   0.9630242
 #3: 2016-01-03   John Doe  0.7883051 0.45333416   0.6928034   0.9022990
 #4: 2016-01-04   John Doe  0.4089769 0.67757064   0.6405068   0.6907053
 #5: 2016-01-05   John Doe  0.8830174 0.57263340   0.9942698   0.7954674
 #6: 2016-01-06 Jane Smith  0.9404673 0.10292468   0.9404673   0.1029247
 #7: 2016-01-07 Jane Smith  0.0455565 0.89982497   0.7085305   0.4777960
 #8: 2016-01-08 Jane Smith  0.5281055 0.24608773   0.5440660   0.7584595
 #9: 2016-01-09 Jane Smith  0.8924190 0.04205953   0.5941420   0.2164079
 #10:2016-01-10 Jane Smith  0.5514350 0.32792072   0.2891597   0.3181810
Run Code Online (Sandbox Code Playgroud)

  • @StevenBeaupré我正在遵循OP的例子。 (2认同)