Home > other >  How to get total number of entries with specific condition in data frame in R
How to get total number of entries with specific condition in data frame in R

Time:01-04

I have a data frame data here pic 1 and this pic 2 and for some reason it includes value that less than 0.15 so this is wrong output. How can I get it fixed? Or how can I get the result as expected?

CodePudding user response:

Your values are character. You cannot make numeric-comparisons and assume it will match with strings. For example,

0.15 < 5.3e-3
# [1] FALSE
"0.15" < "5.3e-3"
# [1] TRUE

Convert your data to numeric and then rerun your logic. Perhaps

moran_deviation_data_multiple_correction_1january_BH_conclusion_spatially_clustered[] <-
  lapply(moran_deviation_data_multiple_correction_1january_BH_conclusion_spatially_clustered,
         type.convert, as.is = TRUE)

(The use of brackets in ...[] <- ... is intentional, without the [] you'll get a list instead of retaining the class data.frame.)

CodePudding user response:

I did some renaming of variables but this should work.

library(data.table)

md_015 = as.data.frame(fread("moran_deviation_data_multiple_correction_1january_BH_conclusion_spatially_clustered.csv"))
md_015_index = which(md_015 > .15)

refined_df <- md_015[,md_015_index] 

refined_df
> refined_df
           V1   METTL7B       DBI       SLN     COX5B     ITM2C     PTGDS     GLUL
1 Moran.index 0.1682925 0.1586386 0.1913133 0.1541711 0.1533256 0.1910979 0.157243

  • Related