我正在处理此类型的数据集(示例数据框):
V1<-c("-","-","*","-","-","*","*")
V2<-c("01","01","01","02","02","02","02")
IND<-c("a","b","c","d","e","f","g")
sex<-c(1,2,1,1,2,2,2)
rang<-c(1,2,3,1,2,3,4)
df<-data.frame(V1,V2,IND,sex,rang)
我使用了重塑命令
df1<-reshape(df,idvar = "V2",v.names = c("V1","IND","sex"),
timevar = "rang",direction="wide")
我得到了这个结果:
> df1
V2 V1.1 IND.1 sex.1 V1.2 IND.2 sex.2 V1.3 IND.3 sex.3 V1.4 IND.4 sex.4
1 01 - a 1 - b 2 * c 1 <NA> <NA> NA
4 02 - d 1 - e 2 * f 2 * g 2
接近期望的结果。
但是我想知道是否有办法将indvidus返回到行的高于3,从第1列和第2列保留相同的个体,得到这样的结果:
V2 V1.1 IND.1 sex.1 V1.2 IND.2 sex.2 V1.3 IND.3 sex.3
1 01 - a 1 - b 2 * c 1
4 02 - d 1 - e 2 * f 2
5 02 - d 1 - e 2 * g 2
或者我应该手动拿走我的桌子吗?
提前感谢您的回答
此致
Theo DA SILVA