用fread()''csv'文件替换点,逗号和百分比标记``''
我们想从csv
文件中管理列,最初是三个contrac
列类,当我们使用fread()
,并在此处详细介绍了参数R代码
e节(指定了分隔符和十进制参数)。 R会话版本为4.2.0
,data.table
版本是1.14.2
。
输入来自csv
file
col_1,col_2, col_3
/100.432,"30,84Â %","4,14"
/3.200,"62,89Â %","1,89"
/10.100,"50,00Â %","1,62"
/15.570, "40,10Â %","3,41"
/900.310, "8,00Â %","0,10"
input 数据的数据r
sessign
> dat
# A tibble: 5 × 3
col_1 col_2 col_3
<chr> <chr> <chr>
1 100.432 30,84 % 4,14
2 3.200 62,89 % 1,89
3 10.100 50,00 % 1,62
4 15.570 40,10 % 3,41
5 900.310 8,00 % 0,10
r代码
data.table::fread(
x,
sep = ',',
dec = '.',
na.strings = c('', 'NA')) %>%
as_tibble()
所需的 output 数据
> dat
# A tibble: 5 × 3
col_1 col_2 col_3
<dbl> <dbl> <dbl>
1 100438 30.84 4.14
2 3200 62.89 1.89
3 10100 50.00 1.62
4 15570 40.10 3.41
5 900310 8.00 0.10
问题
我们想获得所需的输出数据
格式。
提前致谢
We would like to manage the columns from csv
file with originally three character
columns class when we used the fread()
with the arguments detailed in the R code used
section (separator and decimal arguments are specified). The R session version is 4.2.0
and the data.table
version is 1.14.2
.
Input data from csv
file
col_1,col_2, col_3
/100.432,"30,84Â %","4,14"
/3.200,"62,89Â %","1,89"
/10.100,"50,00Â %","1,62"
/15.570, "40,10Â %","3,41"
/900.310, "8,00Â %","0,10"
Input data in R
session
> dat
# A tibble: 5 × 3
col_1 col_2 col_3
<chr> <chr> <chr>
1 100.432 30,84 % 4,14
2 3.200 62,89 % 1,89
3 10.100 50,00 % 1,62
4 15.570 40,10 % 3,41
5 900.310 8,00 % 0,10
R code used
data.table::fread(
x,
sep = ',',
dec = '.',
na.strings = c('', 'NA')) %>%
as_tibble()
Desired output data
> dat
# A tibble: 5 × 3
col_1 col_2 col_3
<dbl> <dbl> <dbl>
1 100438 30.84 4.14
2 3200 62.89 1.89
3 10100 50.00 1.62
4 15570 40.10 3.41
5 900310 8.00 0.10
Question
We would like to obtain the Desired output data
format.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在R中进行一些后处理:
You could just do some postprocessing in R:
我已经改善了@Sindri_Baldur提出的后处理。在
中dplyr
版本1.0.9
代码应为:input 来自
csv
file file输入
r
的数据会话r代码
最终输出数据,
感谢大家提供解决方案并改进代码。
I've improved the postprocessing proposed by @sindri_baldur. In
dplyr
version1.0.9
the code should be:Input data from
csv
fileInput data in
R
sessionR code used
Final output data
Thanks to everybody for giving a solution and improving the code.