R 基本整形功能错误,从宽到长格式
我有以下宽格式数据帧:R 基本整形功能错误,从宽到长格式,r,R,我有以下宽格式数据帧: df<-structure(list(ID = c(1, 2, 3), A.1 = c(0, 1, 0), A.2 = c(1, 1, 0), B.1 = c(99, 99, 0), B.2 = c(99, 99, 0)), .Names = c("ID", "A.1", "A.2", "B.1", "B.2"), row.names = c(NA, 3L), class = "data.frame") > df ID A.1 A.2 B.1
df<-structure(list(ID = c(1, 2, 3), A.1 = c(0, 1, 0), A.2 = c(1,
1, 0), B.1 = c(99, 99, 0), B.2 = c(99, 99, 0)), .Names = c("ID",
"A.1", "A.2", "B.1", "B.2"), row.names = c(NA, 3L), class = "data.frame")
> df
ID A.1 A.2 B.1 B.2
1 1 0 1 99 99
2 2 1 1 99 99
3 3 0 0 0 0
因此,这种转换是不正确的,值变得混乱。例如,对于时间点1,ID 1的参数B的值从99变为1,对于ID 1和2,参数A的值在第二个时间点变为99,依此类推
预期产出应如下所示:
> expected.long.df
ID time A B
1.1 1 1 0 99
2.1 2 1 1 99
3.1 3 1 0 0
1.2 1 2 1 99
2.2 2 2 1 99
3.2 3 2 0 0
我不知道为什么会这样。如果有任何建议,那将是非常好的 我会使用
tidyr
库:
library(tidyr)
temp1 = gather(df, key = "x", value = "y", -ID)
temp2 = separate(temp1, x, into = c("z", "time"), sep = "[.]")
temp3 = spread(temp2, key = z, value = y)
temp3
表看起来像您想要的结果,但顺序不完全相同。使用dplyr
的arrange
将其设置正确:
> dplyr::arrange(temp3, time, ID)
ID time A B
1 1 1 0 99
2 2 1 1 99
3 3 1 0 0
4 1 2 1 99
5 2 2 1 99
6 3 2 0 0
我会使用
tidyr
库:
library(tidyr)
temp1 = gather(df, key = "x", value = "y", -ID)
temp2 = separate(temp1, x, into = c("z", "time"), sep = "[.]")
temp3 = spread(temp2, key = z, value = y)
temp3
表看起来像您想要的结果,但顺序不完全相同。使用dplyr
的arrange
将其设置正确:
> dplyr::arrange(temp3, time, ID)
ID time A B
1 1 1 0 99
2 2 1 1 99
3 3 1 0 0
4 1 2 1 99
5 2 2 1 99
6 3 2 0 0
试试这个。您实际上看到的是一个
melt
ing操作
library(data.table)
df<-structure(list(ID = c(1, 2, 3), A.1 = c(0, 1, 0), A.2 = c(1, 1, 0), B.1 = c(99, 99, 0), B.2 = c(99, 99, 0)), .Names = c("ID", "A.1", "A.2", "B.1", "B.2"), row.names = c(NA, 3L), class = "data.frame")
dt <- setDT(df)
melt(dt, id = 'ID', measure = patterns('^A.', '^B.'), value.name = c('A', 'B'), variable.name = 'time')
ID time A B
1: 1 1 0 99
2: 2 1 1 99
3: 3 1 0 0
4: 1 2 1 99
5: 2 2 1 99
6: 3 2 0 0
库(data.table)
df试试这个。您实际上看到的是一个melt
ing操作
library(data.table)
df<-structure(list(ID = c(1, 2, 3), A.1 = c(0, 1, 0), A.2 = c(1, 1, 0), B.1 = c(99, 99, 0), B.2 = c(99, 99, 0)), .Names = c("ID", "A.1", "A.2", "B.1", "B.2"), row.names = c(NA, 3L), class = "data.frame")
dt <- setDT(df)
melt(dt, id = 'ID', measure = patterns('^A.', '^B.'), value.name = c('A', 'B'), variable.name = 'time')
ID time A B
1: 1 1 0 99
2: 2 1 1 99
3: 3 1 0 0
4: 1 2 1 99
5: 2 2 1 99
6: 3 2 0 0
库(data.table)
df基于您的重塑和stringr
:str\u split\u fixed
df=melt(df,'ID')
df[,c('Time','Name')]=str_split_fixed(as.character(df$variable),"[.]",2)
df$variable=NULL
reshape(df, idvar = c("ID","Name"), timevar = "Time", direction = "wide")
ID Name value.A value.B
1 1 1 0 99
2 2 1 1 99
3 3 1 0 0
4 1 2 1 99
5 2 2 1 99
6 3 2 0 0
基于您的重塑
和stringr
:str\u split\u fixed
df=melt(df,'ID')
df[,c('Time','Name')]=str_split_fixed(as.character(df$variable),"[.]",2)
df$variable=NULL
reshape(df, idvar = c("ID","Name"), timevar = "Time", direction = "wide")
ID Name value.A value.B
1 1 1 0 99
2 2 1 1 99
3 3 1 0 0
4 1 2 1 99
5 2 2 1 99
6 3 2 0 0
问题出在变量中
。我们需要正确地指定模式
reshape(df, idvar = "ID", varying = list(grep("^A", names(df)),
grep("^B", names(df))), direction = "long", v.names = c("A", "B"))
# ID time A B
#1.1 1 1 0 99
#2.1 2 1 1 99
#3.1 3 1 0 0
#1.2 1 2 1 99
#2.2 2 2 1 99
#3.2 3 2 0 0
问题出在变量中
。我们需要正确地指定模式
reshape(df, idvar = "ID", varying = list(grep("^A", names(df)),
grep("^B", names(df))), direction = "long", v.names = c("A", "B"))
# ID time A B
#1.1 1 1 0 99
#2.1 2 1 1 99
#3.1 3 1 0 0
#1.2 1 2 1 99
#2.2 2 2 1 99
#3.2 3 2 0 0
非常感谢艾米娅和勒贝尼奥斯。你的答案很好,而且都与我的数据很好地吻合。投票给Ameya只是因为data.table对我来说更熟悉,我也更容易理解代码。非常感谢Ameya和lebelinoz。你的答案很好,而且都与我的数据很好地吻合。投票给Ameya只是因为data.table对我来说更熟悉,我也更容易理解代码。非常感谢,非常感谢,非常感谢akrun,你已经了解了问题的本质。我重新获得了使用基本R函数的信心。非常感谢akrun,您已经了解了问题的本质。我重新获得了使用基本R函数的信心。