Message-ID: <1347743446.24756.63.camel@milan>
Date: 2012-09-15T21:10:46Z
From: Milan Bouchet-Valat
Subject: create new variable with ifelse? (reproducible example)
In-Reply-To: <CADWGO2zANM_UK8qf=JLZHRSqgtPC=NX+rU2kXx=1etw0uQvxRg@mail.gmail.com>
Le samedi 15 septembre 2012 ? 23:36 +0300, Niklas Fischer a ?crit :
> Dear R users,
>
> I have a reproducible data and try to create new variable "clo" is 1 if
> know variable is equal to "very well" or "fairly well" and getalong is 4 or
> 5
> otherwise it is 0.
>
> rep_data<- read.table(header=TRUE, text="
> id1 id2 know getalong
> 100000016_a1 100000016_a2 very well 4
> 100000035_a1 100000035_a2 fairly well NA
> 100000036_a1 100000036_a2 very well 3
> 100000039_a1 100000039_a2 very well 5
> 100000067_a1 100000067_a2 very well 5
> 100000076_a1 100000076_a2 fairly well 5
> ")
>
>
> rep_data$clo<- ifelse((rep_data$know==c("fairly well","very well") &
> rep_data$getalong==c(4,5)),1,0)
>
> For sure, something must be wrong, I couldn't find it out.
Try:
rep_data$clo <- ifelse(rep_data$know %in% c("fairly well", "very well") &
rep_data$getalong %in% c(4, 5),
1, 0)
(Not checked, because your data is not parsed correclty by read.table
because of the spaces in the levels. Please use dput() instead.)
My two cents