Re: [R] Using if, else statements
You would have something that looks like this: if (data$d.o.w == Sat) data$admission - round(data$admission * 1.21) if (data$d.o.w == Sun) ... On Wed, Jul 23, 2008 at 11:44 AM, Robin Williams [EMAIL PROTECTED] 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@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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? __ R-help@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.
Re: [R] Using if, else statements
My fingers slipped on the keyboard. Here what they intended to write. This sets up a list of the data and the matches on a subset for processing days - list(list(Sat, 1.21), list(Sun, 1.22), list(Mon, 0.91)) for (i in days){ .subset - data$d.o.w == i[[1]] # subset of data that matches data$d.o.w[.subset] - round(data$d.o.w[.subset] * i[[2]]) } write.csv(data, file='xyz') On Wed, Jul 23, 2008 at 11:44 AM, Robin Williams [EMAIL PROTECTED] 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@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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? __ R-help@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.
Re: [R] Using if, else statements
One more try and I quit: This is what happens if someone does not sent a sample of data you have to create things on the fly without testing. days - list(list(Sat, 1.21), list(Sun, 1.22), list(Mon, 0.91)) for (i in days){ .subset - data$d.o.w == i[[1]] # subset of data that matches data$admission[.subset] - round(data$admission[.subset] * i[[2]]) } write.csv(data, file='xyz') On Wed, Jul 23, 2008 at 11:44 AM, Robin Williams [EMAIL PROTECTED] 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@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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? __ R-help@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.
Re: [R] Using if, else statements
jjdow - sample(c('Sun', 'Mon', 'Tues'), 10, replace=TRUE) jjdata - data.frame(admission=1:10, d.o.w.=jjdow) # jjdata # admission d.o.w. #1 1 Tues #2 2 Tues #3 3 Tues #4 4Sun #5 5Mon #6 6Mon #7 7Sun #8 8 Tues #9 9Sun #1010Mon new.jjdata - within(jjdata, admission - ifelse(d.o.w. == 'Sun', 1.21 * admission, ifelse(d.o.w.=='Mon', .91 * admission, admission))) # new.jjdata # admission d.o.w. #1 1.00 Tues #2 2.00 Tues #3 3.00 Tues #4 4.84Sun #5 4.55Mon #6 5.46Mon #7 8.47Sun #8 8.00 Tues #9 10.89Sun #10 9.10Mon Pat Robin Williams wrote: No, this is my first experience with any powerful statistical package. Any code much appreciated. -Original Message- From: Patrick Burns [mailto:[EMAIL PROTECTED] Sent: 23 July 2008 17:44 To: Robin Williams Subject: Re: [R] Using if, else statements You might have found 'ifelse' in S Poetry, which is one way of solving your problem. Patrick Burns [EMAIL PROTECTED] +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and A Guide for the Unwilling S User) Robin Williams 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@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@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.
Re: [R] Using if, else statements
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 [EMAIL PROTECTED] 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 [EMAIL PROTECTED] 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@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.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@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.
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 [EMAIL PROTECTED] To: r-help@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@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. __ R-help@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.