英文:
How to set consistent decimal separators in R data frame?
问题
某些数据框中的列使用逗号 ',' 作为千位分隔符,而其他列使用句点 '.' 作为小数分隔符。
DateTime a b c
2000-01-01 00:30:00 3,4 6,1 9.5
2000-01-01 00:45:00 3,5 6,8 7.4
我想在整个数据框中使用句点 '.' 作为小数分隔符。
我尝试了以下代码:
df = gsub(',', '.', df)
但它只返回一个新的值,而不是数据框。我还尝试了以下代码:
df = lapply(df, function(x) as.character(gsub(",", ".", df)))
但它只返回一个列表。我还尝试将这些列设置为数值型(因为它们目前由于之前的操作都是字符型),但它将a和b中的所有值都设置为NA。
英文:
Some columns in my data frame have ',' and some have '.' as decimal separator.
DateTime a b c
2000-01-01 00:30:00 3,4 6,1 9.5
2000-01-01 00:45:00 3,5 6,8 7.4
I would like to have '.' as a decimal separator throughout my data frame.
I have tried
df = gsub(',','.',df)
but it just gives me a new Value
instead of a data frame. I also tried
df = lapply(df, function(x) as.character(gsub(",",".",df)))
but it just makes a list.
I have tried to set the columns as numeric (currently they're all characters due to earlier procedures), but it sets all values in a and b as NA.
答案1
得分: 0
将逗号作为小数分隔符的数字很可能是字符串。可以这样将它们转换为数值:
library(readr)
parse_number("3,4", locale = locale(decimal_mark = ","))
#> [1] 3.4
创建于2023-03-20,使用reprex v2.0.2
英文:
Probably the numbers with a comma as decimal separator are character strings. Convert them to numeric values like so:
library(readr)
parse_number("3,4", locale = locale(decimal_mark = ","))
#> [1] 3.4
<sup>Created on 2023-03-20 with reprex v2.0.2</sup>
答案2
得分: 0
你可以在所有列中用.
替换`,``,如下所示:
library(tidyverse)
df %>%
mutate(across(everything(), ~str_replace(., ",", ".")))
如果有列不应替换逗号,您可以更具体地处理需要进行更改的列,例如:
library(tidyverse)
df %>%
mutate(across(a:c, ~str_replace(., ",", ".")))
数据:
df <- data.frame(
DateTime = c(2000-01-01, 2000-01-01),
a = c("3,4","3.5"),
b = c("6,1", "6,8"),
c = c(9.5, 7.4))
英文:
You can replace ,
with .
in all columns like this:
library(tidyverse)
df %>%
mutate(across(everything(), ~str_replace(., ",", ".")))
If you have columns where ,
should not be replaced you can address the ones where the change should be implemented more specifically, e.g., like this:
library(tidyverse)
df %>%
mutate(across(a:c, ~str_replace(., ",", ".")))
Data:
df <- data.frame(
DateTime = c(2000-01-01, 2000-01-01),
a = c("3,4","3.5"),
b = c("6,1", "6,8"),
c = c(9.5, 7.4))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论