简体   繁体   中英

Basic R routine: renaming values in a data.frame column

I hope this is the right place to ask my question: I want to replace longer male/female strings in the sex-column of my data.frame with just 'm'/'f'. How to build a wildcard-function stating "if string starts with m/f, set its values to m/f"?

Possible data:

df <- data.frame(list(A=c("fem","mal"), B=c(12, 17)))

Thank you so much

df$A <- substring(df$A, 1, 1)
df
#   A  B
# 1 f 12
# 2 m 17

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM