Copy column from one data.frame to another based on index

TheUndecided

The problem is similar to what posted in Combine dataframe based on index R

I am trying to copy one column from df2 (huge df) to df1 (small df) but based on index. In python it would be:

df1= df1[df.index.isin(df2.index)]
df1['columnx'] = df2['columny']

df1$name <- 0
df1$name[df1$df1['columnx'] == df2$['columny'] <- df2$name

I tried to replace the "name" column in df1 with the values of df2 "name" column corresponding to common index (columnx \ columny as index col) but have failed. I also tried to find a common index

df2.index <- intersect(df1$columnx, df2$columny)

so df2 will have a small index as in df1 and then to copy the column from df2 to df1, but it doesn't work

EDIT:

DF1

   columnx   |     col.1     |    col.2   |...
       a     |      12345    |   etc.     |...
       b     |               |            |

DF2

columny   | col.1  | col.2  | name|
a         | 123    | 1234   |abc  |
b         |        |        |def  |
c         |        |        |ghi  | 
d         |        |        |     |
..

Result needed:

DF combined:

   columnx   |     col.1     |    col.2   |name
       a     |      12345    |   etc.     |abc  |
       b     |               |            |def  |
Darren Tsai

You can simply use match():

df1$name <- df2$name[match(df1$columnx, df2$columny)]

or merge the both data by common columns:

dplyr::left_join(df1, df2[c("columny", "name")], by = c("columnx" = "columny"))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Copy value from one dataframe to another based on multiple column index

Moving index from one column to another in pandas data frame

How do I copy the value of columns in one data frame to another data frame based on column names?

creating a new data frame by extracting columns from one data frame based on the value of column in another data frame

Copy data from one worksheet to another based on column

Pandas: replace values of one data frame with values of another data frame based on index and column

Copy data from one column to another column

Copy data from one column into another column

Subtract one column from another in data frame

Add a column to one data frame based on multiple values in another by index number

SQL copy data from one column to another

Insert values from one data frame into another based on values of one column in R

SQL query to copy column from one table to another table based off index value

Copy value from one column based on the value of another column

Copying column from one data frame to another based on matching of combination of two columns

Replace strings in one column based on two columns from another data frame

Based on Partial string Match fill one data frame column from another dataframe

R: Multiplying One Column to the Rest of the Data Frame Based on Index/Position

Copy data from one excel sheet to another (complex) using VBA based on column name

How to take data from one dataframe and copy it into existing columns in another dataframe based on the shared ID of a third column

R Assign (or copy) column classes from a data frame to another

Create a new column from different columns of one data frame conditioned on another column from another data frame

Divide one column of data frame by condition from another column

SQL - copy data from one table column to another table column

Copy column value from one dataframe to another based on id in Pandas

Copy value from one column to another based on condition (using pandas)

Take a variable from a data frame row based on index found on a column

How to filter based on multiple column values from another data frame?

R: Update column based on matching rows from another data frame