[R] For loop...

Berend Hasselman bhh at xs4all.nl
Sun Nov 4 13:24:50 CET 2012


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.




More information about the R-help mailing list