如何在R数据框中设置一致的小数分隔符?

huangapple go评论77阅读模式
英文:

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 %&gt;%
  mutate(across(everything(), ~str_replace(., &quot;,&quot;, &quot;.&quot;)))

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 %&gt;%
  mutate(across(a:c, ~str_replace(., &quot;,&quot;, &quot;.&quot;)))

Data:

df &lt;- data.frame(
  DateTime = c(2000-01-01, 2000-01-01),
  a = c(&quot;3,4&quot;,&quot;3.5&quot;),
  b = c(&quot;6,1&quot;, &quot;6,8&quot;),
  c = c(9.5, 7.4))

huangapple
  • 本文由 发表于 2023年3月21日 00:15:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792763.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定