[R] create multiple variables loop

Joshua Wiley jwiley.psych at gmail.com
Sun Jul 17 09:55:55 CEST 2011


Hi Carlos,

Assuming your actual dataset has as nice a structure as your example
one, it is extremely easy:


## Your data (in convenient form using dput(df))
df <- structure(list(male1990 = c(10L, 23L, 32L), male1995 = c(11L,
22L, 33L), male2000 = c(15L, 25L, 37L), male2005 = c(17L, 28L,
36L), female1990 = c(15L, 25L, 31L), female1995 = c(14L, 22L,
37L), female2000 = c(17L, 27L, 37L), female2005 = c(20L, 30L,
35L)), .Names = c("male1990", "male1995", "male2000", "male2005",
"female1990", "female1995", "female2000", "female2005"), class =
"data.frame", row.names = c(NA,
-3L))

## the male column indices
i <- 1:4

## calculate the ratio
## (note the column names are probably not what you want)
df[, i]/df[, i + 4]

## another way (proportion of males)
df[, i]/(df[, i] + df[, i + 4])

If the structure is not quite so nice (for example there is not
information on both sexes for all years or the variables are not all
in order, etc.), you will need to do a bit more work to select the
proper column indices.  The key is to make sure the dimensions of the
data frames/matrices match, then you can just add or divide on all of
them at once to get your ratios or whatever you want.  No looping
required.

Hope this helps,

Josh


On Sat, Jul 16, 2011 at 9:58 PM, jour4life <jour4life at gmail.com> wrote:
> Hello all,
>
> I am trying to figure out how to construct several new variables without
> having to write a code for each one. Specifically, I want to calculate
> ratios for several variables. For instance, let's say I have:
>
> df
>
> male1990 male1995 male2000 male2005 female1990 female1995 female2000
> female2005
> 10              11             15             17             15
> 14               17              20
> 23              22             25             28             25
> 22               27              30
> 32              33             37             36             31
> 37               37              35
>
> I want to calculate sex ratios for each of the years. Please note that this
> is an example. I want to calculate age-specific ratios for 5-year age
> groups, meaning that this will create a lot of variables. I am not well
> versed with looping functions and would appreciate it if someone provided
> some information and guidance as how to approach this.
>
> Thanks very much!
>
> Carlos
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/create-multiple-variables-loop-tp3672921p3672921.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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list