将行转换为列并按日期连接它们

时间:2017-06-15 10:51:08

标签: r join dataframe

我正在尝试将每日天气信息添加到R上的数据框,因此df1 + df2 = df3是客观的。我认为在df1上按日期对df2进行分段,将df2和rbind.fill折叠为df1将会是这样,但我遇到了麻烦。

date2 <- c('2014-06-03','2017-05-20')
date  <- c('2014-06-01','2017-05-15')
df1 <- data.frame(date,date2)
date

#> df1
#        date      date2
#1 2014-06-01 2014-06-03
#2 2017-05-15 2017-05-20

date3 <- c('2014-06-01','2014-06-02','2014-06-03','2017-05-15','2017-05-16','2017-05-17','2017-05-18','2017-05-19','2017-05-20')
rain  <-  c(3,            4,               3,          5,           5,            6,         7,           6         ,6)
sun <- c (  10,10,10, 15,15,15,16,15,15)
df2 <- data.frame(date3,rain,sun)

#> df2
#       date3 rain sun
#1 2014-06-01    3  10
#2 2014-06-02    4  10
#3 2014-06-03    3  10
#4 2017-05-15    5  15
#5 2017-05-16    5  15
#6 2017-05-17    6  15
#7 2017-05-18    7  16
#8 2017-05-19    6  15
#9 2017-05-20    6  15

rain_day1 <- c(3,5)
rain_day2 <- c(4,5)
rain_day3 <- c(3,6)
rain_day4 <- c(NA,7)
rain_day5 <- c(NA,6)
rain_day6 <- c(NA,6)
sun_day1 <- c(10,15)
sun_day2 <- c(10,15)
sun_day3 <- c(10,15)
sun_day4 <- c(NA,15)
sun_day5 <- c(NA,16)
sun_day6 <- c(NA,15)
date5 <- c('2014-06-03','2017-05-20')
date4  <- c('2014-06-01','2017-05-15')
df3 <- data.frame(date4,date5,rain_day1,sun_day1,rain_day2,sun_day2,rain_day3,sun_day3,rain_day4,sun_day4,rain_day5,sun_day5,rain_day6,sun_day6)

#> df3
#      date4      date5 rain_day1 sun_day1 rain_day2 sun_day2 rain_day3 sun_day3 rain_day4 sun_day4
#1 2014-06-01 2014-06-03         3       10         4       10         3       10        NA       NA
#2 2017-05-15 2017-05-20         5       15         5       15         6       15         7       15
#  rain_day5 sun_day5 rain_day6 sun_day6
#1        NA       NA        NA       NA
#2         6       16         6       15

任何帮助将不胜感激。提前致谢

2 个答案:

答案 0 :(得分:0)

试试这个。我欢迎任何改进我的管道散文,我正在努力。

library(magrittr)
df1$date <- as.Date(df1$date)
df1$date2 <- as.Date(df1$date2)
df2$date3 <- as.Date(df2$date3)
df2 %<>% setNames(c("date3","rain_day","sun_day"))
row_list <- df1 %>% apply(1,function(x){ df2 %>%
                                   subset(date3 >= x["date"] & date3 <= x["date2"]) %>%     # subsetting
                                   "["(2:3) %>%                                             # selecting relevant column
                                   unlist}) %>%                                             # spearding all data into a vector
  sapply(.,function(x){x[rep(c(0,length(x)/2),length(x)/2)+rep(1:(length(x)/2),each=2)]})   # reordering

row_names <- row_list %>% sapply(length) %>% which.max %>% "[["(row_list,.) %>% names() # taking the names from longest list
row_list %>% sapply(function(x){c(x,rep(NA,max(sapply(.,length))-length(x)))}) %>% # complete with NAs
 t %>% as.data.frame %>% setNames(row_names) %>% cbind(df1,.)                     # transpose, convert, set names and append to df1
#         date      date2 rain_day1 sun_day1 rain_day2 sun_day2 rain_day3 sun_day3 rain_day4 sun_day4 rain_day5 sun_day5 rain_day6 sun_day6
# 1 2014-06-01 2014-06-03         3       10         4       10         3       10        NA       NA        NA       NA        NA       NA
# 2 2017-05-15 2017-05-20         5       15         5       15         6       15         7       16         6       15         6       15

答案 1 :(得分:0)

&#34;我认为在df1上按日期对df2进行分段,将df2和rbind.fill折叠为df1将是方式&#34; ....它花费的时间比想要的长,但我做到了!

lapply(df1, class)
df1$date <- sapply( df1$date , function(x) as.character(x) ) 
df1$date2 <- sapply( df1$date2 , function(x) as.character(x) ) 
lapply(df2, class)
df2$date3 <- sapply( df2$date3 , function(x) as.character(x) ) 
lapply(df3, class)
df3$date4 <- sapply( df3$date4 , function(x) as.character(x) ) 
df3$date5 <- sapply( df3$date5 , function(x) as.character(x) ) 


tryCatch(library("plyr") , 
         error = function(e) {
           install.packages("plyr")
           library("plyr")
         }
)
df4 <- df1
for (i in 1:nrow(df1)){
  dff <- df2[ which( df2$date3 <= df1$date2[i] ) , ]
  dff <- dff[ which( dff$date3 >= df1$date[i] ) , ]
  dff <- as.data.frame(t(unlist(dff)))
  colnames(dff)[1] <- "date"
  df5 <- merge(x = df1, y = dff, by = "date")
  df5 <- rbind.fill(df4,df5)
  df5<-df5[-1,]
  df4 <- df5
} 

df5 <- df5[, -grep("^date3", colnames(df5))]
相关问题