ez.replace: replace a single value in data frame with another value

View source: R/frame.R

ez.replaceR Documentation

replace a single value in data frame with another value

Description

replace within one or more than one columns, or entire data frame (ie, all columns)
keep data type whenever possible, var label and value labels are also kept, but notice that value labels might be incomplete because of the insertion of new unlabelled value

Usage

ez.replace(df, col, oldval, newval = NULL, print2scr = T)

Arguments

df

data frame

col

column name evaluated by eval('dplyr::select()'), can be single, or multiple/vector eg, c('col1','col2'). If skipped (not provided, ie, three-parameter format), all columns used

oldval

old value (e.g., -Inf, NA), can only be single, not multiple/vector. Note would not differentiate 5.0 and 5

newval

new value (e.g., NA), can only be single, not multiple/vector

Details

smilar to ez.recodenum->num (if get replaced with another num), numeric->char (if get replaced with a char), char->char, factor->factor (factor internally converted to char then back to factor)
wrapper of df[[col]][which(df[[col]]==oldval)] <- newval
a=c(1,2,3); a[which(a=='usb')] <-'cup'; a # the assign of a char (even though no match) will change a to char of "1" "2" "3"!
a=c(1,2,3); a[which(a==4)] <-'cup'; a # a changes a to char as well
a=c(1,2,3); a[which(a==4)] <-3.1; a # a changes a to double
a=c(1,'2',3); a[which(a==1)] <-4; a # here a=c(1,'2',3)->c('1','2','3'), then '1'==1 TRUE, because 1 converted to '1', finally 4 converted to '4' assigned
a=c(1,2,3); a[which(a=='1')] <-4; a # same logic, but the returned a is still numeric
Thus, the conclusion is: alawys better to not quote numbers. It is not compatible, could auto convert.
bottom line: the new val determines outcome even without match
But my script protects that; data type remains unchanged if there is no match
logic is tricky TRUE=='TRUE', FALSE=='FALSE' return TRUE; always convert them first to num, eg, mutate(preterm=as.integer(delivery_ega<37.0)) and then start from there.

Value

returns a new df, old one does not change

Note

when 4 parameters provided, it is recognized as (df,col,oldval,newval)
when 3 parameters provided, it is recognized as (df,oldval,newval) see example

See Also

ez.recode, ez.recode2, ez.replace, ez.replacewhen, ez.2label, ez.factorname, ez.strreplace, ez.strrev, ez.regexprep, ez.regexprepi

Other data transformation functions: ez.2char(), ez.2factor(), ez.2label(), ez.2long(), ez.2lower(), ez.2value(), 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.rncols(), ez.rncol(), ez.select(), ez.sort(), ez.split(), ez.str(), ez.unique(), left_join()

Examples

data=data.frame(a=factor(c(1,2)))
ez.replace(data,'a',1,3) %>% .$a
ez.replace(data,'a',1,'abc') %>% .$a
          # a was factor with level (1,2), now is factor with level (2,3), (2 abc)

data=data.frame(a=c('r1','r2'))
ez.replace(data,'a','r1',3) %>% .$a
          # a was factor with level ('r1','r2'), now is factor with level ('3','r2')

data=data.frame(a=c('r1','r2'),stringsAsFactors = F)
ez.replace(data,'a','r1',3) %>% .$a
ez.replace(data,'a','r1',NA) %>% .$a
          # a was char, now is still char, (NA counted here as char)

data=data.frame(a=c(1,2))
ez.replace(data,'a',1,111) %>% .$a
ez.replace(data,'a',1,NA) %>% .$a
          # a was numeric, now is still numeric, (NA counted here as numeric)

ez.replace(data,'a',1,'111') %>% .$a
ez.replace(data,'a',1,'abc') %>% .$a
          # a was numeric, now is character

data=iris[1:10,]; data[1,2]=NA; data[2,5]=NA; data[4,'Species']='versicolor'; data[6,'Species']='virginica'; data['TV']='COBY'
ez.replace(data,c('Sepal.Width','Petal.Length','Petal.Width','Species'),NA,3.1415)
ez.replace(data,NA,3.1415)
          # Species was factor, now is numeric (factor->numeric)
ez.replace(data,NA,'replaced')
          # Sepal.Width was numeric, now is char
          # Species was factor, now is char (factor->char of num)
ez.replace(data,5.1,3.14)
          # Sepal.Length was numeric, now is still numeric
ez.replace(data,'TV','COBY','Mac')
          # TV was char, now is still char
ez.replace(data,'COBY','Mac')
          # TV was char, now is still char
ez.replace(data,'setosa','flower')
          # Species was factor, now is still factor (notice, levels changed)

jerryzhujian9/zmisc documentation built on March 9, 2024, 12:49 a.m.