Hi,
Please show a reproducible example.
countrydiff - c(Albania, Algeria, Belarus, Canada, Germany)
long_df - data.frame(country_name = c(Algeria, Guyana, Hungary,
Algeria, Canada, Iran, Iran, Norway,Uruguay, Zimbabwe) )
ifelse(long_df$country_name %in% countrydiff,1,0)
# [1] 1 0 0 1 1 0 0 0 0
Dear list-members,
I have the following problem: I have a vector (countrydiff) with length 72
and another vector (long_df$country_name) which is about 12000 long.
Basically what I want to do is to if the factor level (or string name) in
long_df$country_name appears on the countrydiff, then
Dear Arun
Thanks for your reply, it made me realize that the problem was not in the
code but in the levels() of the factors. Some countries had some extra
spacing which made the ifelse() function not work. So if I modify your code
(added space to countrydiff), it will then look something like
Hi Adel,
If the problem is the spacing, then
library(stringr)
1*(long_df$country_name %in% str_trim(countrydiff))
# [1] 1 0 0 1 1 0 0 0 0 0
A.K.
Dear Arun
Thanks for your reply, it made me realize that the problem was
not in the code but in the levels() of the factors. Some countries had
4 matches
Mail list logo