r - Rename vector rownames
问题描述
Here is my replicating example.
data <- c(100:105)
As you can view the rownames are 1:6. Instead I would like to change the rownames to a column I have called "names" where
names <- c(0,10,20,30,40,50)
I tried
cbind(names,data)
but this results in rownames being 1:6, and then a column for names, and a column for data. I want to replace the rownames 1:6 with the column "names"
Desired output:
0 100
10 101
20 102
30 103
40 104
50 105
解决方案
你可以这样做:
data <- data.frame(data)
row.names(data) <- names
data
结果:
data
0 100
10 101
20 102
30 103
40 104
50 105
编辑:如果你想保留一个向量:
data <- c(100:105)
names <- c(0,10,20,30,40,50)
attr(data,'names') <- names
attributes(data)
$`names`
[1] "0" "10" "20" "30" "40" "50"
推荐阅读
- powerbi - How to replace no data with 0 using measure in Power Bi
- asp.net-core - 核心 MVC MapRoute 模板和 RequestDelegate 不工作
- haskell - 在应用中,`<*>`如何用`fmap_i, i=0,1,2,...`来表示呢?
- java - Docker 容器中的 Apache Karaf 4 调试
- amazon-web-services - 在 terraform 上匹配 aws ami 和可用区
- twilio - Twilio 排队的呼叫未通过代理电话
- python - 如何从列表中动态生成类?
- python-3.x - 将函数应用于熊猫数据框 Python 3.7.1 时,多处理挂起
- javascript - 未捕获的 ReferenceError:未定义对话框
- python - 从 Python 运行 PowerShell 脚本