将逗号转换为点和数字,但仅限于一定数量的变量
Posted
技术标签:
【中文标题】将逗号转换为点和数字,但仅限于一定数量的变量【英文标题】:Convert comma's to point and as numeric, but only in a certain amount of variables 【发布时间】:2020-10-22 15:39:22 【问题描述】:所以我有一个看起来像这样的 df,其中数值拆分为逗号而不是点,它们被归类为字符。
var0 <- c("There, are commas", "in the text, string", "as,well", "how, can", "i", "fix, this", "thank you")
var1 <- c("50,0", "72,0", "960,0", "1.920,0", "50,0", "50,0", "960,0")
var2 <- c("40,0", "742,0", "9460,0", "1.920,0", "50,0", "50,0", "960,0")
var3<- c("40,0", "72,0", "90,0", "1,30", "50,0", "50,0", "960,0")
...
var96 <- c("40,0", "742,0", "9460,0", "1.920,0", "50,0", "50,0", "960,0")
df <- data.frame(cbind(var0, var1, var2, var3))
我知道如何使用 gsub 手动转换每个变量,但正如您在下面看到的,我有大约 96 个。除此之外,我还有其他变量,包括不需要转换逗号的文本字符串和因子级别。
有什么建议吗?
谢谢
【问题讨论】:
创建data.frame时,不需要cbind
。
【参考方案1】:
这是一个仅用小数点替换逗号并在所有字符都是数字 0-9、点和逗号时删除所有其他点的函数。
commas2dots <- function(x)
if(any(grepl("[^\\.,[:digit:]]", x)))
x
else
y <- gsub("\\.", "", x)
tc <- textConnection(y)
on.exit(close(tc))
scan(tc, dec = ",", quiet = TRUE)
lapply(df, commas2dots)
#$var0
#[1] "There, are commas" "in the text, string"
#[3] "as,well" "how, can"
#[5] "i" "fix, this"
#[7] "thank you"
#
#$var1
#[1] 50 72 960 1920 50 50 960
#
#$var2
#[1] 40 742 9460 1920 50 50 960
#
#$var3
#[1] 40.0 72.0 90.0 1.3 50.0 50.0 960.0
#
#$var96
#[1] 40 742 9460 1920 50 50 960
更改 data.frame 的列:
df[] <- lapply(df, commas2dots)
df
# var0 var1 var2 var3 var96
#1 There, are commas 50 40 40.0 40
#2 in the text, string 72 742 72.0 742
#3 as,well 960 9460 90.0 9460
#4 how, can 1920 1920 1.3 1920
#5 i 50 50 50.0 50
#6 fix, this 50 50 50.0 50
#7 thank you 960 960 960.0 960
数据
var0 <- c("There, are commas", "in the text, string", "as,well", "how, can", "i", "fix, this", "thank you")
var1 <- c("50,0", "72,0", "960,0", "1.920,0", "50,0", "50,0", "960,0")
var2 <- c("40,0", "742,0", "9460,0", "1.920,0", "50,0", "50,0", "960,0")
var3<- c("40,0", "72,0", "90,0", "1,30", "50,0", "50,0", "960,0")
var96 <- c("40,0", "742,0", "9460,0", "1.920,0", "50,0", "50,0", "960,0")
df <- data.frame(var0, var1, var2, var3, var96)
【讨论】:
【参考方案2】:tidyverse 包非常适合这种事情。
library(tidyverse)
df <- df %>%
# First, remove the points in your numbers b/c otherwise, you'll end up
# with, e.g., "1.920.0"
mutate_all(.fun = function(x) gsub("\\.", "", x)) %>%
# Next, replace all the commas with points and convert to numeric. Only do
# this for the columns that don't contain text, though.
mutate_at(.vars = vars(matches("var[1-3]")),
.fun = function(x) as.numeric(gsub(",", "\\.", x)))
请注意,在mutate_at
调用中,我假设只有“var0”列包含您想要保留的文本,并且我将任何与正则表达式“var[1-3]”匹配的内容转换为数字数据和使用的点而不是逗号。您需要根据您的情况调整该正则表达式。
【讨论】:
以上是关于将逗号转换为点和数字,但仅限于一定数量的变量的主要内容,如果未能解决你的问题,请参考以下文章