r - Stuck with a 2 data frames row copy -
r - Stuck with a 2 data frames row copy -
i have decided larn r , going through introduction scientific programming in r book (http://www.ms.unimelb.edu.au/spurs/)
i stuck on chapter 7 question 3 of book, question is:
consider next simple genetic model. population consists of equal numbers of 2 sexes: male , female. @ each generation men , women paired @ random, , each pair produces 2 offspring, 1 male , 1 female. interested in distribution of height 1 generation next. suppose height of both children average of height of parents, how distribution of height alter across generations?
represent heights of current generation dataframe 2 variables, m , f, 2 sexes. command rnorm(100, 160, 20) generate vector of length 100, according normal distribution mean 160 , standard deviation 20 (see section 16.5.1). utilize randomly generate population @ generation 1:
pop <- data.frame(m = rnorm(100, 160, 20), f = rnorm(100, 160, 20))
the command sample(x, size = length(x)) homecoming random sample of size size taken vector x (without replacement). (it sample replacement, if optional argument replace set true.) next function takes dataframe pop , randomly permutes ordering of men. men , women paired according rows, , heights next generation calculated taking mean of each row. function returns dataframe same structure, giving heights of next generation.
next.gen <- function(pop) { pop$m <- sample(pop$m) pop$m <- apply(pop, 1, mean) pop$f <- pop$m return(pop) }
use function next.gen generate 9 generations, utilize lattice function histogram plot distribution of male heights in each generation, in figure 7.7. phenomenon see called regression mean.
hint: build dataframe variables height , generation, each row represents single man.
i have constructed blank info frame:
generations <- data.frame(gen="", height="")
for trying first generation height info it, run:
next.gen(pop) generations$height <- pop$m
and next error:
error in `$<-.data.frame`(`*tmp*`, "height", value = c(165.208323681597, : replacement has 100 rows, info has 1
i understand i'm trying squeeze in info pop$m dataframe single row of generations$height , causing problem, not know how prepare this? thought blank info frame flexible plenty add together rows beingness copied pop info frame?
i tried run code:
generations <- pop$m
and 100 values turns generations dataframe vector think , running
generations
just lists values copied in vector only.
i think approaching first step wrong, dataframe definition correct? why can't re-create row info 1 info frame empty 1 , adjust size of empty info frame needed?
thank you
unsure exact output looking for. here approach should simple plenty follow. ** note: there workable approaches aplenty.
pop <- data.frame(m = rnorm(100, 160, 20), f = rnorm(100, 160, 20)) next.gen <- function(pop) { pop$m <- sample(pop$m) pop$m <- apply(pop, 1, mean) pop$f <- pop$m return(pop) } # code test <- list() (i in 1:9) { test[[i]] <- next.gen(pop)["m"] test[[i]]$generation <- paste0("g", i) } library(data.table) test2 <- rbindlist(test) # result m generation 1: 174.6558 g1 2: 143.2617 g1 3: 185.2829 g1 4: 168.9719 g1 5: 151.6948 g1 --- 896: 159.6091 g9 897: 161.4546 g9 898: 171.8679 g9 899: 138.4982 g9 900: 152.7390 g9
r
Comments
Post a Comment