有没有一种快速的方法可以将数据框的所有列附加到单个列中?

时间:2021-01-28 07:49:41

标签: r dataframe

我找不到将数据框转换为由 df 列组成的向量的快速方法。 我有一个由每 y 列 x 行组成的 df,我想要一个向量或一个列表或一个 df(类并不重要),它是每 y 行 x 并且只有 3 列,其中一个是行名(对每一列重复),第二个是列出的值(数据),第三个是重复的列名。 为了更好地解释,我想从这里开始

<头>
c1 c2 c3
n1 0.1 0.2 0.3
n2 0.4 0.5 0.6
n3 0.7 0.8 0.9

到这里

<头>
价值观 列名
n1 0.1 c1
n2 0.4 c1
n3 0.7 c1
n1 0.2 c2
n2 0.5 c2
n3 0.8 c2

是否有一种快速的方法来操作这个数据框,或者唯一的方法是逐列抓取和rbind()

3 个答案:

答案 0 :(得分:2)

在基础 R 中:

result <- data.frame(row = rownames(df1), 
                     name = rep(names(df1), each = ncol(df1)), 
                     value = unlist(df1), row.names = NULL)

result
#  row name value
#1  n1   c1   0.1
#2  n2   c1   0.4
#3  n3   c1   0.7
#4  n1   c2   0.2
#5  n2   c2   0.5
#6  n3   c2   0.8
#7  n1   c3   0.3
#8  n2   c3   0.6
#9  n3   c3   0.9

或者使用 tidyrpivot_longer :

library(dplyr)
library(tidyr)

df1 %>% rownames_to_column('row') %>% pivot_longer(cols = -row)

数据

df1 <- structure(list(c1 = c(0.1, 0.4, 0.7), c2 = c(0.2, 0.5, 0.8), 
    c3 = c(0.3, 0.6, 0.9)), class = "data.frame", 
    row.names = c("n1", "n2", "n3"))

答案 1 :(得分:1)

您可以使用stack

cbind(row = rownames(x), stack(x))
#  row values ind
#1  n1    0.1  c1
#2  n2    0.4  c1
#3  n3    0.7  c1
#4  n1    0.2  c2
#5  n2    0.5  c2
#6  n3    0.8  c2
#7  n1    0.3  c3
#8  n2    0.6  c3
#9  n3    0.9  c3

数据

x <- read.table(header=TRUE, text="c1   c2  c3
n1  0.1     0.2     0.3
n2  0.4     0.5     0.6
n3  0.7     0.8     0.9")

答案 2 :(得分:0)

使用 reshape2 包的 melt() 函数的命题:

df <- read.table(header = TRUE, text = "
n  c1  c2  c3
n1 0.1 0.2 0.3
n2 0.4 0.5 0.6
n3 0.7 0.8 0.9
")

reshape2::melt(df,
               id.vars = "n",
               mesure.vars = c("c1":"c3"),
               variable.name = "var",
               value.name = "val")
#>    n var val
#> 1 n1  c1 0.1
#> 2 n2  c1 0.4
#> 3 n3  c1 0.7
#> 4 n1  c2 0.2
#> 5 n2  c2 0.5
#> 6 n3  c2 0.8
#> 7 n1  c3 0.3
#> 8 n2  c3 0.6
#> 9 n3  c3 0.9

# Created on 2021-02-02 by the reprex package (v0.3.0.9001)