使用R

时间:2016-08-14 22:42:58

标签: r dataframe

我有一个数据框(summary_transposed_no_time),我想将其中一列重命名为我存储为值的名称。

summary_transposed_no_time看起来像这样:

       | A      | B      | C      | D
------ | ------ | ------ | ------ | ------
area_1 | 0.870  | 0.435  | 0.968  | 0.679
area_2 | 0.456  | 0.259  | 0.906  | 0.467
area_3 | 0.298  | 0.256  | 0.457  | 0.768
area_4 | 0.994  | 0.987  | 0.365  | 0.765

我的值称为test,它被设置为“B”,所以我尝试使用以下代码而没有运气:

summary_transposed_no_time <- names(summary_transposed_no_time)[c(test_col)]<-c("test")

欲望输出

       | A      | test   | C      | D
------ | ------ | ------ | ------ | ------
area_1 | 0.870  | 0.435  | 0.968  | 0.679
area_2 | 0.456  | 0.259  | 0.906  | 0.467
area_3 | 0.298  | 0.256  | 0.457  | 0.768
area_4 | 0.994  | 0.987  | 0.365  | 0.765

1 个答案:

答案 0 :(得分:2)

我认为您需要(我已将summary_transposed_no_time替换为x

names(x)[match(test_col, names(x))] <- "test"
x <- trees[1:5, ]
#  Girth Height Volume
#1   8.3     70   10.3
#2   8.6     65   10.3
#3   8.8     63   10.2
#4  10.5     72   16.4
#5  10.7     81   18.8

names(x)[match("Girth", names(x))] <- "test"
#   test Height Volume
#1  8.3     70   10.3
#2  8.6     65   10.3
#3  8.8     63   10.2
#4 10.5     72   16.4
#5 10.7     81   18.8