Re: [R] Using if, else statements

From: milton ruser <milton.ruser_at_gmail.com>
Date: Wed, 23 Jul 2008 14:43:45 -0300

Just to clarify,

if you have two data.frame, one with your data, other with data-admissions, just use

data.merge<-merge(my.df, data.weigth, by.x="data", by.y="data", all=T)

miltinho

On 7/23/08, milton ruser <milton.ruser_at_gmail.com> wrote:
>
> Hi Rob Williams
>
> I think it is one way of you do the job.
> Cheers,
>
> miltinho astronauta
> brazil
>
>
>
>
> my.df<-data.frame(cbind(data=sample(c("mon","sat","sun"), 20, replace=T),
> values=rnorm(20)))
> my.df$values<-as.numeric(my.df$values)
>
>
> data.weigth<-read.table(stdin(), head=T, sep=",")
> data,weigth
> mon,0.91
> sat,1.21
> sun,1.22
>
>
> data.weigth
>
>
> data.merge<-merge(my.df, data.weigth, by.x="data", by.y="data", all=T)
> data.merge$values.weigth<-data.merge$values*data.merge$weigth
>
> data.merge
>
>
>
> On 7/23/08, Robin Williams <robster3_at_hotmail.com> wrote:
>>
>> Hi all,
>>
>> Again I have searched the net and so on, without finding an answer to this
>> surely simple problem. A short bit of code would be appreciated.
>>
>> I have a object named `data' with the following column headings.
>>
>> Date, maxitemp, minitemp, admissions, d.o.w.
>>
>> Where d.o.w. is day of the week, written "Sun" "Mon" etc.
>>
>> I just need to scale the Monday admissions by 0.91, the Saturday
>> admissions by 1.21 and the Sunday admissions by 1.22. So basically what I
>> want is:
>>
>> If d.o.w. == "Sat"
>>
>> Multiply Sat admissions by 1.21.
>>
>> (Now do I need an else statement here, or can I just do another)
>>
>> If d.o.w. == "Sun"
>>
>> Multiply "Sun" admissions by 1.22
>>
>> (and finally)
>>
>> If d.o.w. == "Mon"
>>
>> Multiply Monday admissions by 0.91.
>>
>> Else do nothing.
>>
>> I assume in my code I need to specify that I am using the data.frame
>> `data', so do I need to write things like
>>
>> If(data[d.o.w.]=="Mon")
>>
>> I would then like to round the new admissions to integers (I assume I just
>> use round(data$admissions)), and output the new data to another csv file.
>>
>> I could of course do this in excel but I will need to extend this type of
>> idea in the future so I would like to master it in R.
>>
>> Many thanks for any help/code.
>>
>> Robin Williams.
>>
>>
>> [[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<http://www.r-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>

        [[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. Received on Wed 23 Jul 2008 - 18:05:39 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 23 Jul 2008 - 18:32:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive