Reputation: 87
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
Upvotes: 0
Views: 186
Reputation: 92310
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))
Upvotes: 3
Reputation: 23574
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))
Upvotes: 1