我对R还是比较陌生,想将要使用的数据框从宽改为高。
我现在拥有的数据如下所示:
df.wide<-read.csv(header=T, text="
ID, Group, left.hemisphere.thk, right.hemisphere.thk, left.hemisphere.vol, right.hemisphere.vol, left.hemisphere.sa, right.hemisphere.sa
100, 0, 72, 55, 18, 6, 333, 22
101, 0, 73, 50, 19, 7, 332, 11
103, 0, 72, 49, 20, 1, 300, 14
200, 1, 50, 45, 8, 1, 100, 44
201, 1, 52, 52, 9, 2, 222, 18
203, 1, 50, 33, 10, 10, 100, 15")
我想将数据集转换为长数据,并创建新的变量列,使其看起来如下所示:
# make Hemisphere = 0 (if left) =1 (if right)
ID Group Hemisphere Brain.Data
100 0 0 72
100 0 1 55
100 0 0 18
100 0 1 6
100 0 0 333
100 0 1 22
101 0 0 73
101 0 1 50
101 0 0 19
101 0 1 7
101 0 0 332
.
.
.
.
203 1 1 15
答案 0 :(得分:2)
我们可以使用gather
中的tidyr
,并通过将列名重新编码为0和1来创建“半球”
library(tidyverse)
df.wide %>%
gather(Hemisphere, Brain.data, 3:4) %>%
mutate(Hemisphere = group_indices(., Hemisphere) - 1) %>%
arrange(ID)
# ID Group Hemisphere Brain.data
#1 100 0 0 72
#2 100 0 1 55
#3 101 0 0 73
#4 101 0 1 50
#5 103 0 0 72
#6 103 0 1 49
#7 200 1 0 50
#8 200 1 1 45
#9 201 1 0 52
#10 201 1 1 52
#11 203 1 0 50
#12 203 1 1 33
答案 1 :(得分:1)
您可以将stats::reshape
与direction = "long"
一起使用:
> df.long <- reshape(df.wide, idvar = "ID",
+ times = c("left.hemisphere.thk", "right.hemisphere.thk"),
+ timevar = "Hemisphere",
+ varying = list(c("left.hemisphere.thk", "right.hemisphere.thk")),
+ direction = "long")
> rownames(df.long) <- NULL
> colnames(df.long) <- c("ID", "Group", "Hemisphere", "Brain.Data")
> df.long
ID Group Hemisphere Brain.Data
1 100 0 left.hemisphere.thk 72
2 101 0 left.hemisphere.thk 73
3 103 0 left.hemisphere.thk 72
4 200 1 left.hemisphere.thk 50
5 201 1 left.hemisphere.thk 52
6 203 1 left.hemisphere.thk 50
7 100 0 right.hemisphere.thk 55
8 101 0 right.hemisphere.thk 50
9 103 0 right.hemisphere.thk 49
10 200 1 right.hemisphere.thk 45
11 201 1 right.hemisphere.thk 52
12 203 1 right.hemisphere.thk 33
再次尝试搜索数据透视表/数据透视表或铸造/熔化表。