R:填写多列

wos*_*hui 3 r fill tidyr

我正在使用tidyr包中的fill().fill(df,colname1,colname2,colname3)工作正常,直到我找到一个包含32个变量的数据集.如何在不键入每个名称的情况下填写所有列?

我试过了:

fill(df,colnames(df)),
fill(df,1:32), 
fill(df,colname1:colname32). 
Run Code Online (Sandbox Code Playgroud)

并产生以下错误:

Error: All select() inputs must resolve to integer column positions.
The following do not:
*  colnames(df1)

Error: tinyformat: Not enough conversion specifiers in format string

Error: tinyformat: Not enough conversion specifiers in format string
Run Code Online (Sandbox Code Playgroud)

akr*_*run 8

我们可以fill_在选择变量时使用names.

library(tidyr)# using tidyr_0.4.1.9000
res <- fill_(df, names(df))
head(res)
#   col1 col2 col3
#1    1   NA    b
#2    1    3    b
#3    2    4    a
#4    2    4    a
#5    2    1    a
#6    3    4    a
Run Code Online (Sandbox Code Playgroud)

其他选择是

fill(df, everything())
Run Code Online (Sandbox Code Playgroud)

然而,如果我们使用fillnames(df)),它会给出同样的错误作为OP显示

fill(df, names(df)[1])
#Error: All select() inputs must resolve to integer column positions.
#The following do not:
#*  names(df)[1]
Run Code Online (Sandbox Code Playgroud)

数据

set.seed(24)
 df <- data.frame(col1= sample(c(NA, 1:3), 20, replace=TRUE), 
                  col2 = sample(c(NA, 1:5), 20, replace=TRUE),
                  col3 = sample(c(NA, letters[1:5]), 20, replace=TRUE),
                  stringsAsFactors=FALSE)
Run Code Online (Sandbox Code Playgroud)