【问题标题】:renaming multiple columns of vectors in data frame without typing all names [duplicate]重命名数据框中的多列向量而不输入所有名称[重复]
【发布时间】:2020-09-26 10:17:21
【问题描述】:

不用输入 c("Col1","Col2","Col3", "Col4", "Col5", "Col6", "Col7", "Col8", "Col9", "Col10"),可以我使用更简单的方法,例如 c(Col[1:10])

library("plyr")
df<-as.data.frame(matrix)
colnames(df) <- c("Col1","Col2","Col3", "Col4", "Col5", "Col6", "Col7", "Col8", "Col9", "Col10")
rownames(df) <- rownames(hypo100.1.df, do.NULL = FALSE, prefix = "Obs.")
df$Col1 <- as.factor(df$Col1)               
df$Col2 <- as.character(df$Col2)             
df$Col3 <- as.numeric(df$Col3)               
df

【问题讨论】:

  • 使用paste0("Col", 1:10)

标签: r


【解决方案1】:

演示MrFlick 的解决方案。

df <- as.data.frame(matrix(1:20, ncol = 10))

# Without column names
df
#>   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
#> 1  1  3  5  7  9 11 13 15 17  19
#> 2  2  4  6  8 10 12 14 16 18  20

# Add column names quickly
colnames(df) <- paste0("Col", 1:10)

# With column names
df
#>   Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 Col9 Col10
#> 1    1    3    5    7    9   11   13   15   17    19
#> 2    2    4    6    8   10   12   14   16   18    20

reprex package (v0.3.0) 于 2020 年 9 月 25 日创建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 2022-10-15
    • 2019-09-17
    • 2015-12-29
    • 1970-01-01
    • 2020-08-30
    • 2017-12-19
    相关资源
    最近更新 更多