如何将R中某些行中的信息移动到列中?

时间:2014-11-14 14:32:41

标签: r reshape

我希望在R中做一些看起来类似于我将使用reshape包的东西,但不完全。我希望将数据帧的某些行移动到列中但不是全部。例如,我的数据框看起来像:

v1, v2, v3
info, time, 12:00
info, day, Monday
info, temperature, 70
data, 1, 2
data, 2, 2
data, 3, 1
data, 4, 1
data, 5, 3

我想将其转换为:

v1, v2, v3, info_time, info_day, info_temperature
data, 1, 2, 12:00, Monday, 70
data, 2, 2, 12:00, Monday, 70
data, 3, 1, 12:00, Monday, 70
data, 4, 1, 12:00, Monday, 70
data, 5, 3, 12:00. Monday, 70

有一种简单的方法吗?重塑包有帮助吗?

提前感谢您的帮助!

文森特

2 个答案:

答案 0 :(得分:1)

尝试

library(reshape2)
indx <- df$v1=='data'
res <- cbind(df[indx,],dcast(df[!indx,],v1~v2, value.var='v3'))[,-4]
row.names(res) <- NULL
colnames(res)[4:6] <- paste('info', colnames(res)[4:6], sep="_")
res
#    v1 v2 v3 info_day info_temperature info_time
#1 data  1  2   Monday               70     12:00
#2 data  2  2   Monday               70     12:00
#3 data  3  1   Monday               70     12:00
#4 data  4  1   Monday               70     12:00
#5 data  5  3   Monday               70     12:00

或使用dplyr/tidyr

library(dplyr)
library(tidyr)

 cbind(df[indx,], 
       unite(df[!indx,], Var, v1, v2) %>% 
                                mutate(id=1) %>%
                                spread(Var, v3)%>%
                                select(-id))

或使用base R

 cbind(df[indx,], 
      reshape(transform(df[!indx,], v2= paste(v1, v2, sep="_")),
          idvar='v1', timevar='v2', direction='wide')[,-1]) 

数据

df <-  structure(list(v1 = c("info", "info", "info", "data", "data", 
"data", "data", "data"), v2 = c("time", "day", "temperature", 
"1", "2", "3", "4", "5"), v3 = c("12:00", "Monday", "70", "2", 
"2", "1", "1", "3")), .Names = c("v1", "v2", "v3"), class = "data.frame",
row.names = c(NA, -8L))

答案 1 :(得分:0)

没有外部包的解决方案(使用Akrun的df结构):

df1 <- cbind(df[4:8,1:3],apply(df[1:3,3,drop=FALSE],1,function(x) rep(x,nrow(df)-3)))
colnames(df1)[4:6] <- paste("info",df[1:3,2], sep = "_")
df1

> df1
    v1 v2 v3 info_time info_day info_temperature
4 data  1  2     12:00   Monday               70
5 data  2  2     12:00   Monday               70
6 data  3  1     12:00   Monday               70
7 data  4  1     12:00   Monday               70
8 data  5  3     12:00   Monday               70