[R] ifelse statement with two vectors of different length

Sarah Goslee sarah.goslee at gmail.com
Wed Dec 18 16:04:30 CET 2013


Hi,

Suggestion 1: read
http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
and bookmark it for future reference.

Suggestion 2:
set.seed(123)
countrydiff <- letters[1:5]
long_df <- data.frame(country_name = sample(letters[1:8], 20, replace=TRUE))

long_df$povdat <- as.numeric(long_df$country_name %in% countrydiff)

Sarah

On Wed, Dec 18, 2013 at 8:57 AM, Adel <adel.daoud at sociology.gu.se> wrote:
>
> 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 long_df$povdat should
> be equal to 1, if it does not appear on the countrydiff vector then
> long_df$povdat should be equal to zero. I have tried different combinations
> and read some. The following code should in my mind do it, but it doesn’t:
>
> long_df$povdat<-ifelse(long_df$country_name == countrydiff, 1, 0)
>
> long_df$povdat<-ifelse(long_df$country_name %in% countrydiff, 1, 0)
>
> Additional information: the factor vector countrydiff contains unique
> country names (Albania, Zimbabwe etc.), whereas long_df$country_name also
> contains country names albeit not unique since it is in longform. The unique
> names that appear in long_df$country_name is around 200.
>
>
> Any suggestions?
> Thanks in advance.
>
> Best
> Adel
>
>

-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list