[R] If condition using accessors

Vitória Magalhães Piai V.Piai at psych.ru.nl
Fri Nov 27 15:48:46 CET 2009


Thanx.
I'll just post in here the way I solved it in case someone happens to 
need something similar in the future. It may not be the most elegant 
way, but it worked!

yes = "small"
no = "large"
data3$group = ifelse(data3$SIE < 50.64593,yes,no)

Peter Ehlers wrote:
>
> Peter Ehlers wrote:
>> Vitória Magalhães Piai wrote:
>>> Hi,
>>>
>>> I'm quite new using R and have got no one to help me get through it. 
>>> Hopefully someone can help me with one problem I've been struggling 
>>> with for the last hours!!
>>> (Sorry if I'm using the wrong terminology as well!)
>>>
>>> I have a data matrix in which SIE is one of my variables. What I 
>>> need to do now is to create a new variable (group) if a certain 
>>> condition in SIE is met.
>>> I tried:
>>>
>>> if (data2$SIE < 50.646){
>>> data2$group=as.factor("small")
>>> }
>>> which gives me "In if (data2$SIE < 50.646) { :  the condition has 
>>> length  > 1 and only the first element will be used"
>>>
>>> and
>>> ifelse (data2$SIE < 50.64593, data2$group = 
>>> as.factor("small"),data2$group = as.factor("large"))
>>> with "Error: unexpected '=' in "ifelse (data2$SIE < 50.64593, 
>>> data2$group ="
>>>
>>> I tried all other kinds of variations and so far, nothing!!
>>
>> Do all of the following:
>>
>> ?"="
>> ?"=="
>> ?"if"
>> ?ifelse
>>
>>  -Peter Ehlers
>
> Actually, I assumed without checking that ?ifelse would
> have an example of assignment of the result of the test.
> (Maybe it could be added.)
>
> yourVariable <- ifelse(...)
>
>  -Peter Ehlers
>
>>
>>>
>>> Is there a good way to do this that someone could teach me?
>>> Thank you so much,
>>> Vitória
>>>
>>> ______________________________________________
>>> 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.
>>>
>>>
>>
>> ______________________________________________
>> 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.
>>
>>




More information about the R-help mailing list