[R] creating mulptiple new variables from one data.frame according to columns and rows in that frame
jim holtman
jholtman at gmail.com
Wed Nov 4 03:12:13 CET 2009
try this:
> x <- read.table(textConnection(" Age(yrs) country mu sigma
+ 1 0.00000000 Bolivia 11.42168 0.1014872
+ 2 0.08333333 Bolivia 11.33625 0.1053837
+ 3 0.16666667 Bolivia 11.28417 0.1070594
+ 4 0.25000000 Bolivia 11.21125 0.1083872
+ 5 0.33333333 Bolivia 11.11637 0.1095305
+ 5.1 5 Bolivia 11.11637 0.1095305
+ 5.2 5.5 Bolivia 11.11637 0.1095305
+ 5.3 6 Bolivia 11.11637 0.1095305
+ 5.4 20 Bolivia 11.11637 0.1095305
+ 5.5 20.1 Bolivia 11.11637 0.1095305
+ 5.6 50 Bolivia 11.11637 0.1095305
+ 602 0.00000000 Brazil 11.54888 0.10839417
+ 603 0.08333333 Brazil 11.46345 0.11255592
+ 604 0.16666667 Brazil 11.41137 0.11434565
+ 605 0.25000000 Brazil 11.33844 0.11576378
+ 606 0.33333333 Brazil 11.24357 0.11698489"), header=TRUE)
> closeAllConnections()
> result <- lapply(split(x, x$country), function(.ctry){
+ # keep all < 5 and only integers over 5
+ subset(.ctry, .ctry$Age.yrs. < 5 | .ctry$Age.yrs. %in% 5:50)
+ })
>
> result
$Bolivia
Age.yrs. country mu sigma
1 0.00000000 Bolivia 11.42168 0.1014872
2 0.08333333 Bolivia 11.33625 0.1053837
3 0.16666667 Bolivia 11.28417 0.1070594
4 0.25000000 Bolivia 11.21125 0.1083872
5 0.33333333 Bolivia 11.11637 0.1095305
5.1 5.00000000 Bolivia 11.11637 0.1095305
5.3 6.00000000 Bolivia 11.11637 0.1095305
5.4 20.00000000 Bolivia 11.11637 0.1095305
5.6 50.00000000 Bolivia 11.11637 0.1095305
$Brazil
Age.yrs. country mu sigma
602 0.00000000 Brazil 11.54888 0.1083942
603 0.08333333 Brazil 11.46345 0.1125559
604 0.16666667 Brazil 11.41137 0.1143456
605 0.25000000 Brazil 11.33844 0.1157638
606 0.33333333 Brazil 11.24357 0.1169849
On Tue, Nov 3, 2009 at 9:31 AM, Hayes, Daniel <D.J.Hayes at liverpool.ac.uk> wrote:
> Dear R-helpers,
>
> I have a data.frame (bcpe.lat.m) containing 13 countries, ages 0-50yrs per month, and the corresponding mu&sigma (see below).
>
> * I would like to limit the age range to include all 12 months for the 1st 5 years and only whole years for all ages thereafter for each of the countries present in the data frame.
>
> * I would like to create separate data.frames according to the country the data is from (Bolivia.bcpe.lat.m, brazil.bcpe.lat.m, etc)
>
>
> I have tried using: c(seq(0,5,1/12),seq(5,50,1) ) to select the desired ages but am unsure how to repeat that sequence for consecutive countries.
> I have tried using: split(bcpe.lat.m, bcpe.lat.m$country) But end up with a string which I am no longer to select the specific ages I want and all the data still remains in one variable
> Have also looked a 'by', 'apply' and things like 'for (i in 1:13)'
>
> Help with either or both steps would be greatly appreciated.
>
> Greetings from Formentera,
> Daniel
>
> Age(yrs) country mu sigma
> 1 0.00000000 Bolivia 11.42168 0.1014872
> 2 0.08333333 Bolivia 11.33625 0.1053837
> 3 0.16666667 Bolivia 11.28417 0.1070594
> 4 0.25000000 Bolivia 11.21125 0.1083872
> 5 0.33333333 Bolivia 11.11637 0.1095305
> ...
> 602 0.00000000 Brazil 11.54888 0.10839417
> 603 0.08333333 Brazil 11.46345 0.11255592
> 604 0.16666667 Brazil 11.41137 0.11434565
> 605 0.25000000 Brazil 11.33844 0.11576378
> 606 0.33333333 Brazil 11.24357 0.11698489
> ...
>
>
> [[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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list