如何基于其他列值将列值转换为行

时间:2018-06-20 20:30:55

标签: r dplyr

基本上,我想将一列中的数值用于在其他列中复制或散布分类值。

我有这样的东西:

df <- data.frame(a = c(1:3), 
             b = c(2:4), 
             c = c(1:3), 
             d = c("cow", "moose", "chicken"))
# > df
#   a b c       d
# 1 1 2 3     cow
# 2 2 3 4   moose
# 3 3 4 5 chicken

我想要这样的东西:

df2 <- data.frame(col1, col2)
col1 <- c("a", "b", "b", "c", "c", "c", "a","a")
col2 <- c( "cow", "cow", "cow", "cow", "cow", "cow", "moose", "moose")

# > df2
#   col1  col2
# 1    a   cow
# 2    b   cow
# 3    b   cow
# 4    c   cow
# 5    c   cow
# 6    c   cow
# 7    a moose
# 8    a moose ....and so on.

2 个答案:

答案 0 :(得分:2)

这是一个使用tidyr的{​​{1}}将数据从宽格式恢复为长格式,将gatherexpandRows恢复为正确的方式,以扩展数据框。

splitstackshape

数据

library(splitstackshape)
library(tidyr)
df %>% 
  gather(key, value, -d) %>% 
  expandRows(., count = 'value')
#          d key
#1       cow   a
#2     moose   a
#2.1   moose   a
#3   chicken   a
#3.1 chicken   a
#3.2 chicken   a
#4       cow   b
#4.1     cow   b
#5     moose   b
#5.1   moose   b
#5.2   moose   b
#6   chicken   b
#6.1 chicken   b
#6.2 chicken   b
#6.3 chicken   b
#7       cow   c
#8     moose   c
#8.1   moose   c
#9   chicken   c
#9.1 chicken   c
#9.2 chicken   c

答案 1 :(得分:1)

这是data.table

的一个选项
library(data.table)
melt(setDT(df), id.var = 'd', variable.name = 'key'
       )[rep(seq_len(.N), value)][, value := NULL][]
#          d key
# 1:     cow   a
# 2:   moose   a
# 3:   moose   a
# 4: chicken   a
# 5: chicken   a
# 6: chicken   a
# 7:     cow   b
# 8:     cow   b
# 9:   moose   b
#10:   moose   b
#11:   moose   b
#12: chicken   b
#13: chicken   b
#14: chicken   b
#15: chicken   b
#16:     cow   c
#17:   moose   c
#18:   moose   c
#19: chicken   c
#20: chicken   c
#21: chicken   c