Home > OS >  How to access the entry value with unrecognised number of decimal in data frame in R?
How to access the entry value with unrecognised number of decimal in data frame in R?

Time:12-20

I have a data frame in R that I want to analyse. I want to know how many specific numbers are in a data frame column. for example, I want to know the frequency of number 0.9998558 by using

sum(deviation_multiple_regression_3cell_types_all_spots_all_intersection_genes_exclude_50_10dec_rowSums_not_0_for_moran_scaled[,3]== 0.9998558)

However, it seems that the decimal shown is not the actual one (it must be 0.9998558xxxxx) since the result I got from using the above command is 0 (the correct one should be 3468). How can I access that number without knowing the exact decimal numbers so that I get the correct answer? Please see the screenshot below.

dataframe

CodePudding user response:

The code below gives the number of occurrences in the column.

x <- 0.9998558
length(which(df$a==x))

CodePudding user response:

If you are looking for numbers stating with 0.9998558, I think you can do it in two different ways: working with data as numeric or as character. Let x be your variable:

Data as character

This way counts exactly what you are looking for

  sum(substr(as.character(x),1,9)=="0.9998558")

Data as numeric

This will include all the values with a difference with the reference value lower than 1e-7; this may include values not starting exactly with 0.9998558

  sum(abs(x-0.9998558)<1e-7)

You can also "truncate" the numbers in your vector and compare them with the number you want. Here, we write 10^7 because 7 is the number of decimals you want to compare.

sum(trunc(x*10^7)/10^7)==0.9998558)
  • Related