You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Base R's internal string functions (regex, strsplit, substr, paste, etc.) all coerce non-string types (particularly factors, but also numbers) to character vectors, e.g.
# Nice ordered factormon<-factor(month.name, month.name, ordered=TRUE)
mon#> [1] January February March April May June July #> [8] August September October November December #> 12 Levels: January < February < March < April < May < June < ... < December# Implicitly coerces factor to character
substr(mon, 1, 3)
#> [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov"#> [12] "Dec"# Operating on levels keeps types properly
levels(mon) <- substr(levels(mon), 1, 3)
mon#> [1] Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec#> 12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < ... < Dec
or more ridiculously
gsub(12L, 21, 1234L)
#> [1] "2134"
While the coercion is at least consistent and probably expected by anyone who has used R for a while, it would occasionally be convenient to have a stricter type-safety requirement whereby all coercion must be explicit (like strict does with apply on data.frames), making R
Base R's internal string functions (regex,
strsplit
,substr
,paste
, etc.) all coerce non-string types (particularly factors, but also numbers) to character vectors, e.g.or more ridiculously
While the coercion is at least consistent and probably expected by anyone who has used R for a while, it would occasionally be convenient to have a stricter type-safety requirement whereby all coercion must be explicit (like strict does with
apply
on data.frames), making Rmore like Python:
The text was updated successfully, but these errors were encountered: