Reputation: 161
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 decimals. In this column, all numbers are in order from 1 to 9 and then 1 to 9 again but with different replicates. For example, there might be five of 1 or three of 2.
1
1
1
2
3
4
5
8.6
6
7
7
7
7
8
9
9
4.2
1
1
1
2
3
How can I find these numbers (With decimal) among whole numbers and replace them with other values (Not rounded) or remove these numbers from my dataset? Thanks in advance
Upvotes: 1
Views: 716
Reputation: 1304
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.
Upvotes: 2
Reputation: 388962
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]
Upvotes: 1