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.
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 10 months ago.
I have 2 data frames.
Data1 is like this (with length of 50)
|Depth|Age|
|1|56|
|2|78|
|3|104|
|4|157|
|5|200|
Data 2 is like this (with lenght of 300)
|Age|Rate|
|1|15.2|
|2|15.4|
|3|15.6|
|4|16.4|
|5|17.8|
|..|..|
|300|19.1|
I want to create a new column Rate in Data1 based on Age, and the value of Rate is from Data2 with corresponding Age (same age in Data1 and Data2).
I want the output to be like this:
|Depth|Age|Rate|
|1|56|18.1|
|2|78|20.1|
|3|104|21.0|
|4|157|20.2|
|5|200|23.1|
I tried if else and it said cannot work because the data frame's length is different.
Is there any way to do this?
Found it using match:
data1$rate <- data2$rate[match(data1$age,data2$age)]
I found the solution here
Add column to data frame based on values of another column in another row
This question already has answers here:
Subset rows in a data frame based on a vector of values
(4 answers)
How to join (merge) data frames (inner, outer, left, right)
(13 answers)
Closed 1 year ago.
I have a data frame id_df of ids:
id
1434903254
3940505900
5902309590
...
and another data frame df with ids and some other fields:
id field1 field2
3905094505 390300 929300
9503205909 030932 023039
3950259005 032030 023090
...
I want to filter df to have only rows with the ids from the id_df.
We can use
subset(df1, id %in% df2$id)
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)
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))