ez.2value: e=e, f(0,1)<-f[1,2], g<-g/h/i/j/k

View source: R/frame.R

ez.2valueR Documentation

e=e, f(0,1)<-f[1,2], g<-g/h/i/j/k

Description

e=e, f(0,1)<-f[1,2], g<-g/h/i/j/k
number e<-f[(0/1)]| attr number g(0/1)<–factor attr number h[0/1] / factor char i[girl/boy] / char j/k[(girl/boy)]

Usage

ez.2value(x, col = NULL, start.at = 0, keep.labels = TRUE, ...)

Arguments

x

a data frame or a vector/col

col

internally evaluated by eval('dplyr::select()')
if x is a data frame, col is specified (e.g., "cond"), convert that col only
if x is a data frame, col is unspecified (i.e., NULL default), convert all possible cols in x
if x is not a data frame, col is ignored

start.at

starting index, i.e. the lowest numeric value of the variable's value range.
If NULL, the lowest value of the returned numeric variable corresponds to the lowest factor level (if factor is numeric, eg, factor(1:2)->c(1,2))
or to 1 (if factor levels are not numeric, factor(c('girl','boy'))->c(2,1)).

Attention: ez.2value(c(”,'2','1'),start.at = NULL) -> c(1,3,2), ez.2value(c(”,'2','1'),start.at = 0) -> c(0,2,1)

To keep consistent with other R functions (eg, lm which converts numeric/non-numeric factor to values starting from 0), set start.at=0 in ez.2value(), then factor(1:2)->c(0,1), factor(c('girl','boy'))->c(1,0)
in lm() the coding (0,1) vs.(1,2) does not affect slope, but changes intercept (but a coding from 1,2->1,3 would change slope–interval difference matters)

Details

Variable label will be kept. opposite of ez.2factor, ez.2label wrapper of sjmisc_to_value

Value

returns a numeric variable or a data frame with changed col(s)
if x is a factor with normal chars, will be converted to 1 2 3 etc, see the example
if x, however, is a factor with chars of numbers ez.2value(c("2","4","6.3"),start.at=NULL), will be converted to (2 4 6.3) etc, see the example

See Also

ez.num

Other data transformation functions: ez.2char(), ez.2factor(), ez.2label(), ez.2long(), ez.2lower(), ez.2wide(), ez.compute(), ez.del(), ez.label.get(), ez.label.set(), ez.labels.get(), ez.labels.set(), ez.move(), ez.newcol(), ez.num(), ez.recode(), ez.recols(), ez.recol(), ez.rename(), ez.replace(), ez.rncols(), ez.rncol(), ez.select(), ez.sort(), ez.split(), ez.str(), ez.unique(), left_join()

Examples

e=c(1,2); f=factor(1:2)
g=c(0,0,1,1,1,0); attr(g,'value.labels') <- c(boy=1,girl=0)
h=factor(c(0,0,1,1,1,0)); attr(h,'labels') <- c(boy=1,girl=0)
i=factor(c('girl','girl','boy','boy','boy','girl'))
j=c('x','y'); k=factor(c('x','y'))

ez.2label: e=e, f=f, g/h/i->i, j=j, k=k
ez.2value: e=e, f(0,1)<-f[1,2], g<-g/h/i/j/k
ez.2factor: ef->f, g/h->h, i=i, factor[x,y,z]<j/k

ez.2value(c('','2','1'),start.at = NULL) # -> c(1,3,2)
ez.2value(c('','2','1'),start.at = 0) # -> c(0,2,1)
ez.num(c('','2','1')) # -> c(NA,2,1)
ez.2value(c("2","4","6.3"),start.at=NULL)  # -> (2.0 4.0 6.3)
ez.2value(factor(c("2","4","7.3")),start.at=0)  # -> (0.0 2.0 5.3)
for consistent and easy convertion, consider to use ez.num(c("2","4","7.3")), ez.num(factor(c("2","4","7.3")), force=T) in this case

jerryzhujian9/ezmisc documentation built on March 9, 2024, 12:44 a.m.