r - Using rnorm for a dataframe -


i wanted use rnorm function on dataframe having e.g nrow=11451 elements. don't know how write code apply rnorm each row leading sim-dataframe nsim columns , nrow rows.

dfsim <- rnorm (n=nsim, mean=df[[?]], sd=df[[?]]) 

as example:

> head(df) object of class "spatiallinesdataframe" slot "data":            linearid            fullname rttyp mtfcc          m01         sd01 nsim 10969 1104486135650       hwy 90 alt     u s1200 0.0009886878 0.0001253361   10 10970 1104486135651       hwy 90 alt     u s1200 0.0009831224 0.0001442643   10 10416 1102965182224 southwest fwy e acc     m s1640 0.0010000000 0.0000000000   10 10494 1103342335512   robin hood ct pvt     m s1780 0.0010000000 0.0000000000   10 10493 1103342334514 little john way pvt     m s1750 0.0010000000 0.0000000000   10 1847  1101842210421      arrowood cir n     m s1400 0.0010000000 0.0000000000   10 

my expected result have ten more columns each row including simulated values.

i used following code got "invalid argument error"

> dfnorm <- apply(df@data, 1, function(x) rnorm(x["nsim"], mean=x["m01"], sd=x["sd01"]))  error in rnorm(x["nsim"], mean = x["m01"], sd = x["sd01"]) :    invalid arguments  

since dataframe large, used subset function keep 3 rows , save .rdata file. here link: df.rdata

can pmap purrr package , build rnorms right dataframe:

library(tidyverse) df@data <- df@data %>%    mutate(rnorms = pmap(list(nsim, m01, sd01), function(n, mu, sd) rnorm(n, mu, sd)),          rnorms = map_chr(rnorms, ~ paste(., collapse = " "))) %>%    bind_cols(., read.table(text = .$rnorms, sep = " ")) %>%   select(-rnorms) 

Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -