Reputation: 2084
I'm using fill() from the tidyr package. fill(df, colname1, colname2, colname3)
works fine, until I found a dataset with 32 variables. How should I fill down all columns without typing each name?
I've tried:
fill(df,colnames(df)),
fill(df,1:32),
fill(df,colname1:colname32).
and produced the following errors:
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
Upvotes: 18
Views: 16851
Reputation: 331
Building off @akrun's comment and data, here are two other ways using the tidyr:
Data
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)
Two Options
#Specify column names
fill(df, c("col1", "col2"), .direction = "down")
#Specify range of columns
fill(df, c(col1:col3), .direction = "down")
Upvotes: 6
Reputation: 26238
Another alternative with package zoo
which can also fill backwards if desired.
On the sample created above-
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
where df
is
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
Upvotes: 1
Reputation: 887971
We can use fill_
when we select variables with 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
Other option would be
fill(df, everything())
However, if we use fill
with names(df))
, it will give the same error as the OP showed
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)
Upvotes: 25