An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121104/8eb9752b/attachment.pl>
For loop...
6 messages · Berend Hasselman, Bert Gunter, SHAILLY MEHROTRA +1 more
On 04-11-2012, at 13:07, SHAILLY MEHROTRA wrote:
Dear R-users, I have dataset with column A and B with1000 values, For each of column C value (C = seq(1,1000,1), I want to repeat A and B values and calculate R = A+B*C for each row. I want to get output as A B C R 1 10 1 11 2 30 1 32 3 50 1 53 1000 1000 1 2000 1 10 2 21 2 30 2 62 3 50 2 103 1000 1000 2 3000 How can I do it using for loop?
You don't do that with a for loop. You can do it like this, assuming your dataset is a data.frame and is named "dat" dat["R"] <- dat["A"] + dat["B"]*dat["C"] dat[,"R"] <- dat[,"A"] + dat[,"B"]*dat[,"C"] Read the R intro manual. Berend
Thanks Shailly [[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.
But ...
On Sun, Nov 4, 2012 at 4:24 AM, Berend Hasselman <bhh at xs4all.nl> wrote:
On 04-11-2012, at 13:07, SHAILLY MEHROTRA wrote:
Dear R-users, I have dataset with column A and B with1000 values, For each of column C value (C = seq(1,1000,1), I want to repeat A and B values and calculate R = A+B*C for each row. I want to get output as A B C R 1 10 1 11 2 30 1 32 3 50 1 53 1000 1000 1 2000 1 10 2 21 2 30 2 62 3 50 2 103 1000 1000 2 3000 How can I do it using for loop?
You don't do that with a for loop. You can do it like this, assuming your dataset is a data.frame and is named "dat" dat["R"] <- dat["A"] + dat["B"]*dat["C"]
dat[["R"]] <- dat[["A"]] + dat[["B"]]*dat[["C"]] would be better, I believe. And the OP would be well-advised to read the Intro to R tutorial before posting further on this list. To quote Rolf Turner on this list: " Learn something about R; don't just hammer and hope. Read the introductory manuals and scan the FAQ.." -- Bert
dat[,"R"] <- dat[,"A"] + dat[,"B"]*dat[,"C"] Read the R intro manual. Berend
Thanks
Shailly
[[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.
______________________________________________ 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.
Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
On 04-11-2012, at 15:40, Bert Gunter wrote:
But ... On Sun, Nov 4, 2012 at 4:24 AM, Berend Hasselman <bhh at xs4all.nl> wrote:
On 04-11-2012, at 13:07, SHAILLY MEHROTRA wrote:
Dear R-users, I have dataset with column A and B with1000 values, For each of column C value (C = seq(1,1000,1), I want to repeat A and B values and calculate R = A+B*C for each row. I want to get output as A B C R 1 10 1 11 2 30 1 32 3 50 1 53 1000 1000 1 2000 1 10 2 21 2 30 2 62 3 50 2 103 1000 1000 2 3000 How can I do it using for loop?
You don't do that with a for loop. You can do it like this, assuming your dataset is a data.frame and is named "dat" dat["R"] <- dat["A"] + dat["B"]*dat["C"]
dat[["R"]] <- dat[["A"]] + dat[["B"]]*dat[["C"]] would be better, I believe.
Right. I insert the reply which was sent to me and not to the R-help list:
I think you misunderstood my question. In my dataset, I have only two columns.( A and B with 1000 values each). I generated a variable C using seq(1,4200,20). Now for each value of C, I want to calculate 1000 R values (using 1000 - A and 1000 - B values) Finally, I want to have a dataset in which for each C value , I have 1000 R values. So the final dataset should have 210*1000 rows. Hope it is clear now.
Well try this then: dat <- read.table(text=" A B 1 10 2 30 3 50 1000 1000 1 10 2 30 3 50 1000 1000", header=TRUE) dat[,"C"] <- rep(1:2,each=nrow(dat)/2,length.out=nrow(dat)) dat[,"R"] <- unlist(lapply(split(dat,dat$C), FUN=function(x) x$A+x$B*x$C)) dat Berend
And the OP would be well-advised to read the Intro to R tutorial before posting further on this list. To quote Rolf Turner on this list: " Learn something about R; don't just hammer and hope. Read the introductory manuals and scan the FAQ.." -- Bert
dat[,"R"] <- dat[,"A"] + dat[,"B"]*dat[,"C"] Read the R intro manual. Berend
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121104/aae9fb27/attachment.pl>
HI, Try this: dat1<-read.table(text=" A????????????? B??????????? C????? 1????????????? 10????????? 1?????? 2????????????? 30????????? 1?????? 3????????????? 50????????? 1?????? 1000????? 1000??????? 1??????? 1????????????? 10????????? 2?? 2????????????? 30????????? 2?? 3????????????? 50????????? 2?? 1000??????? 1000????? 2??????? ",sep="",header=TRUE) ?dat1$R<-apply(dat1,1,function(x) x[1]+x[2]*x[3]) ?dat1 #???? A??? B C??? R #1??? 1?? 10 1?? 11 #2??? 2?? 30 1?? 32 #3??? 3?? 50 1?? 53 #4 1000 1000 1 2000 #5??? 1?? 10 2?? 21 #6??? 2?? 30 2?? 62 #7??? 3?? 50 2? 103 #8 1000 1000 2 3000 A.K. ----- Original Message ----- From: SHAILLY MEHROTRA <shaillymehrotra at gmail.com> To: r-help at r-project.org Cc: Sent: Sunday, November 4, 2012 7:07 AM Subject: [R] For loop... Dear R-users, I have dataset? with column A and B with1000 values, For each of column C value (C = seq(1,1000,1), I want to repeat A and B values and calculate R = A+B*C for each row. I want to get output as A? ? ? ? ? ? ? B? ? ? ? ? ? C? ? ? ? R 1? ? ? ? ? ? ? 10? ? ? ? ? 1? ? ? ? 11 2? ? ? ? ? ? ? 30? ? ? ? ? 1? ? ? ? 32 3? ? ? ? ? ? ? 50? ? ? ? ? 1? ? ? ? 53 1000? ? ? 1000? ? ? ? 1? ? ? ? 2000 1? ? ? ? ? ? ? 10? ? ? ? ? 2? ? ? ? 21 2? ? ? ? ? ? ? 30? ? ? ? ? 2? ? ? ? 62 3? ? ? ? ? ? ? 50? ? ? ? ? 2? ? ? ? 103 1000? ? ? ? 1000? ? ? 2? ? ? ? 3000 How can I do it using for loop? Thanks Shailly ??? [[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.