2016-06-28 88 views
1
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列數據框,每個字符串作爲一行。我會怎麼做?如何構建明智的數據框架而不是明智的列?

回答

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

輸出:

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

另一種選擇是使用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" 
0

您可以通過建行矩陣,並且將其轉換成一個DF可以得到所需的輸出

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

此代碼會產生錯誤。 – Megatron

+0

修復了代碼。 – mkearney

0

使用rbind和as.data.frame功能。

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