Yash Yash - 1 month ago 6
R Question

How to append two dataframe by overwriting the existing rows

I have a dataframe say df. I have extracted a sample 5% rows from df and created a new dataframe df1 to do few manipulations in the dataset. Now I need to append df1 to df and overwrite the existing rows of df1 as it is a subset of df.

I tried to extract the rows that are not present in df using

df2 <- subset(df, !(rownames(df) %in% rownames(df1[])))


But this didnt work.
Can anyone help please.

Answer

You should always try to make a reproducible example so that it is easy for others to help you

I have tried to do that with the help of mtcars dataset

#Copied mtcars data into df
df = mtcars
# sample 5 rows from df 
df1 = df[sample(1:nrow(df), 5), ]
# did few manipulations in the dataset
df1 = df1 * 2
# overwrite the existing rows of df1 as it is a subset of df
df[rownames(df1), ] <- df1
Comments