R 将多id重复变量读数从长改宽

R 将多id重复变量读数从长改宽,r,reshape,data-science,reshape2,R,Reshape,Data Science,Reshape2,这就是我所拥有的: id<-c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2) measure<-c("speed","weight","time","speed","weight","time","speed","weight","time", "speed","weight","time","speed","weight","time","speed","weight","time") value<-c(1.23,10.3,3

这就是我所拥有的:

id<-c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2)
measure<-c("speed","weight","time","speed","weight","time","speed","weight","time",
           "speed","weight","time","speed","weight","time","speed","weight","time")
value<-c(1.23,10.3,33,1.44,10.4,31,1.21,10.1,33,4.25,12.5,38,1.74,10.8,31,3.21,10.3,33)
testdf<-data.frame(id,measure,value) 

id如果您想走tidyverse路线:

library(tidyr)
library(dplyr)
testdf %>% 
  # add unique id for rows to be able to use spread
  group_by(measure) %>% mutate(unique_id = row_number()) %>% 
  spread(measure, value) %>% select(-unique_id )

R Cookbook是这类问题的绝佳资源:

使用data.table中的
rowid
(与@Kelli Jean的答案非常相似):

这是我的解决办法

library(plyr)

a=daply(testdf, .(id, measure), function(x) x$value)
listdf=apply(a, c(3), function(x) rbind(data.frame(x,id=row.names(x))))
df <- ldply(listdf, data.frame)
df$.id=NULL
df <- df[order(df$id),] 
df

  speed time weight id
1  1.23   33   10.3  1
3  1.44   31   10.4  1
5  1.21   33   10.1  1
2  4.25   38   12.5  2
4  1.74   31   10.8  2
6  3.21   33   10.3  2
库(plyr)
a=daply(testdf,.(id,measure),函数(x)x$value)
listdf=apply(a,c(3),函数(x)rbind(data.frame(x,id=row.names(x)))

df首先安装重塑2以帮助重新格式化数据

然后创建另一个标识符,以帮助按所需数据集中的三行顺序一次组织数据

A<-c("A","A","A")
B<-c("B","B","B")
C<-c("C","C","C")
D<-c("D","D","D")
E<-c("E","E","E")
F<-c("F","F","F")

A <- as.data.frame(A)
colnames(A) <- "id2"
B <- as.data.frame(B)
colnames(B) <- "id2"
C <- as.data.frame(C)
colnames(C) <- "id2"
D <- as.data.frame(D)
colnames(D) <- "id2"
E <- as.data.frame(E)
colnames(E) <- "id2"
F <- as.data.frame(F)
colnames(F) <- "id2"
如果有必要,可以使用删除id2变量

 testdf$id2 <- NULL

testdf$id2我被之前发布的答案弄糊涂了,不过你的答案很有道理~补充我的:),谢谢你的回答,澄清这个问题你为什么要使用Reforme2?您是否试图将其保持为data.frame格式?为什么不发布完整的
data.table
solution:
library(data.table);dcast(如.data.table(testdf)[,rn:=rowid(measure)],id+rn~measure)[,-“rn”]
dcast(setDT(testdf),粘贴(id,rowid(measure),sep=“”)~measure)
library(plyr)

a=daply(testdf, .(id, measure), function(x) x$value)
listdf=apply(a, c(3), function(x) rbind(data.frame(x,id=row.names(x))))
df <- ldply(listdf, data.frame)
df$.id=NULL
df <- df[order(df$id),] 
df

  speed time weight id
1  1.23   33   10.3  1
3  1.44   31   10.4  1
5  1.21   33   10.1  1
2  4.25   38   12.5  2
4  1.74   31   10.8  2
6  3.21   33   10.3  2
A<-c("A","A","A")
B<-c("B","B","B")
C<-c("C","C","C")
D<-c("D","D","D")
E<-c("E","E","E")
F<-c("F","F","F")

A <- as.data.frame(A)
colnames(A) <- "id2"
B <- as.data.frame(B)
colnames(B) <- "id2"
C <- as.data.frame(C)
colnames(C) <- "id2"
D <- as.data.frame(D)
colnames(D) <- "id2"
E <- as.data.frame(E)
colnames(E) <- "id2"
F <- as.data.frame(F)
colnames(F) <- "id2"
x<-rbind(A,B,C,D,E,F)
testdf <- cbind(testdf, x)
x2<-dcast(testdf, id + id2 ~ measure, value.var="value")
  id id2 speed time weight
1  1   A  1.23   33   10.3
2  1   B  1.44   31   10.4
3  1   C  1.21   33   10.1
4  2   D  4.25   38   12.5
5  2   E  1.74   31   10.8
6  2   F  3.21   33   10.3
 testdf$id2 <- NULL