[英]Avoiding and renaming .x and .y columns when merging or joining in r
Often I go about joining two dataframes together that have the same name.我经常 go 关于将两个具有相同名称的数据帧连接在一起。 Is there a way to do this within the join-step so that I don't end up with ax and ay column?
有没有办法在连接步骤中执行此操作,这样我就不会得到 ax 和 ay 列? So the names might be 'original_mpg', and 'new_mpg'?
所以名称可能是“original_mpg”和“new_mpg”?
library(dplyr)
left_join(mtcars, mtcars[,c("mpg",'cyl')], by=c("cyl"))
names(mtcars) #ugh
Currently, this is an open issue with dplyr . 目前,这是dplyr的一个未解决的问题 。 You'll either have to
rename
before or after the join or use merge
from base R, which takes a suffixes
argument. 您必须在连接之前或之后
rename
,或者使用基本R的merge
,后者采用suffixes
参数。
The default suffixes, c(".x", ".y")
, can be overridden by passing them as a character vector of length 2: 默认后缀
c(".x", ".y")
可以通过将它们作为长度为2的字符向量传递来覆盖:
library(dplyr)
left_join(mtcars, mtcars[,c("mpg","cyl")],
by = c("cyl"),
suffix = c("_original", "_new")) %>%
head()
Output 产量
mpg_original cyl disp hp drat wt qsec vs am gear carb mpg_new
1 21 6 160 110 3.9 2.62 16.46 0 1 4 4 21.0
2 21 6 160 110 3.9 2.62 16.46 0 1 4 4 21.0
3 21 6 160 110 3.9 2.62 16.46 0 1 4 4 21.4
4 21 6 160 110 3.9 2.62 16.46 0 1 4 4 18.1
5 21 6 160 110 3.9 2.62 16.46 0 1 4 4 19.2
6 21 6 160 110 3.9 2.62 16.46 0 1 4 4 17.8
You can use suffix with a slightly modified function I found in the help of strsplit to make it a prefix您可以将后缀与我在 strsplit 的帮助下找到的略微修改的 function 一起使用,使其成为前缀
library(dplyr)
mt_cars <- left_join(mtcars, mtcars[,c("mpg","cyl")],
by = c("cyl"),
suffix = c("_original", "_new"))
strReverse <- function(x){
sapply(lapply(strsplit(x, "_"), rev), paste, collapse = "_")
}
colnames(mt_cars) <- strReverse(colnames(mt_cars))
Well, I had a similar question when I found this post.好吧,当我找到这篇文章时,我也有类似的问题。 I found a different solution to the question that I hope helps.
我找到了一个不同的解决方案,希望对我有所帮助。
The solution is actually fairly simple, you generate a list with all the data frames you want to merge and use the reduce function.解决方案实际上相当简单,您生成一个包含所有要合并的数据框的列表,然后使用 reduce function。
library(dplyr)
df_list <- list(df1, df2, df3)
df <- Reduce(function(x, y) merge(x, y, all=TRUE), df_list)
This was a solution to another problem I had, I wanted to simplify merging multiple dataframes.这是我遇到的另一个问题的解决方案,我想简化合并多个数据帧的过程。 But if you use two dataframes in the list, it works all the same and merging does not rename the columns.
但是,如果您在列表中使用两个数据框,它的工作原理是一样的,并且合并不会重命名列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.