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

后端 未结 2 1432
予麋鹿
予麋鹿 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:32

    If your dataframe is called df and the column is V1 you can identify values which are decimals by

    df$V1 %% 1 != 0
    #[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
    #[14] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
    

    If you want to replace these values (let's say with NA)

    df$V1[df$V1 %% 1 != 0] <- NA
    

    If you want to drop decimal values you can do :

    df <- df[df$V1 %% 1 == 0, , drop = FALSE]
    

提交回复
热议问题