I have a data frame that looks like:
我有一个数据帧,看起来像:
ID Time U1 U2 U3 U4 ...
1 20 1 2 3 5 ..
2 20 2 5 9 4 ..
3 20 2 5 6 4 ..
.
.
And I would need to keep it like:
ID Time U
1 20 1
1 20 2
1 20 3
1 20 5
2 20 2
2 20 5
2 20 9
2 20 4
3 20 2
3 20 5
3 20 6
3 20 4
I tried with:
我试着:
X <- read.table("mydata.txt", header=TRUE, sep=",")
X_D <- as.data.frame(X)
X_new <- stack(X_D, select = -c(ID, Time))
But I haven't managed to get the data into that form. Honestly, I have little experience with stacking/transposing, so any help is greatly appreciated!
但是我还没有把数据输入到这个表格中。老实说,我对堆叠/调换没有什么经验,所以任何帮助都非常感谢!
4 个解决方案
#1
6
Here's the stack
approach:
这是堆栈的方法:
dat2a <- data.frame(dat[1:2], stack(dat[3:ncol(dat)]))
dat2a
# ID Time values ind
# 1 1 20 1 U1
# 2 2 20 2 U1
# 3 3 20 2 U1
# 4 1 20 2 U2
# 5 2 20 5 U2
# 6 3 20 5 U2
# 7 1 20 3 U3
# 8 2 20 9 U3
# 9 3 20 6 U3
# 10 1 20 5 U4
# 11 2 20 4 U4
# 12 3 20 4 U4
This is very similar to melt
from "reshape2":
这与“reshape2”的融化非常相似:
library(reshape2)
dat2b <- melt(dat, id.vars=1:2)
dat2b
# ID Time variable value
# 1 1 20 U1 1
# 2 2 20 U1 2
# 3 3 20 U1 2
# 4 1 20 U2 2
# 5 2 20 U2 5
# 6 3 20 U2 5
# 7 1 20 U3 3
# 8 2 20 U3 9
# 9 3 20 U3 6
# 10 1 20 U4 5
# 11 2 20 U4 4
# 12 3 20 U4 4
And, very similar to @TylerRinker's answer, but not dropping the "times", is to just use sep = ""
to help R guess time and variable names.
与@TylerRinker的答案非常相似,但不是删除“times”,而是使用sep =“”来帮助猜测时间和变量名。
dat3 <- reshape(dat, direction = "long", idvar=1:2,
varying=3:ncol(dat), sep = "", timevar="Measure")
dat3
# ID Time Measure U
# 1.20.1 1 20 1 1
# 2.20.1 2 20 1 2
# 3.20.1 3 20 1 2
# 1.20.2 1 20 2 2
# 2.20.2 2 20 2 5
# 3.20.2 3 20 2 5
# 1.20.3 1 20 3 3
# 2.20.3 2 20 3 9
# 3.20.3 3 20 3 6
# 1.20.4 1 20 4 5
# 2.20.4 2 20 4 4
# 3.20.4 3 20 4 4
In all three of those, you end up with four columns, not three, like you describe in your desired output. However, as @ndoogan points out, by doing so, you're loosing information about your data. If you're fine with that, you can always drop that column from the resulting data.frame
quite easily (for example, dat2a <- dat2a[-4]
.
在这三种情况下,你最后得到的是四列,而不是三列,就像你在期望的输出中描述的那样。但是,正如@ndoogan指出的那样,通过这样做,您将丢失关于您的数据的信息。如果您对此很满意,那么您可以很容易地从结果数据a中删除该列(例如,dat2a <- dat2a[-4]。
#2
2
With base reshape
:
基地改造:
dat <- read.table(text="ID Time U1 U2 U3 U4
1 20 1 2 3 5
2 20 2 5 9 4
3 20 2 5 6 4", header=TRUE)
colnames(dat) <- gsub("([a-zA-Z]*)([0-9])", "\\1.\\2", colnames(dat))
reshape(dat, varying=3:ncol(dat), v.names="U", direction ="long", timevar = "Time",
idvar = "ID")
#3
0
Try this:
试试这个:
do.call(rbind, lapply(1:4, function(i)structure(dat[,c("ID", "Time", paste0("U",i))], names=c("ID", "Time", "U"))))
Where dat
is your data.frame...
dat是你的数据。
#4
0
You can also use melt():
你也可以使用熔体():
library(reshape2)
new_data <- melt(old_data, id.vars=c("ID","Time"),
value.name = "U")
Then remove the 'variable' column:
然后移除“变量”栏:
new_data$variable <- NULL
#1
6
Here's the stack
approach:
这是堆栈的方法:
dat2a <- data.frame(dat[1:2], stack(dat[3:ncol(dat)]))
dat2a
# ID Time values ind
# 1 1 20 1 U1
# 2 2 20 2 U1
# 3 3 20 2 U1
# 4 1 20 2 U2
# 5 2 20 5 U2
# 6 3 20 5 U2
# 7 1 20 3 U3
# 8 2 20 9 U3
# 9 3 20 6 U3
# 10 1 20 5 U4
# 11 2 20 4 U4
# 12 3 20 4 U4
This is very similar to melt
from "reshape2":
这与“reshape2”的融化非常相似:
library(reshape2)
dat2b <- melt(dat, id.vars=1:2)
dat2b
# ID Time variable value
# 1 1 20 U1 1
# 2 2 20 U1 2
# 3 3 20 U1 2
# 4 1 20 U2 2
# 5 2 20 U2 5
# 6 3 20 U2 5
# 7 1 20 U3 3
# 8 2 20 U3 9
# 9 3 20 U3 6
# 10 1 20 U4 5
# 11 2 20 U4 4
# 12 3 20 U4 4
And, very similar to @TylerRinker's answer, but not dropping the "times", is to just use sep = ""
to help R guess time and variable names.
与@TylerRinker的答案非常相似,但不是删除“times”,而是使用sep =“”来帮助猜测时间和变量名。
dat3 <- reshape(dat, direction = "long", idvar=1:2,
varying=3:ncol(dat), sep = "", timevar="Measure")
dat3
# ID Time Measure U
# 1.20.1 1 20 1 1
# 2.20.1 2 20 1 2
# 3.20.1 3 20 1 2
# 1.20.2 1 20 2 2
# 2.20.2 2 20 2 5
# 3.20.2 3 20 2 5
# 1.20.3 1 20 3 3
# 2.20.3 2 20 3 9
# 3.20.3 3 20 3 6
# 1.20.4 1 20 4 5
# 2.20.4 2 20 4 4
# 3.20.4 3 20 4 4
In all three of those, you end up with four columns, not three, like you describe in your desired output. However, as @ndoogan points out, by doing so, you're loosing information about your data. If you're fine with that, you can always drop that column from the resulting data.frame
quite easily (for example, dat2a <- dat2a[-4]
.
在这三种情况下,你最后得到的是四列,而不是三列,就像你在期望的输出中描述的那样。但是,正如@ndoogan指出的那样,通过这样做,您将丢失关于您的数据的信息。如果您对此很满意,那么您可以很容易地从结果数据a中删除该列(例如,dat2a <- dat2a[-4]。
#2
2
With base reshape
:
基地改造:
dat <- read.table(text="ID Time U1 U2 U3 U4
1 20 1 2 3 5
2 20 2 5 9 4
3 20 2 5 6 4", header=TRUE)
colnames(dat) <- gsub("([a-zA-Z]*)([0-9])", "\\1.\\2", colnames(dat))
reshape(dat, varying=3:ncol(dat), v.names="U", direction ="long", timevar = "Time",
idvar = "ID")
#3
0
Try this:
试试这个:
do.call(rbind, lapply(1:4, function(i)structure(dat[,c("ID", "Time", paste0("U",i))], names=c("ID", "Time", "U"))))
Where dat
is your data.frame...
dat是你的数据。
#4
0
You can also use melt():
你也可以使用熔体():
library(reshape2)
new_data <- melt(old_data, id.vars=c("ID","Time"),
value.name = "U")
Then remove the 'variable' column:
然后移除“变量”栏:
new_data$variable <- NULL