简体   繁体   English

如何重塑我的数据,将行移动到新列?

[英]How can I reshape my data, moving rows to new columns?

I know that my problem is trival, however now I'm learing methods how to reshape data in different ways, so please be understanding.我知道我的问题很简单,但是现在我正在学习如何以不同的方式重塑数据,所以请理解。

I have data like this:我有这样的数据:

Input = (
 'col1 col2
  A 2
  B 4
  A 7
  B 3
  A 4
  B 2
  A 4
  B 6
  A 3
  B 3')
df = read.table(textConnection(Input), header = T)

> df
   col1 col2
1     A    2
2     B    4
3     A    7
4     B    3
5     A    4
6     B    2
7     A    4
8     B    6
9     A    3
10    B    3

And I'd like to have something like this, where the column names are not important:我想要这样的东西,其中列名并不重要:

      col1 v1   v2   v3   v4   v5
1     A    2    7    4    4    3
2     B    4    3    2    6    3

So far, I did something like:到目前为止,我做了类似的事情:

res_1 <- aggregate(col2 ~., df, toString)
  col1          col2
1    A 2, 7, 4, 4, 3
2    B 4, 3, 2, 6, 3

And it actually works, however, I have one column and valiues are comma separated, instead of being in new columns, so I decided to fix it up:它实际上有效,但是,我有一列并且值以逗号分隔,而不是在新列中,所以我决定修复它:

res_2 <- do.call("rbind", strsplit(res_1$col2, ","))
     [,1] [,2] [,3] [,4] [,5]
[1,] "2"  " 7" " 4" " 4" " 3"
[2,] "4"  " 3" " 2" " 6" " 3"

Adn finally combine it and remove unnecessary columns: Adn 最后合并它并删除不必要的列:

final <- cbind(res_1,res_2)
final$col2 <- NULL
  col1 1  2  3  4  5
1    A 2  7  4  4  3
2    B 4  3  2  6  3

So I have my desired output, but I'm not satisfied about the method, I'm sure there's one easy and short command for this.所以我有我想要的输出,但我对方法不满意,我确定有一个简单而简短的命令。 As I said I'd like to learn new more elegant options using different packages.正如我所说,我想使用不同的包学习新的更优雅的选项。 Thanks!谢谢!

You can simply do,你可以简单地做,

do.call(rbind, split(df$col2, df$col1))
#  [,1] [,2] [,3] [,4] [,5]
#A    2    7    4    4    3
#B    4    3    2    6    3

You can wrap it to data.frame() to convert from matrix to df您可以将其包装到data.frame()以从矩阵转换为 df

The question is tagged with reshape2 and reshape so we show the use of that package and the base reshape function.这个问题用 reshape2 和reshape标记,所以我们展示了该包和基本reshape函数的使用。 Also the use of dplyr/tidyr is illustrated.还说明了 dplyr/tidyr 的使用。 Finally we show a data.table solution and a second base R solution using xtabs .最后,我们展示了一个 data.table 解决方案和一个使用xtabs的第二个基本 R 解决方案。

reshape2 Add a group column and then convert from long to wide form: reshape2添加组列,然后从长格式转换为宽格式:

library(reshape2)

df2 <- transform(df, group = paste0("v", ave(1:nrow(df), col1, FUN = seq_along)))
dcast(df2, col1 ~ group, value.var = "col2")

giving:给予:

  col1 v1 v2 v3 v4 v5
1    A  2  7  4  4  3
2    B  4  3  2  6  3

2) reshape Using df2 from (1) we have the following base R solution using the reshape function: 2)重塑使用(1)中的df2 ,我们使用reshape函数获得以下基本R解决方案:

wide <- reshape(df2, dir = "wide", idvar = "col1", timevar = "group")
names(wide) <- sub(".*\\.", "", names(wide))
wide

giving:给予:

  col1 v1 v2 v3 v4 v5
1    A  2  7  4  4  3
2    B  4  3  2  6  3

3) dplyr/tidyr 3) dplyr/tidyr

library(dplyr)
library(tidyr)

df %>%
  group_by(col1) %>%
  mutate(group = paste0("v", row_number())) %>%
  ungroup %>%
  pivot_wider(names_from = "group", values_from = "col2")

giving:给予:

# A tibble: 2 x 6
  col1     v1    v2    v3    v4    v5
  <fct> <int> <int> <int> <int> <int>
1 A         2     7     4     4     3
2 B         4     3     2     6     3

4) data.table 4) 数据表

library(data.table)

as.data.table(df)[, as.list(col2), by = col1]

giving:给予:

   col1 V1 V2 V3 V4 V5
1:    A  2  7  4  4  3
2:    B  4  3  2  6  3

5) xtabs Another base R solution uses df2 from (1) and xtabs . 5) xtabs另一个基本的 R 解决方案使用来自 (1) 和xtabs df2 。 This produces an object of class c("xtabs", "table")`.这会产生一个类 c("xtabs", "table")` 的对象。 Note that it labels the dimensions.请注意,它标记了尺寸。

xtabs(col2 ~., df2)

giving:给予:

    group
col1 v1 v2 v3 v4 v5
   A  2  7  4  4  3
   B  4  3  2  6  3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM