You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R语言中将数据集行名替换为作者名,适配mtcars格式?

如何将R数据框的行名替换为author列的值(模仿mtcars格式)

你想要把数据框的数字行名替换成author列的内容,就像mtcars用车型名作为行名那样,这个需求很常见,有两种简单的实现方式:

方法1:基础R实现

先加载你的数据(运行你提供的structure()代码得到数据框,这里我们命名为df),然后直接用rownames()函数赋值,还可以选择删除原author列来完全匹配mtcars的结构:

# 加载数据
df <- structure(list(author = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("dispt", "Hamilton", "HM", "Jay", "Madison"), class = "factor"), filename = structure(1:6, .Label = c("dispt_fed_49.txt", "dispt_fed_50.txt", "dispt_fed_51.txt", "dispt_fed_52.txt", "dispt_fed_53.txt", "dispt_fed_54.txt", "dispt_fed_55.txt", "dispt_fed_56.txt", "dispt_fed_57.txt", "dispt_fed_62.txt", "dispt_fed_63.txt", "Hamilton_fed_1.txt", "Hamilton_fed_11.txt", "Hamilton_fed_12.txt", "Hamilton_fed_13.txt", "Hamilton_fed_15.txt", "Hamilton_fed_16.txt", "Hamilton_fed_17.txt", "Hamilton_fed_21.txt", "Hamilton_fed_22.txt", "Hamilton_fed_23.txt", "Hamilton_fed_24.txt", "Hamilton_fed_25.txt", "Hamilton_fed_26.txt", "Hamilton_fed_27.txt", "Hamilton_fed_28.txt", "Hamilton_fed_29.txt", "Hamilton_fed_30.txt", "Hamilton_fed_31.txt", "Hamilton_fed_32.txt", "Hamilton_fed_33.txt", "Hamilton_fed_34.txt", "Hamilton_fed_35.txt", "Hamilton_fed_36.txt", "Hamilton_fed_59.txt", "Hamilton_fed_6.txt", "Hamilton_fed_60.txt", "Hamilton_fed_61.txt", "Hamilton_fed_65.txt", "Hamilton_fed_66.txt", "Hamilton_fed_67.txt", "Hamilton_fed_68.txt", "Hamilton_fed_69.txt", "Hamilton_fed_7.txt", "Hamilton_fed_70.txt", "Hamilton_fed_71.txt", "Hamilton_fed_72.txt", "Hamilton_fed_73.txt", "Hamilton_fed_74.txt", "Hamilton_fed_75.txt", "Hamilton_fed_76.txt", "Hamilton_fed_77.txt", "Hamilton_fed_78.txt", "Hamilton_fed_79.txt", "Hamilton_fed_8.txt", "Hamilton_fed_80.txt", "Hamilton_fed_81.txt", "Hamilton_fed_82.txt", "Hamilton_fed_83.txt", "Hamilton_fed_84.txt", "Hamilton_fed_85.txt", "Hamilton_fed_9.txt", "HM_fed_18.txt", "HM_fed_19.txt", "HM_fed_20.txt", "Jay_fed_2.txt", "Jay_fed_3.txt", "Jay_fed_4.txt", "Jay_fed_5.txt", "Jay_fed_64.txt", "Madison_fed_10.txt", "Madison_fed_14.txt", "Madison_fed_37.txt", "Madison_fed_38.txt", "Madison_fed_39.txt", "Madison_fed_40.txt", "Madison_fed_41.txt", "Madison_fed_42.txt", "Madison_fed_43.txt", "Madison_fed_44.txt", "Madison_fed_45.txt", "Madison_fed_46.txt", "Madison_fed_47.txt", "Madison_fed_48.txt", "Madison_fed_58.txt"), class = "factor"), a = c(0.28, 0.177, 0.339, 0.27, 0.303, 0.245), all = c(0.052, 0.063, 0.09, 0.024, 0.054, 0.059), also = c(0.009, 0.013, 0.008, 0.016, 0.027, 0.007), an = c(0.096, 0.038, 0.03, 0.024, 0.034, 0.067), and = c(0.358, 0.393, 0.301, 0.262, 0.404, 0.282), any = c(0.026, 0.063, 0.008, 0.056, 0.04, 0.052), are = c(0.131, 0.051, 0.068, 0.064, 0.128, 0.111), as = c(0.122, 0.139, 0.203, 0.111, 0.148, 0.252), at = c(0.017, 0.114, 0.023, 0.056, 0.013, 0.015), be = c(0.411, 0.393, 0.474, 0.365, 0.344, 0.297)), .Names = c("author", "filename", "a", "all", "also", "an", "and", "any", "are", "as", "at", "be"), row.names = c(NA, 6L), class = "data.frame")

# 将author列的值设置为行名
rownames(df) <- df$author

# 可选:删除原author列,让结构更贴近mtcars
df <- df[, !names(df) %in% "author"]

方法2:用dplyr(tidyverse)实现

如果你习惯使用tidyverse工具链,dplyr包的column_to_rownames()函数可以一步完成转置和删除原列的操作:

library(dplyr)

# 加载数据后直接转换
df <- df %>%
  column_to_rownames(var = "author")

注意事项

  • 如果author列存在重复值(比如你的示例里全是dispt),R会自动给重复的行名添加.1、.2这样的后缀,保证所有行名唯一,这和mtcars里的行名规则一致(mtcars的行名都是唯一的)。
  • 之后如果需要把行名转回数据列,可以用rownames_to_column()函数(同样来自dplyr),用法类似:df %>% rownames_to_column(var = "author")。

内容的提问来源于stack exchange,提问作者Data_is_Power

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:34:46