How can I find and replace decimal numbers among whole numbers in a column in R?

后端 未结 2 1447
予麋鹿
予麋鹿 2021-01-16 11:56

I am working with a dataset that came from a machine. In one column, all values are whole numbers without any decimals, but sometimes the machine recorded some numbers with

2条回答
  •  迷失自我
    2021-01-16 12:42

    simply use the ifelse() function

     d<-c(1, 1, 1, 2, 3, 4, 5, 8.6, 6, 7, 7, 7, 7, 8, 9, 9, 4.2, 1, 1, 1, 2, 3)
    
    
    e<-ifelse(d==as.integer(d), d, NA) # instead of NA you can insert any value you want. 
    

    result:

    1  1  1  2  3  4  5 NA  6  7  7  7  7  8  9  9 NA  1  1  1  2  3
    

    you said you didn't want to just round the values but if it was an option a simple d<-as.integer(d) would solve the problem as well.

提交回复
热议问题