yokota
yokota

Reputation: 1017

Replace value using index [R]

I am trying to use an index to identify which rows to create a new column based on a join of another DF. I am not able to do the match() when using an index.

df1 <- data.frame(A=c("A", "C", "E"), B=c(1, 3, 5))
df2 <- data.frame(A=c("A", "C", "E"), C=c("Z", "X", "V"))

idx <- c(1, 3)

df1[match(df1$A, df2$A), "D"] <- df2$C
> df1
  A B D
1 A 1 Z
2 C 3 X
3 E 5 V

df1[idx,][match(df1$A, df2$A), "D"] <- df2$C
Warning messages:
  1: In `[<-.data.frame`(`*tmp*`, idx, , value = list(A = c(1L, 3L, NA : replacement element 1 has 3 rows to replace 2 rows

# what I would like the outcome to look like
> df1
  A B D
1 A 1 Z
2 C 3 NA
3 E 5 V

update: akrun gave me the idea to first throw in a new column. I get what I want now, but with warnings.

df1$D <- NA
df1[idx,][match(df1$A, df2$A), "D"] <- df2$C

Warning messages:
1: In `[<-.data.frame`(`*tmp*`, idx, , value = list(A = c(1L, 3L, NA :
  replacement element 1 has 3 rows to replace 2 rows
2: In `[<-.data.frame`(`*tmp*`, idx, , value = list(A = c(1L, 3L, NA :
  replacement element 2 has 3 rows to replace 2 rows
3: In `[<-.data.frame`(`*tmp*`, idx, , value = list(A = c(1L, 3L, NA :
  replacement element 3 has 3 rows to replace 2 rows
> df1
  A B  D
1 A 1  3
2 C 3 NA
3 E 5  2

Any solution to do this without warnings?

Upvotes: 1

Views: 5611

Answers (1)

akrun
akrun

Reputation: 887691

May be we can try

df1[intersect(idx,match(df1$A, df2$A)), "D"] <- as.character(df2$C[idx])
df1
#  A B    D
#1 A 1    Z
#2 C 3 <NA>
#3 E 5    V

Or a slightly more concise option (based on the example) would be

df1[match(df1$A, df2$A)[idx], "D"] <- as.character(df2$C[idx])

Upvotes: 2

Related Questions