[R] Fw: Using if, else statements
Gabriela Cendoya
gcendoya at balcarce.inta.gov.ar
Wed Jul 23 21:24:29 CEST 2008
Sorry in the previos replay I forgot monday' scale
so weight should be:
weight <- c(0.91,1,1,1,1,1.21,1.22)
names(weight) <- c("mon","tue","wed","thu","fri","sat","sun")
----- Original Message -----
From: "Gabriela Cendoya" <gcendoya at balcarce.inta.gov.ar>
To: "Robin Williams" <>
Cc: <>
Sent: Wednesday, July 23, 2008 4:14 PM
Subject: Re: [R] Using if, else statements
> Hi Robin:
> I think you can avoid the loops doing this:
>
> my.df<-data.frame(d.o.w=sample(c("mon","sat","sun"), 20, replace=T),
> admissions=rnorm(20))
> weight <- c(1,1,1,1,1,1.21,1.22)
> names(weight) <- c("mon","tue","wed","thu","fri","sat","sun")
> my.df$NewAdm <- my.df$admissions * weight[as.character(my.df$d.o.w)]
>
> my.df
>
> Gabriela
>
>
>
> ----- Original Message -----
> From: "Robin Williams" <robster3 at hotmail.com>
> To: <r-help at r-project.org>
> Sent: Wednesday, July 23, 2008 12:44 PM
> Subject: [R] Using if, else statements
>
>
>> 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
>> and provide commented, minimal, self-contained, reproducible code.
>
___________________________________________________________________________
Aviso:
=====
El contenido del presente e-mail y sus posibles adjuntos pertenecen al INTA y pueden contener información confidencial. Si usted no es el destinatario original de este mensaje y por este medio pudo acceder a dicha información, por favor solicitamos contactar al remitente y eliminar el mensaje de inmediato. Se encuentra prohibida la divulgación, copia, distribución o cualquier otro uso de la información contenida en el presente e-mail por parte de personas distintas al destinatario.
This e-mail contents and its possible attachments belong to INTA and may contain confidential information. If this message was not originally addressed to you, but you have accessed to such information by this means, please contact the sender and eliminate this message immediately. Circulation, copy, distribution, or any other use of the information contained in this e-mail is not allowed on part of those different from the addressee.
Antes de imprimir este mensaje, asegúrese de que sea necesario. Proteger el medio ambiente está también en su mano.
More information about the R-help
mailing list