将data_frame中的行名从R中的字母更改为数字 [英] Changing row names in a data_frame from letters to numbers in R

查看:709
本文介绍了将data_frame中的行名从R中的字母更改为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组数据集,这些数据集来自应用于许多不同国家的调查,我希望将其合并以创建单个合并的data.frame.不幸的是,对于其中一个,变量名与其他变量名不同,但是遵循一种模式:就像在其他变量名中一样,变量名类似于"VAR1","VAR2"等,在此变量名是"VAR_a","VAR_b"等

I have a group of datasets, from a survey applied to many different countries, which I want to combine to create a single merged data.frame. Unfortunately, for one of them , the variable names is different from the others, but it follows a pattern: as in the others the names of the variables are like "VAR1", "VAR2", etc., in this one their names are "VAR_a", "VAR_b", etc.

到目前为止,我用于解决此问题的代码如下:

The code I've used so far to solve this problem is something like:

names (df) <- gsub("_a", "01", names(df)) 
names (df) <- gsub("_b", "02", names(df)) 
names (df) <- gsub("_c", "03", names(df)) 
names (df) <- gsub("_d", "04", names(df)) 
names (df) <- gsub("_e", "05", names(df)) 
names (df) <- gsub("_f", "06", names(df)) 
names (df) <- gsub("_g", "07", names(df)) 

直到第14个字母/数字(没有其他变量超出此范围),因此它可以变得与其他data.frames相似.

up to the 14th letter/ number (no variable goes further than that), so that it can become similar to the other data.frames.

我知道应该有一种方法可以用几行甚至一行代码来做到这一点,但是我找不到在gsub本身中执行迭代或任何参数的方法.谁能帮我吗?

I know there should be a way of doing that with a few or maybe even one single line of code, but I can't find a way to do an iteration or any argument inside gsub itself to do this. Can anyone help me?

我在想类似的东西:

names (df) <- gsub ("_[a-z]", "[1-9]", names(df))

但是,这当然没有用.我需要R才能理解我希望每个字母都变成对应的数字("_a"等于1,依此类推).

But this didn't work, of course. I need R to understand I want each letter to become the corresponding number ("_a" becomes 1, etc.)

感谢任何帮助.

推荐答案

如果您只想要版本化于模式和替换的gsub版本,则stringr有一个称为str_replace的版本.以下代码还在任何版本的R中使用letters.

If you just want a version of gsub that vertorises over pattern and replacement, stringr has one called str_replace. The below code also uses letters in any version of R.

library(stringr)
df <- data.frame(matrix(0, nrow = 5, ncol = 10))
colnames(df) <- paste0("abcd2345p_", letters[1:10])
colnames(df)

> [1] "abcd2345p_a" "abcd2345p_b" "abcd2345p_c" "abcd2345p_d" "abcd2345p_e"
[6] "abcd2345p_f" "abcd2345p_g" "abcd2345p_h" "abcd2345p_i" "abcd2345p_j"

str_replace(colnames(df), paste0("_", letters[1:ncol(df)], "$"), as.character(1:ncol(df)))

>  [1] "abcd2345p1"  "abcd2345p2"  "abcd2345p3"  "abcd2345p4"  "abcd2345p5" 
[6] "abcd2345p6"  "abcd2345p7"  "abcd2345p8"  "abcd2345p9"  "abcd2345p10"

这篇关于将data_frame中的行名从R中的字母更改为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆