i have a dataframe simple df1
and another dataframe simple df2
.
df1 <- data.frame(a= c(1:3),
b = c(4:6),
c = c(7:9),
d = c("aa", "aa", "aa"))
df2 <- data.frame(a = c("aa","bb", "cc"),
d = c("x", "y", "z"))
now i want get this exit:
a b c d f
1 1 4 7 aa x
2 2 5 8 aa x
3 3 6 9 aa x
how can use tidyverse library for do it?
df1 <- data.frame(a= c(1:3),
b = c(4:6),
c = c(7:9))
df2 <- data.frame(d = c("aa","bb", "cc"),
e = c("x", "y", "z"))
tidyr::expand_grid(df1,df2)
You don't need tidyverse,
Just use the R base command cbind()
df3 <- cbind(df1, df2)
in the function expand_grid()
the column's name of two datafarame not be the same and in my dataframe column a
is same.
rename your columns, or use name repair
tidyr::expand_grid(df1,df2,.name_repair = "universal")
i change the exit and i wnat to get this exit
a b c d f
1 1 4 7 aa x
2 2 5 8 aa x
3 3 6 9 aa x
on of column is same name and same variable .but variable in one of dataframe is on and another dataframe is three repate.(aa)
what are you asking ? how to rename a variable ?
not rename the variable and i correction the exit.
It wasnt clear from your message, but I see you edited your original post to ask for a different output.
df1 <- data.frame(a= c(1:3),
b = c(4:6),
c = c(7:9),
d = c("aa", "aa", "aa"))
df2 <- data.frame(a = c("aa","bb", "cc"),
d = c("x", "y", "z"))
dplyr::left_join(df1,
df2
,by=c("d"="a")) %>%
dplyr::rename(f=d.y)
a b c d f
1 1 4 7 aa x
2 2 5 8 aa x
3 3 6 9 aa x
1 Like
system
Closed
10
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.