gpt4 book ai didi

r - 如何在转换后的数据框中获取第一列的第一行

转载 作者:行者123 更新时间:2023-12-04 01:58:27 24 4
gpt4 key购买 nike

我转换了一个数据框,但找不到提取第一行行名的方法(在我的示例中是“颜色”):

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"

如何只提取字符串“color”并保存。

最佳答案

在您的示例中,"color" 是行名之一。试试 rownames(df_t)[1]

关于r - 如何在转换后的数据框中获取第一列的第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33981418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com