2015-11-29 88 views
0

我轉換了一個數據幀,但我找不到一種方法來提取第一行的行名稱(在我的示例中它是「顏色」):如何獲得轉換後的數據幀中第一列的第一行

df <- read.table(text = " color birds wolfs  
        red   9   7 
       red   8   4 
       red   2   8 
       red   2   3 
       black   8   3 
       black   1   2 
       black   7   16 
       black   1   5 
       black   17  7 
       black   8   7 
       black   2   7 
       green   20  3 
       green   6   3 
       green   1   1 
       green   3   11 
       green   30   1 ",header = TRUE) 
df_t<-t(df) 
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] 
color "red" "red" "red" "red" "black" "black" "black" "black" "black" "black" "black" "green" "green" "green" "green" "green" 
birds " 9" " 8" " 2" " 2" " 8" " 1" " 7" " 1" "17" " 8" " 2" "20" " 6" " 1" " 3" "30" 
wolfs " 7" " 4" " 8" " 3" " 3" " 2" "16" " 5" " 7" " 7" " 7" " 3" " 3" " 1" "11" " 1" 

如果我使用:df_t[1,1]我得到:

color 
"red" 

我怎麼能只提取字符串 「色」 並保存。

+1

我以爲你的問題是從'df_t [1,1]' – akrun

+0

提取'顏色'謝謝@akrun,是的,我應該更確切地問一下。 – mql4beginner

回答

2

在您的例子"color"是行的名字之一。嘗試rownames(df_t)[1]

2

我們可以使用names

str1 <- names(df_t[1,1]) 
str1 
#[1] "color" 
相關問題