简体   繁体   English

从 R dataframe 中的列名中去除字符的更有效方法?

[英]A more efficient way to strip a character from column names in an R dataframe?

I want to strip the X's from the column names in this R data frame.我想从这个 R 数据框中的列名中删除 X。

> d
     days X2000 X2001 X2002 X2003
1 June-01    90    85    88    75
2 June-02    93    84    88    81
3 June-03    94    83    85    83

Here's what I've got so far.这是我到目前为止所得到的。 It gets me the column names that I want, by taking the substring of each of the columns 2 through 5.它通过获取第 2 到第 5 列的 substring 得到我想要的列名。

> new_colnames <- c()
> for (name in colnames(d[2:5])) { new_colnames <- c(new_colnames, substring(name, 2)) }
> colnames(d) <- c('days', new_colnames)
> d
     days 2000 2001 2002 2003
1 June-01   90   85   88   75
2 June-02   93   84   88   81
3 June-03   94   83   85   83

Is there a more efficient way of doing this?有没有更有效的方法来做到这一点? What is "best practice"?什么是“最佳实践”?

As someone already suggested above, you just need to use the sub command.正如上面已经有人建议的那样,您只需要使用sub命令。 I recreated your database above and did just this:我在上面重新创建了您的数据库并这样做了:

# Load piping library:
library(tidyverse)

# Create days variable:
days <- c("June-01",
          "June-02",
          "June-03")

# Create year variables:
X2000 <- c(90,93,94)
X2001 <- c(85,84,83)
X2002 <- c(88,88,85)
X2003 <- c(75,81,83)

# Make data frame with variables:
df <- data.frame(days,
           X2000,
           X2001,
           X2002,
           X2003)

At this point you should have the same data frame as what you have above:此时,您应该拥有与上面相同的数据框:

    Xdays X2000 X2001 X2002 X2003
1 June-01    90    85    88    75
2 June-02    93    84    88    81
3 June-03    94    83    85    83

From here you just need to change the names:从这里您只需要更改名称:

# Change names:
names(df) <- sub("X", "", names(df))

# Print:
df

This should print this new table:这应该打印这个新表:

     days 2000 2001 2002 2003
1 June-01   90   85   88   75
2 June-02   93   84   88   81
3 June-03   94   83   85   83

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R strip在数据帧中分割一列 - R strip split a column in dataframe 有没有一种更有效的内存使用方式,可以使用combn从R中的其他每一列中减去每一列? - Is there a more memory-efficient way to use combn to subtract every column from every other column in R? 有没有更有效的方法来处理在 R 数据帧中重复的事实? - Is there a more efficient way to handle facts which are duplicating in an R dataframe? 寻找一种比在R中循环更有效的方法来创建数据帧 - looking for a more efficient way to create a dataframe than looping in R 根据R数据帧中其他列的值缩放列的有效方法 - Efficient way of scaling column based on value in other column in R dataframe 以有效(矢量化)方式用整数替换R中的列中的字符串(值) - Replacing character strings (values) in a column in R with integers in an efficient (vectorized) way R中的有效方法是向具有庞大数据集的数据框添加新列 - Efficient way in R to add a new column to a dataframe with huge dataset R:一种有效的方式来对一列中的值进行排序/选择,这些值对应于同一数据帧中另一列中的特定值 - R: Efficient way to sort/select values from one column that correspond to specific values from a different column within the same dataframe 有没有使用dplyr过滤器从数据帧中删除行的更有效的方法? - Is there a more efficient way of using dplyr filter to remove rows from a dataframe? R循环-有没有更有效的方法? - R loops - is there a more efficient way?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM