简体   繁体   English

根据字符选择观察值

[英]select observations based on characters

I have a column in my dataset that R is interpreting as numeric when it should be character.我的数据集中有一列 R 在它应该是字符时将其解释为数字。 The original data was something like this: 1, 1.2, 2, 3, 4, 5, 5.2 It's really important for working with other datasets, that R doesn't add additional 0s, but when I import my csv file, the column looks like this: 1.0, 1.2, 2.0, 3.0, 4.0, 5.0, 5.2原始数据是这样的: 1, 1.2, 2, 3, 4, 5, 5.2 使用其他数据集非常重要,R 不会添加额外的 0,但是当我导入我的 csv 文件时,该列看起来像这样:1.0, 1.2, 2.0, 3.0, 4.0, 5.0, 5.2

How do I remove the extra 0s?如何删除多余的 0? I was thinking something like this might work:我在想这样的事情可能会奏效:

column <- c(1.0, 1.2, 2.0, 3.0, 4.0, 5.0, 5.2)
column <- if_else(column *contains* 0.2, column, round(column, 0)

But I'm not sure what function to use for contains (this is used to pick columns based on heading).但我不确定使用什么函数包含(这用于根据标题选择列)。 Help!帮助!

You can change it to character as you mentioned it should ideally be one.您可以将其更改为您提到的字符,理想情况下它应该是一个。

> column <- c(1.0, 1.2, 2.0, 3.0, 4.0, 5.0, 5.2)
> class(column)
[1] "numeric"
> column <- as.character(column)
> class(column)
[1] "character"
> column
[1] "1"   "1.2" "2"   "3"   "4"   "5"   "5.2"
> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM