替换“名称”在R中的另一个文件中具有不同(新)名称的数据框的列 [英] Replace "names" of columns of a data frame with different (new) names in another file in R
本文介绍了替换“名称”在R中的另一个文件中具有不同(新)名称的数据框的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
S.No A1 A2 A3 A4 A6
1 0 0 0 0 0
2 2 4 7 7 9
3 6 7 9 10 0
等等。
另一个(数据帧B)文件如下所示:
S.No old_names new_names
1 A1 qq
2 A2 ww
3 A3 gg
4 A4 zz
5 A6 mm
A的名称不需要与B $ old_names相同的顺序。
我的新文件应如下所示:
S.No qq ww gg zz mm
有没有更简单的方法来做到这一点n不使用循环并比较两个文件?
1 0 0 0 0 0
2 2 4 7 7 9
3 6 7 9 10 0
任何帮助将不胜感激。
这两个文件都太大了。解决方案
n < df1)[ - 1]#摆脱S.No
名称(df1)< - c(S.No,as.character(df2 $ new_names)[match(n,df2 $ old_names)])
I have two data frames: One (dataframe A) is like as follows:
S.No A1 A2 A3 A4 A6 1 0 0 0 0 0 2 2 4 7 7 9 3 6 7 9 10 0
and so on.
Another (dataframe B) file is like as follows:
S.No old_names new_names 1 A1 qq 2 A2 ww 3 A3 gg 4 A4 zz 5 A6 mm
Names of A need not be in same sequence as of B$old_names.
My new file should look like:
S.No qq ww gg zz mm 1 0 0 0 0 0 2 2 4 7 7 9 3 6 7 9 10 0
IS there any simpler way to do this in R without using for loop and comparing both files?
Any help would be greatly appreciated. Both files are too big.
解决方案n <- names(df1)[-1] # get rid of S.No names(df1) <- c("S.No", as.character(df2$new_names)[match(n, df2$old_names)])
这篇关于替换“名称”在R中的另一个文件中具有不同(新)名称的数据框的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文