1

So I have a large dataframe, let's call it df1. Now, I want to replace a subset of rows and columns with values from another dataframe which has exactly the same column names and ids. But the below code isn't producing the results I want and I don't understand why.

df1 <- data.frame(id = c(1:4), a = c("a", "a", "a", "a"), b = c("b", "b", "b", "b"), c = c("c", "c", "c", "c"), d = c("d", "d", "d", "d"))
df2 <- data.frame(id = c(2,3), b = c("x", "x"), c = c("y", "y"))

df1[df2$id, names(df2)] <- df2

I've looked at replace but don't know how to use it with two data frames. I'm looking for something like merge which replaces the values.

GreenManXY
  • 401
  • 1
  • 5
  • 14
  • 3
    When you create your 2 dataframes, add `data.frame(..,stringsAsFactors=F)` so that your columns are not factors. Otherwise, you wont be able to add values that are not part of the factor levels. – Lamia Jun 09 '17 at 17:16
  • 2
    `the below code isn't producing the results I want` this is not helpful. did you get an error or warning? what did it say, did you google it? I get a warning and googling it points to this https://stackoverflow.com/questions/16819956/invalid-factor-level-na-generated – rawr Jun 09 '17 at 17:17
  • The problem is that I'm not getting any errors, it does the replacement but it's incorrect and I don't know how it mixes up the values. I only see that a value on say b at row 2,3 in df1 isn't ("x", "x"). – GreenManXY Jun 09 '17 at 17:23

1 Answers1

2

You have different factor levels in the two data frames. This should work for you:

df1 <- data.frame(id = c(1:4), a = c("a", "a", "a", "a"), b = c("b", "b", "b", "b"), c = c("c", "c", "c", "c"), d = c("d", "d", "d", "d"), stringsAsFactors = FALSE)
df2 <- data.frame(id = c(2,3), b = c("x", "x"), c = c("y", "y"), stringsAsFactors = FALSE)

df1[df2$id, names(df2)] <- df2
ssp3nc3r
  • 3,662
  • 2
  • 13
  • 23