'R: fill down multiple columns
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
Solution 1:[1]
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
Solution 2:[2]
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")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AnilGoyal |
| Solution 2 | EDennnis |
