This question already has answers here:
How to join (merge) data frames (inner, outer, left, right)
(13 answers)
Closed 4 years ago.
I would like to merge 2 dataframes and I have tried with the code below but it's not working,
merg <- merge(companies, rounds2,
by.companies = "permalink",
by.rounds2 = "company_permalink", all = TRUE)
One data frame has more than 1,00,000 rows and 8 columns and other dataframe has 60,000 + rows, 6 columns. Permalink is the unique key both dataframes but different column names. I m not sure how the file will look if merge 2 dataframes which have more and fewer rows. We need to merge as column wise.
In the by.x="" and in by.y="" you have to put the name of the permalink identifier. I do not know what this is since I do not have a data example. Regarding the join there are several options there as well for instance all.x=TRUE all.y=TRUE or all=TRUE or FALSE. These depend on how you want to join the dataframe.
companies=data.frame(companies=rnorm(100),other1=rnorm(100))
rounds2=data.frame(rounds2=rnorm(100),other1=rnorm(100))
companies
rounds2
merge(companies,rounds2,by.x="companies",by.y="rounds2",all=TRUE)
Related
This question already has answers here:
How to join (merge) data frames (inner, outer, left, right)
(13 answers)
Closed 6 months ago.
I want to create a new variable (Sex) in a dataframe by matching up the ID's between my main data frame and a reference data frame which contains information about the sex of each individual.
I have the following code that works - but as my data frame is over 6 million rows long it is taking over 10 hours to run.
data.df$Sex_Varb <- NA
for(i in 1:nrow(data.df)){
find.match <- which(data.df$ID_Varb[i] == Reference_Dat$ID_Varb)
if(length(find.match) != 0){
data.df$Sex_Varb[i] <- Reference_Dat$Sex[find.match]
}
}
Is there a faster way to create a new variable based on the matching values between two datasets?
Perhaps you are looking for a left join?
merge(data.df, Reference_dat[, c("ID_Varb", "Sex")], by="ID_Varb", all.x=TRUE)
This question already has answers here:
How to join (merge) data frames (inner, outer, left, right)
(13 answers)
Closed 2 years ago.
I have two tables 1 and 2 and I want to join the tables by a common column. Moreover, I want my data to be sorted so that another column of my first table to be in between two other columns of my second table.
It's difficult to answer properly because you haven't provided any sample data, but something along these lines should work:
library(dplyr)
left_join(table1, table2, by = c("commoncolumn") %>%
select(col2, col1, col3) #reorders columns to order listed
left_join() will join any rows from table2 to the matching rows of table1 but discard any rows in table2 with no matches. Depending on the behaviour you want, full_join() (keeps all rows from both tables) or inner_join() (only keeps rows which are in both tables) might work better for you.
This question already has answers here:
How to join (merge) data frames (inner, outer, left, right)
(13 answers)
r cbind a single column of a dataframe with another dataframe without changing the column name
(3 answers)
Closed 3 years ago.
Im trying to combine two data frames in R, both with 1 column, and have them added with the columns beside each other.
Let's say I have one one data frame called names.df and another called id.df, how do I combine them (name.df and name.df) so the the names are column1 and the ids are column2?
names <- c('John','Peter','Sally')
names.df <- data.frame(names)
id <- c('12632','82174','27036')
id.df <- data.frame(id)
Use cbind:
cbind(names.df, id.df)
names id
1 John 12632
2 Peter 82174
3 Sally 27036
This would generate a data frame output from the two inputs.
This question already has answers here:
How to join (merge) data frames (inner, outer, left, right)
(13 answers)
Closed 5 years ago.
I am using R. I have two tables, both with an ID variable. I want to make another table where I have a column for ID which only has IDs that appear in both original tables, plus a column for another variable that only appeared in one of the original tables.
Any help would be much appreciated.
Try this
library(dplyr)
df_temp <- inner_join(df_1, df_2, by=ID)
df_3 <- subset(df_temp, select = c(ID, OtherColumn))
This question already has answers here:
How to join (merge) data frames (inner, outer, left, right)
(13 answers)
Closed 8 years ago.
Lets say I have big DataFrame and smaller one, which is its subset. Big one has column Y, and smaller has not, both have colmun ID. How, based on equality of values in ID assign values from Y in Big DF to small DF?
If these are your data frames
BIG<-data.frame(ID=1:20, Y=runif(20))
SMALL<-data.frame(ID=c(3,7,12))
Then you can do the matching using match(). For example
SMALL$Y<-BIG$Y[match(SMALL$ID, BIG$ID)]