如何从左到右完成数据框中的列

时间:2019-06-26 14:15:41

标签: r

我有一个数据框,我想通过使用持续的非NA值并在其后添加(foo)后缀来从左至右完成列。例如,此数据框:

df <- data.frame(
  x = c("one", "one", "three"),
  y = c("two", "four", NA),
  z = c("three", NA, NA)
)

df
#>       x    y     z
#> 1   one  two three
#> 2   one four  <NA>
#> 3 three <NA>  <NA>

会产生:

data.frame(
  x = c("one", "one", "three"),
  y = c("two", "four", "three (foo)"),
  z = c("three", "four (foo)", "three (foo)")
)
#>       x           y           z
#> 1   one         two       three
#> 2   one        four  four (foo)
#> 3 three three (foo) three (foo)

有一种优雅的方法吗?它可以是R,tidyverse或data.table解决方案。 reprex package(v0.3.0)于2019-06-26创建

2 个答案:

答案 0 :(得分:1)

这是一种tidyverse方法,

library(tidyverse)

df %>% 
 mutate(new = row_number()) %>% 
 gather(var, val, - new) %>% 
 group_by(new) %>% 
 mutate(flag = as.integer(is.na(val))) %>% 
 fill(val) %>% 
 mutate(val = replace(val, flag == 1, paste(val[flag == 1], '(foo)')))  %>% 
 select(-flag) %>% 
 spread(var, val)

给出,

# A tibble: 3 x 4
# Groups:   new [3]
    new x     y           z          
  <int> <chr> <chr>       <chr>      
1     1 one   two         three      
2     2 one   four        four (foo) 
3     3 three three (foo) three (foo)

答案 1 :(得分:1)

以下是使用"A String" | Export-Csv软件包和base的方法:

dplyr

奉献

library(dplyr)

df <- data.frame(
  x = c("one", "one", "three"),
  y = c("two", "four", NA),
  z = c("three", NA, NA)
)

nalast = function(x){
  l1 = x
  nas = is.na(l1)
  l1[nas] = paste0(x[tail(which(!nas),n=1)]," (foo)")
  return(l1)
}

df2 = apply(X = df, MARGIN = 2, FUN = nalast)

df2