问题
V1 V2 V3
1 a b c
2 a d c
3 a b g
4 f b c
5 a b c
6 a b c
7 a b c
I want to fill columns in data frame with values from rows above. It should look like this.
V1 V2 V3
1 a b c
2 a d c
3 a d g
4 f d g
5 f d g
6 f d g
7 f d g
回答1:
rollapply
from the package zoo
does this. Here the window is given as the entire set. partial=TRUE
is used so you get intermediate results.
rollapplyr(df, nrow(df), max, partial=TRUE)
## V1 V2 V3
## [1,] "a" "b" "c"
## [2,] "a" "d" "c"
## [3,] "a" "d" "g"
## [4,] "f" "d" "g"
## [5,] "f" "d" "g"
## [6,] "f" "d" "g"
## [7,] "f" "d" "g"
rollapplyr
sets align = 'right'
. Note that this does produce a matrix as a result.
回答2:
We can use base R
methods for this
df1[] <- lapply(df1, function(x) {x1 <- factor(x)
levels(x1)[cummax(as.integer(x1))]})
df1
# V1 V2 V3
#1 a b c
#2 a d c
#3 a d g
#4 f d g
#5 f d g
#6 f d g
#7 f d g
回答3:
I would use a for-loop for this.
Here's a very manual approach that computes the indexes of the breaks in value along the vector (unconditionally taking the first element as a pseudo-break), gets the index NL
from the last where NL
allows us to parameterize the run-length that will be used to fill subsequent run-lengths, and finally assigns over subsequent run-lengths the value from the selected run-length.
NL <- 2L;
for (ci in seq_len(ncol(df))) {
x <- c(1L,which(df[-1L,ci]!=df[-nrow(df),ci])+1L);
i <- x[max(1L,length(x)-NL+1L)];
df[i:nrow(df),ci] <- df[i,ci];
}; ## end for
df;
## V1 V2 V3
## 1 a b c
## 2 a d c
## 3 a d g
## 4 f d g
## 5 f d g
## 6 f d g
## 7 f d g
Data
df <- data.frame(V1=c('a','a','a','f','a','a','a'),V2=c('b','d','b','b','b','b','b'),V3=c('c'
,'c','g','c','c','c','c'),stringsAsFactors=F);
回答4:
Here's another solution using cumany
in dplyr
:
library(dplyr)
df %>%
mutate_all(funs(ifelse(cumany(. == max(.)), max(.), .)))
Result:
V1 V2 V3
1 a b c
2 a d c
3 a d g
4 f d g
5 f d g
6 f d g
7 f d g
来源:https://stackoverflow.com/questions/38064415/r-fill-columns-in-data-frame