Monday 15 June 2015

r - Automatically compute and name several new versions of eachvariable in a dataset -



r - Automatically compute and name several new versions of eachvariable in a dataset -

for many of variables, need create 3 new versions (i.e., mean-centered, mean-centered plus 1 standard deviation, , mean centered minus 1 standard deviation) before can run simple effects tests.

i can create function calculate each new variable, not rename variable automatically, , time-consuming , repetitive many variables.

# illustration info of test performance d <- read.table(header=t, text=' subject sex math read sci 1 m 7.9 12.3 10.7 2 f 6.3 10.6 11.1 3 f 9.5 13.1 13.8 4 m 11.5 13.4 12.9 ') # function create mean-centered version of variable mc <- function(x) { x - mean(x, na.rm=t) } d$readmc <- mc(d$read) # function create mc version minus 1 sd msd <- function(x) { x - sd(x, na.rm=t) } d$readmcmsd <- msd(d$read_mc) # function create mc version plus 1 sd psd <- function(x) { x + sd(x, na.rm=t) } d$readmcpsd <-psd(d$read_mc)

for dataset 1 above how can write single functions or for-loops calculate, rename, , add together dataset 3 new versions of variables math, read , sci?

thanks in advance assistance.

your function like

foo <- function(x, ...) { mc <- x - mean(x, ...) sd <- sd(mc, ...) data.frame(mc = mc, mcmsd = mc - sd, mcpsd = mc + sd) }

and run on math, sci, , read columns lapply

lapply(d[-(1:2)], foo, na.rm = true) # $math # mc mcmsd mcpsd # 1 -0.9 -3.1241103 1.3241103 # 2 -2.5 -4.7241103 -0.2758897 # 3 0.7 -1.5241103 2.9241103 # 4 2.7 0.4758897 4.9241103 # # $read # mc mcmsd mcpsd # 1 -0.05 -1.3056539 1.2056539 # 2 -1.75 -3.0056539 -0.4943461 # 3 0.75 -0.5056539 2.0056539 # 4 1.05 -0.2056539 2.3056539 # # $sci # mc mcmsd mcpsd # 1 -1.425 -2.8955441 0.04554412 # 2 -1.025 -2.4955441 0.44554412 # 3 1.675 0.2044559 3.14554412 # 4 0.775 -0.6955441 2.24554412

r function for-loop

No comments:

Post a Comment