简体   繁体   English

将数据帧放入r中的数组中

[英]Put data frame into array in r

I Have two data frames : 我有两个数据框:

The first one : 第一个 :

c1.df1 <- c(10, 55, 3)
c2.df1 <- c(-5, 18, 60)
df1 <- data.frame(c1.df1, c2.df1)
rownames(df1) <- c("row1", "row2", "row3")
colnames(df1) <- c("col1", "col2")

The result : 结果 :

     col1 col2
row1   10   -5
row2   55   18
row3    3   60

The seconde one : 借调一:

c1.df2 <- c(12, -99, 98)
c2.df2 <- c(-55, 8, 45)
df2 <- data.frame(c1.df2, c2.df2)
rownames(df2) <- c("row1", "row2", "row3")
colnames(df2) <- c("col1", "col2")

The result : 结果 :

     col1 col2
row1   12  -55
row2  -99    8
row3   98   45

i want to put the two of them into an array, and get as result: 我想把它们中的两个放入一个数组中,得到结果:

, , 1
     col1 col2
row1   10   -5
row2   55   18
row3    3   60

, , 2
    Col1 Col2
row1   12  -55
row2  -99    8
row3   98   45

I tried : 我试过了 :

res <- array(c(df1, df2), c(3, 2, 2))

but i do not get what i want. 但我没有得到我想要的东西。

We need to use unlist 我们需要使用unlist

array(c(unlist(df1), unlist(df2)), c(3, 2, 2), 
            dimnames=list(rownames(df1), colnames(df1)))

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

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