Adding Value from One Data.Frame to Another Data.Frame by Matching a Variable

Adding value from one data.frame to another data.frame by matching a variable

df1 <- data.frame(Id=c(10L,10L,10L,11L,11L,12L,12L),Price=c(5L,5L,5L,7L,7L,0L,5L),Profit=c(2L,3L,2L,3L,1L,0L,1L),Month=c(1L,2L,3L,1L,2L,1L,2L),stringsAsFactors=F);
df2 <- data.frame(Id=c(9L,10L,10L,11L,12L,13L,14L),Name=c('Kane','Jack','Jack','Will','Matt','Lee','Han'),stringsAsFactors=F);
df1$Name <- df2$Name[match(df1$Id,df2$Id)];
df1;
## Id Price Profit Month Name
## 1 10 5 2 1 Jack
## 2 10 5 3 2 Jack
## 3 10 5 2 3 Jack
## 4 11 7 3 1 Will
## 5 11 7 1 2 Will
## 6 12 0 0 1 Matt
## 7 12 5 1 2 Matt

R: add value from another data frame by finding same values in two data frames

use merge() from the base package

merge(df1, df2, by = 'Code', all.x=T, all.y=F)

Adding value from one pandas dataframe to another dataframe by matching a variable

One easy way you can do is to use the merge of pandas based on similar column.

df2.drop('c1', axis=1, inplace=True)


main_df = pd.merge(df2, df, on="c2", how="left")


df2['c1'] = main_df['c1']


df2.columns = ['c1','c2','c3','c4']

How to add variable from one dataframe to another dataframe (several conditions)

First, change the EUETS column names and sector as you want the to show up in the end:

names(EUETS)[3] = "EUETS"
EUETS$sector = "Regulated"

Make sure your original sector column is a character, not a factor:

newdata$sector = as.character(newdata$sector)

Merge the data

result = merge(newdata, EUETS, all = TRUE)

For adding unrepresented countries back into EUETS, I'm not sure what year and emissions values you want to add in, so I'll ignore that for now. But basically you want to use merge again.

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

You can use match :

df2$BANK_CODE <- df1$BANK_CODE[match(df2$BANK_NAME, df1$BANK_NAME)]

Creating new variable in dataframe based on matching values from other dataframe

I think this does what you want:

df1$z <- df2$b[match(df1$x,df2$a)]
df1$z[df1$x=='G']=NA

Output:

> df1
x z
1 A 1
2 <NA> NA
3 L NA
4 G 7
5 C 3
6 F 6
7 <NA> NA
8 J 10
9 G 7
10 K NA

Hope this helps!

Adding values from one dataframe to another based on two matching conditions in R

In one of the datasets, the 'Month' is abbreviated and in the second it is full name. We can adjust to either one of those formats and the merge would work

df2$MonthN <- month.abb[match(df2$Month, month.name)]
library(dplyr)
left_join(df1, df2[, c("MonthN", "Building", "Population")],
by = c('Month' = 'MonthN', 'Building'))

Or with merge

merge(df1, df2[, c("MonthN", "Building", "Population")], 
by.x = c('Month', 'Building'), by.y = c('MonthN', 'Building'), all.x = TRUE)

Note: The Population column on the merged dataset will be NA based on the example as "Building" values are different in the subset datasets

R: How do I add a column to a dataframe based on paired values in another dataframe with different length?

Using left_join from dplyr package:

library(dplyr)

data <- left_join(df1,df2, by="id")

Add a column by matching id from another dataframe

The cases of both the data is not same, one is upper-case and another one is lower case. R is case-sensitive, so either change one of the dataframe and then do merge.

df1$Provinsi <- toupper(df1$Provinsi)
result <- merge(df1, df2)

assign values of one dataframe column to another dataframe column based on condition

df1['per']=df1['date'].map(dict(zip(df2['Date'], df2['percent']))).fillna(0)



date value1 value2 per
0 4/1/2021 A 1 0.1
1 4/2/2021 B 2 0.2
2 4/6/2021 C 3 0.6
3 4/4/2021 D 4 0.0
4 4/5/2021 E 5 0.0
5 4/6/2021 F 6 0.6
6 4/2/2021 G 7 0.2


Related Topics



Leave a reply



Submit