Message-ID: <1347742489.24291.YahooMailNeo@web142602.mail.bf1.yahoo.com>
Date: 2012-09-15T20:54:49Z
From: arun
Subject: create new variable with ifelse? (reproducible example)
In-Reply-To: <CADWGO2zANM_UK8qf=JLZHRSqgtPC=NX+rU2kXx=1etw0uQvxRg@mail.gmail.com>
Hi,
Try this:
> str(rep_data)
#'data.frame':??? 6 obs. of? 4 variables:
# $ id1???? : Factor w/ 6 levels "100000016_a2",..: 1 2 3 4 5 6
# $ id2???? : Factor w/ 2 levels "fairly","very": 2 1 2 2 2 1
# $ know??? : Factor w/ 1 level "well": 1 1 1 1 1 1
# $ getalong: int? 4 NA 3 5 5 5
?rownames(rep_data)
#[1] "100000016_a1" "100000035_a1" "100000036_a1" "100000039_a1" "100000067_a1"
#[6] "100000076_a1"
?
rep_data$clo<-ifelse((rep_data$id2%in% c("very","fairly")) &(rep_data$getalong%in%c(4,5)),1,0)
?rep_data
#????????????????????? id1??? id2 know getalong clo
#100000016_a1 100000016_a2?? very well??????? 4?? 1
#100000035_a1 100000035_a2 fairly well?????? NA?? 0
#100000036_a1 100000036_a2?? very well??????? 3?? 0
#100000039_a1 100000039_a2?? very well??????? 5?? 1
#100000067_a1 100000067_a2?? very well??????? 5?? 1
#100000076_a1 100000076_a2 fairly well??????? 5?? 1
A.K.
----- Original Message -----
From: Niklas Fischer <niklasfischer980 at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Saturday, September 15, 2012 4:36 PM
Subject: [R] create new variable with ifelse? (reproducible example)
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.
rep_data
? ? ? ? ? ? ? ? ? ? ? id1? ? id2 know getalong clo
100000016_a1 100000016_a2? very well? ? ? ? 4? 0
100000035_a1 100000035_a2 fairly well? ? ? NA? 0
100000036_a1 100000036_a2? very well? ? ? ? 3? 0
100000039_a1 100000039_a2? very well? ? ? ? 5? 0
100000067_a1 100000067_a2? very well? ? ? ? 5? 0
100000076_a1 100000076_a2 fairly well? ? ? ? 5? 0
Any help is appreciated..
Bests,
Niklas
??? [[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.