繁体   English   中英

我如何构建一个数据行明智的而不是列明智的?

[英]How do I build a data frame row wise not column wise?

a <- "dog cat carrot cabbage"
b <- "orange cat chair cabbage"
c <- "dog phone book beach"
x <- data.frame(a,b,c)
> x
                       a                        b                    c
1 dog cat carrot cabbage orange cat chair cabbage dog phone book beach

因此,它已内置在列中。 我想要的是一个1列数据帧,每个字符串都作为一行。 我该怎么做?

d <- c(a,b,c)
data.frame(d)

输出:

                         d
1   dog cat carrot cabbage
2 orange cat chair cabbage
3     dog phone book beach

另一种选择是使用as.data.frame

as.data.frame(c(a,b,c))

#        c(a, b, c)
#1   dog cat carrot cabbage
#2   orange cat chair cabbage
#3   dog phone book beach

您也可以使用rbind

rbind(a, b, c)

#   [,1]                      
#a "dog cat carrot cabbage"  
#b "orange cat chair cabbage"
#c "dog phone book beach"  

您可以按行构建矩阵并将其转换为df

data.frame(matrix(c(1, 7, 4, 6, 2, 0, 6, 2, 8), 3, 3, byrow = TRUE))

使用rbind和as.data.frame函数可以获得所需的输出。

as.data.frame(rbind(a,b,c))

暂无
暂无

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

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