[R] for loop help

Michael Bedward michael.bedward at gmail.com
Wed Sep 15 05:23:31 CEST 2010


Hello,

So you want to sum Value by Name and assign to an object in your
workspace called Name ?  (it's best to say specifically what the
desired output is when you post a question here).

Assuming your data are in a matrix or data.frame called foo, you could
use a for loop as follows...

for (nm in unique(foo$Name)) assign(nm, sum(foo$Value[foo$Name == nm]))

You might also want to study the help pages for the functions tapply,
by, and aggregate.

Michael


On 15 September 2010 06:29, lord12 <gaut222 at yahoo.com> wrote:
>
> If you have:
> Name   Value
> A           2
> A           3
> A           4
> B           5
> B           6
> B            7
>
>
> In R how do you assign one value to the name:
>
> A:9
> B:18
> --
> View this message in context: http://r.789695.n4.nabble.com/for-loop-help-tp2539621p2539621.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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