Skip to content
Back to formatted view

Raw Message

Message-ID: <1371067417.95095.YahooMailNeo@web142605.mail.bf1.yahoo.com>
Date: 2013-06-12T20:03:37Z
From: arun
Subject: Creating a new var from conditional values in other vars

Hi,
Try this:
set.seed(25)
dat1<- data.frame(A=sample(1:30,100,replace=TRUE),B=sample(1:35,100,replace=TRUE),C=sample(1:25,100,replace=TRUE))


dat1$pattern<-with(dat1,ifelse(A>20 & B<=2.5 & C<=20,"Normal",ifelse(A <20 & B >2.5 & C >20,"Increased",ifelse(A >=20 & B >2.5 & C <=20,"Low","Other"))))
head(dat1)
#?? A? B? C pattern
#1 13 20? 5?? Other
#2 21 15? 4???? Low
#3? 5 24? 1?? Other
#4 27 13? 6???? Low
#5? 4? 4 10?? Other
#6 30 19 22?? Other
A.K.




I am newbie to R coming from SAS (basic user). Quite a difficult move.... 
I have a dataframe named kappa exported from csv file 
kappa<-read.csv("kappacsv", header=TRUE, sep=";", dec=".") 
kappa contains 3 numeric vars (A,B and C) 
I want to create a new var called "pattern" depending on the values of the conditions in A, B and C 
I have tried many ways one has been this: 
kappa$pattern1<-99 # create a new numeric var in kappa dataframe 

if (A>=20 & B<=2.5 & C <=20){kappa$pattern<-"Normal"} 
else if (A <20 & B >2.5 & C >20){kappa$pattern<-"Increased"} 
else if (A >=20 & B >2.5 & C <=20){kappa$pattern<-"Low"} 
else {kappa$pattern<-?Other?} 
? 
The code does not work and I get errors all the time. 
Any help will be greatly appreciated 
Thanks