R中如何将列转换为行?

发布于 2024-10-21 06:11:15 字数 322 浏览 9 评论 0原文

我也有同样的问题。我有这种顺序的数据: ;=column

D1 ;hurs

1  ;0.12

1  ;0.23

1  ;0.34

1  ;0.01

2  ;0.24

2  ;0.67

2  ;0.78

2  ;0.98

我喜欢这样:

D1; X; X; X; X    
1;0.12; 0.23; 0.34; 0.01; 
2;0.24; 0.67; 0.78; 0.98;

我想根据 D1 对它进行排序并想重塑它?有人有想法吗?我需要对 D1 的 7603 个值执行此操作。

I kind of have the same problem. I have data in this kind of order: ;=column

D1 ;hurs

1  ;0.12

1  ;0.23

1  ;0.34

1  ;0.01

2  ;0.24

2  ;0.67

2  ;0.78

2  ;0.98

and I like to have it like this:

D1; X; X; X; X    
1;0.12; 0.23; 0.34; 0.01; 
2;0.24; 0.67; 0.78; 0.98;

I would like to sort it with respect to D1 and like to reshape it? Does anyone have an idea? I need to do this for 7603 values of D1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

请远离我 2024-10-28 06:11:15

我会研究 Hadley 的 reshape 包。它可以做各种很棒的事情。下面的代码将适用于您的玩具示例,但可能有一种更优雅的方法来执行此操作。简而言之,您的数据已经呈现为 ?melt 形式,因此您可以简单地 ?cast 它。

另外,请查看这些链接

http://www.statmethods.net/management/reshape.html

http://had.co.nz/reshape/

library(reshape)

help(package=reshape)
?melt

D1 <- c(1,1,1,1,2,2,2,2)
hurs <- c(.12, .23, .34, .01, .24, .67, .78, .98)
var <- rep(paste("X", 1:4, sep=""), 2)

foo <- data.frame(D1, var, hurs)
foo

cast(foo, D1~var)

I would look into Hadley's reshape package. It does all sorts of great stuff. The code below will work with your toy example, but there is probably a more elegant way of doing this. Simply, your data already appear to be in the ?melt form, so you can simply ?cast it.

Also, check out these links

http://www.statmethods.net/management/reshape.html

http://had.co.nz/reshape/

library(reshape)

help(package=reshape)
?melt

D1 <- c(1,1,1,1,2,2,2,2)
hurs <- c(.12, .23, .34, .01, .24, .67, .78, .98)
var <- rep(paste("X", 1:4, sep=""), 2)

foo <- data.frame(D1, var, hurs)
foo

cast(foo, D1~var)
情深如许 2024-10-28 06:11:15

挖掘不可能被认领的骷髅,为什么不使用aggregate()呢?

dat = read.table(header = TRUE, sep = ";", text = "D1 ;hurs
1  ;0.12
1  ;0.23
1  ;0.34
1  ;0.01
2  ;0.24
2  ;0.67
2  ;0.78
2  ;0.98")
aggregate(hurs ~ D1, dat, c)
#   D1 hurs.1 hurs.2 hurs.3 hurs.4
# 1  1   0.12   0.23   0.34   0.01
# 2  2   0.24   0.67   0.78   0.98

如果 D1 中每个 id 的长度不相同,您还可以在首先创建“时间”变量后使用基本 R reshape()

dat2 <- dat[-8, ]
dat2$timeSeq <- ave(dat2$D1, dat2$D1, FUN = seq_along)
reshape(dat2, direction="wide", idvar="D1", timevar="timeSeq")
#   D1 hurs.1 hurs.2 hurs.3 hurs.4
# 1  1   0.12   0.23   0.34   0.01
# 5  2   0.24   0.67   0.78     NA

Digging up skeletons not likely to ever be claimed, why not use aggregate()?

dat = read.table(header = TRUE, sep = ";", text = "D1 ;hurs
1  ;0.12
1  ;0.23
1  ;0.34
1  ;0.01
2  ;0.24
2  ;0.67
2  ;0.78
2  ;0.98")
aggregate(hurs ~ D1, dat, c)
#   D1 hurs.1 hurs.2 hurs.3 hurs.4
# 1  1   0.12   0.23   0.34   0.01
# 2  2   0.24   0.67   0.78   0.98

If the lengths of each id in D1 are not the same, you can also use base R reshape() after first creating a "time" variable:

dat2 <- dat[-8, ]
dat2$timeSeq <- ave(dat2$D1, dat2$D1, FUN = seq_along)
reshape(dat2, direction="wide", idvar="D1", timevar="timeSeq")
#   D1 hurs.1 hurs.2 hurs.3 hurs.4
# 1  1   0.12   0.23   0.34   0.01
# 5  2   0.24   0.67   0.78     NA
伤痕我心 2024-10-28 06:11:15

我假设每个 D1 的小时数不相等(7603 值)

txt = 'D1 ;hurs
 1 ;0.12
 1 ;0.23
 1 ;0.34
 1 ;0.01
 2 ;0.24
 2 ;0.67
 2 ;0.78
 2 ;0.98'

dat <- read.table(textConnection(txt),header=T,sep=";")
dat$Lp <- 1:nrow(dat)
dat <- dat[order(dat$D1,dat$Lp),]
out <- split(dat$hurs,dat$D1)
out <- sapply(names(out),function(x) paste(paste(c(x,out[[x]]),collapse=";"),";",sep="",collapse=""))

I have assumed that there are unequal number of hurs per D1 (7603 values)

txt = 'D1 ;hurs
 1 ;0.12
 1 ;0.23
 1 ;0.34
 1 ;0.01
 2 ;0.24
 2 ;0.67
 2 ;0.78
 2 ;0.98'

dat <- read.table(textConnection(txt),header=T,sep=";")
dat$Lp <- 1:nrow(dat)
dat <- dat[order(dat$D1,dat$Lp),]
out <- split(dat$hurs,dat$D1)
out <- sapply(names(out),function(x) paste(paste(c(x,out[[x]]),collapse=";"),";",sep="",collapse=""))
顾冷 2024-10-28 06:11:15

reshape2 实际上比 reshape 更好。使用 reshape 比 reshape2 使用更多的内存和时间(至少对于我使用 900 万行之类的特定示例而言)。

reshape2 is actually better than reshape. Using reshape uses significantly more memory and time than reshape2 (at least for my specific example using something like 9million rows).

风流物 2024-10-28 06:11:15

您可以检查 Hadley Wickham 的 reshape 包及其cast() 函数

http://had.co.nz/reshape/< /a>

You might check Hadley Wickham's reshape package and its cast() function

http://had.co.nz/reshape/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文