我有一个存储字符串的数据框,其中一些字符串可以解释为数字,但是,它们仍然是类字符。我想自动将所有可以解释为数字的列转换为数字。我可以使用 mutate_if 轻松完成此操作,但是它会为每个剩余的字符列生成 NA。我想保留这些列中的原始信息。
# Reproducible example
df <- data.frame(Col1 = c("647", "237", "863", "236"),
Col2 = c("125", "623", "854", "234"),
Col3 = c("ABC", "BCA", "DFL", "KFD"),
Col4 = c("PWD", "CDL", "QOW", "DKC"))
df %>% mutate_if(is.character, as.numeric)
Col1 Col2 Col3 Col4
1 647 125 NA NA
2 237 623 NA NA
3 863 854 NA NA
4 236 234 NA NA
Warning messages:
1: Problem while computing `..1 = across(, ~as.numeric(.))`.
ℹ NAs introduced by coercion
2: Problem while computing `..1 = across(, ~as.numeric(.))`.
ℹ NAs introduced by coercion
期望的输出:
# Character strings still available
Col1 Col2 Col3 Col4
1 647 125 ABC PWD
2 237 623 BCA CDL
3 863 854 DFL QOW
4 236 234 KFD DKC
str(df)
'data.frame': 4 obs. of 4 variables:
$ Col1: num 647 237 863 236
$ Col2: num 125 623 854 234
$ Col3: chr "ABC" "BCA" "DFL" "KFD"
$ Col4: chr "PWD" "CDL" "QOW" "DKC"
最佳答案
一个可能的解决方案:
df <- type.convert(df, as.is = T)
str(df)
#> 'data.frame': 4 obs. of 4 variables:
#> $ Col1: int 647 237 863 236
#> $ Col2: int 125 623 854 234
#> $ Col3: chr "ABC" "BCA" "DFL" "KFD"
#> $ Col4: chr "PWD" "CDL" "QOW" "DKC"
关于R:在保持字符列的同时使用 mutate 将所有列转换为数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72804552/