2017-09-06 3 views
1
I 다른 데이터 프레임 (date.index )의 기간에 소정의 데이터 프레임 (fruits.df)의 인덱스 값을 변경해야

일자로 하나의 데이터 프레임의 인덱스 값을 대체.다른 데이터 프레임

예 데이터 :

# fruits.df  
x <- 1:5 
y <- 1:12 
z <- 1:16 
w <- 1:7 
fruits.list <- list(Apples = x, Bananas = y, Grapes = z, Kiwis = w) 

library(qpcR) 
fruits.df <- do.call(qpcR:::cbind.na, lapply(
    fruits.list, data.frame)) 

names(fruits.df) <- names(fruits.list) 

이 다음 날짜 프레임이 생성 된 날짜 인덱스 데이터 프레임

enter image description here

예 데이터 : 그래서 필요한

date.index <- data.frame(Days = seq(as.Date("2017-07-01"), 
    as.Date("2017-07-20"), by = 1), index = as.integer(1:20)) 

을 다음과 같습니다 :

enter image description here

나는 TI는 dplyr의 필터 기능을 사용하여 시도했지만 내가 명시 적으로 열을 선택하는 경우에만 작동합니다. , 당신은 당신의 fruits.df의 각 열에 match을 사용할 수 있습니다

filtered_found_Index <- filter(date.index, index %in% 
    fruits.df$**Bananas**) 

답변

3

을 :

filtered_found_Index <- filter(date.index, index %in% 
    fruits.df) 

작품을,하지만 전체 DF과 동시에 그것을 수행해야합니다

가 작동하지 않습니다 즉

fruits.df[] <- lapply(fruits.df, function(i) date.index$Days[match(i, date.index$index)]) 

이 경우,

 Apples Bananas  Grapes  Kiwis 
1 2017-07-01 2017-07-01 2017-07-01 2017-07-01 
2 2017-07-02 2017-07-02 2017-07-02 2017-07-02 
3 2017-07-03 2017-07-03 2017-07-03 2017-07-03 
4 2017-07-04 2017-07-04 2017-07-04 2017-07-04 
5 2017-07-05 2017-07-05 2017-07-05 2017-07-05 
6  <NA> 2017-07-06 2017-07-06 2017-07-06 
7  <NA> 2017-07-07 2017-07-07 2017-07-07 
8  <NA> 2017-07-08 2017-07-08  <NA> 
9  <NA> 2017-07-09 2017-07-09  <NA> 
10  <NA> 2017-07-10 2017-07-10  <NA> 
11  <NA> 2017-07-11 2017-07-11  <NA> 
12  <NA> 2017-07-12 2017-07-12  <NA> 
13  <NA>  <NA> 2017-07-13  <NA> 
14  <NA>  <NA> 2017-07-14  <NA> 
15  <NA>  <NA> 2017-07-15  <NA> 
16  <NA>  <NA> 2017-07-16  <NA> 
관련 문제