Mar*_*ssa 5 r rows multiple-columns reshape
我在R中有一个数据帧,它有许多行(超过3000),其中有一个话语的F0(基频)轨道.行中包含以下信息:说话者ID,组#,重复#,重音类型,性别,然后50列F0点.数据如下所示:
Speaker Sex Group Repetition Accent Word 1 2 3 4
105 M 1 1 N AILMENT 102.31030 102.31030 102.31030 102.31127
105 M 1 1 N COLLEGE 111.80641 111.80313 111.68612 111.36020
105 M 1 1 N FATHER 124.06655 124.06655 124.06655 124.06655
Run Code Online (Sandbox Code Playgroud)
但是它不是仅仅转到X4,而是每行50个点,所以我有一个3562x56的数据帧.我想改变它,因此F0轨道中的每一列数据(因此,从1:50开始)都有自己的列,相关的列号作为另一行.我想在每个数据点的前六列中保留所有信息,所以它看起来像这样:
Speaker Sex Group Repetition Accent Word Num F0
105 M 1 1 N AILMENT 1 102.31030
105 M 1 1 N AILMENT 2 102.31030
105 M 1 1 N AILMENT 3 102.31030
105 M 1 1 N AILMENT 4 102.31127
...
105 M 1 1 N COLLEGE 1 111.80641
105 M 1 1 N COLLEGE 1 111.80313
105 M 1 1 N COLLEGE 1 111.68612
105 M 1 1 N COLLEGE 1 111.36020
...
Run Code Online (Sandbox Code Playgroud)
我尝试使用的代码虽然单调乏味,但如下所示:
x = 1
for (i in 1:dim(normrangef0)[1]) {
for (j in 1:50) {
norm.all$Speaker[x] <- normrangef0$Speaker[i]
norm.all$Sex[x] <- normrangef0$Sex[i]
norm.all$Group[x] <- normrangef0$Group[i]
norm.all$Repetition[x] <- normrangef0$Repetition[i]
norm.all$Word[x] <- normrangef0$Word[i]
norm.all$Accent[x] <- normrangef0$Accent[i]
norm.all$Time[x] <- j
norm.all$F0[x] <- normrangef0[i,j+6]
x = x+1
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我使用norm.all作为NULL对象(仅由norm.all = c()定义)时,我最终会得到一个包含超过20万个项目的列表,其中很多是NA.当我将norm.all定义为数据帧(空数或全0中的一个,在178100x8数据帧中,我得到一个错误:
$<-.data.frame(*tmp*,"Speaker"中的错误,值= 105L):替换有1行,数据有0
我的代码完全关闭了吗?还有另一种方法吗?
使用melt"reshape2"
library(reshape2)
melt(mydf, id.vars=c("Speaker", "Sex", "Group", "Repetition", "Accent", "Word"))
# Speaker Sex Group Repetition Accent Word variable value
# 1 105 M 1 1 N AILMENT 1 102.3103
# 2 105 M 1 1 N COLLEGE 1 111.8064
# 3 105 M 1 1 N FATHER 1 124.0666
# 4 105 M 1 1 N AILMENT 2 102.3103
# 5 105 M 1 1 N COLLEGE 2 111.8031
# 6 105 M 1 1 N FATHER 2 124.0666
# 7 105 M 1 1 N AILMENT 3 102.3103
# 8 105 M 1 1 N COLLEGE 3 111.6861
# 9 105 M 1 1 N FATHER 3 124.0666
# 10 105 M 1 1 N AILMENT 4 102.3113
# 11 105 M 1 1 N COLLEGE 4 111.3602
# 12 105 M 1 1 N FATHER 4 124.0666
Run Code Online (Sandbox Code Playgroud)
在基础R中,您还可以使用stack堆叠名为1到4的列,以及cbind第一组列.或者,unlist也会这样做.
您可能还想查看"data.table"包以获得一点速度提升.