0

I am using R and need a hint to solve my problem:

I have two lists and I want to compare the values of the first row of list "a" with the values of the first row of list "b". If the element exists, I want to write the value of the second row of list "b" into the second row of list "a".

So, here is list "a":

X.WORD    FREQ
abase     0
abased    0
abasing   0
abashs    0

here list "b"

V1        V2
arthur    11
abased    29
turtle    9
abash     2

The result should be

X.WORD    FREQ
abase     0
abased    29
abasing   0
abashs    0

Thanks for your answers

Roger Sánchez
  • 87
  • 1
  • 1
  • 8

2 Answers2

3

That's just a task for simple merge in base R

Res <- merge(a, b, by.x = "X.WORD", by.y = "V1", all.x = TRUE)[, -2]
Res$V2[is.na(Res$V2)] <- 0
Res
#    X.WORD V2
# 1   abase  0
# 2  abased 29
# 3  abashs  0
# 4 abasing  0

Data

a <- structure(list(X.WORD = structure(c(1L, 2L, 4L, 3L), .Label = c("abase", 
"abased", "abashs", "abasing"), class = "factor"), FREQ = c(0L, 
0L, 0L, 0L)), .Names = c("X.WORD", "FREQ"), class = "data.frame", row.names = c(NA, 
-4L))

b <- structure(list(V1 = structure(c(3L, 1L, 4L, 2L), .Label = c("abased", 
"abash", "arthur", "turtle"), class = "factor"), V2 = c(11L, 
29L, 9L, 2L)), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA, 
-4L))
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
1

Here is one approach.

library(dplyr)

ana <- foo %>%
    left_join(foo2, by = c("X.WORD" = "V1")) %>%
    select(-FREQ) %>%
    rename(FREQ = V2)

ana$FREQ[is.na(ana$FREQ)] <- 0

#   X.WORD FREQ
#1   abase    0
#2  abased   29
#3 abasing    0
#4  abashs    0

Data

foo <- structure(list(X.WORD = structure(c(1L, 2L, 4L, 3L), .Label = c("abase", 
"abased", "abashs", "abasing"), class = "factor"), FREQ = c(0L, 
0L, 0L, 0L)), .Names = c("X.WORD", "FREQ"), class = "data.frame", row.names = c(NA, 
-4L))

foo2 <- structure(list(V1 = structure(c(3L, 1L, 4L, 2L), .Label = c("abased", 
"abash", "arthur", "turtle"), class = "factor"), V2 = c(11L, 
29L, 9L, 2L)), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA, 
-4L))
jazzurro
  • 23,179
  • 35
  • 66
  • 76
  • I get the following error message: Error: cannot join on columns 'V1' x 'X.WORD' : index out of bounds – Roger Sánchez Nov 03 '14 at 15:52
  • @RogerSánchezNa Thanks for your message. I tested the code with the data. It is running on my machine. Could you double check it? – jazzurro Nov 03 '14 at 16:22