R:向下填充多个列

15

我正在使用tidyr软件包中的fill()函数。fill(df, colname1, colname2, colname3)操作正常,但当我遇到一个有32个变量的数据集时出现问题。请问如何在不输入每个变量名称的情况下,填充所有列?

我尝试过:

fill(df,colnames(df)),
fill(df,1:32), 
fill(df,colname1:colname32). 

并且产生了以下错误:

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

1
鉴于您说填充(fill)(df,1:32)不起作用,并且下面提供的常规评估选项也不起作用,可能存在数据框(该对象的类)的某些问题?您能否提供“class(df)”和“str(df)”的输出? - Gopala
3个回答

24

当我们使用names选择变量时,可以使用fill_

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

另一个选择是

fill(df, everything())

但是,如果我们使用names(df)fill一起使用,它将产生和OP显示的相同的错误。

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

数据

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)

1
@woshishui 这个例子是有效的。由于您没有提供可重现的示例,因此很难发表评论。 - akrun
1
@woshishui 我也尝试了混合类别的数据集,对我来说是有效的。 - akrun
@Gopala 是的,可能是版本差异。由于 OP 没有展示任何可重现的例子,所以很难知道。 - akrun
1
在尝试重现数据时,akrun和Gopala发现列名中有一个NA。当使用read.xlsx导入数据时,“'”符号会被传递到数据框中,导致填充(fill())出现问题。解决问题后,所有三种方法fill_(df, names(df))、fill(df,1:32)和fill(df, colname1:colname32)都能正常工作。我应该删除这个问题吗?它可能会误导人吗?感谢您的帮助,如果您没有指出我的错误,我就不知道哪里错了。 - woshishui
@woshishui 你可以保留它,因为我还包括了 fill_ 选项,它可以与 names 一起使用。 - akrun
显示剩余6条评论

3

在@akrun的评论和数据基础上,这里介绍使用tidyr的另外两种方法:

数据

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)

两种选择

#Specify column names
fill(df, c("col1", "col2"), .direction = "down")

#Specify range of columns
fill(df, c(col1:col3), .direction = "down")

1

另一个选择是使用zoo包,如果需要,它也可以向后填充。 在上面创建的示例中-

zoo::na.locf(df)

   col1 col2 col3
1     2    4    e
2     2    4    e
3     3    4    a
4     2    4    b
5     1    3    d
6     2    4    d
7     2    1    b
8     1    1    e
9     3    3    e
10    1    2    e
11    1    4    e
12    1    1    e
13    3    1    a
14    3    4    c
15    3    3    b
16    2    3    e
17    3    1    e
18    3    2    b
19    3    5    c
20    3    5    e

其中df

   col1 col2 col3
1     2    4    e
2     2   NA    e
3     3    4    a
4     2    4    b
5     1    3    d
6     2    4 <NA>
7    NA    1    b
8     1   NA    e
9     3    3 <NA>
10    1    2    e
11    1    4    e
12   NA    1 <NA>
13    3   NA    a
14   NA    4    c
15    3    3    b
16    2    3    e
17    3    1    e
18   NA    2    b
19   NA    5    c
20    3    5    e

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接