简体   繁体   中英

How to get the min and max values of a column?

I have a dataset where the values are collapsed so each row has multiple inputs per one column.

For example:

Gene   Score1                      
Gene1  NA, NA, NA, 0.03, -0.3 
Gene2  NA, 0.2, 0.1, ., .   

I am looking to make 2 new columns that select the min and max values of that column. In reality I have 70 columns so I coded to get all the min and max columns at once with:

get_range <- function(x) {
  x <- type.convert(str_split(x, ",\\s+", simplify = TRUE), na.strings = ".")
  x <- t(apply(x, 1L, function(i) {
    i <- i[!is.na(i)]
    if (length(i) < 1L) c(NA_real_, NA_real_) else range(i)
  }))
  dimnames(x)[[2L]] <- c("min", "max")
  x
}

dt <- dt[, c(Gene = .(Gene), lapply(.SD, get_range)), .SDcols = -"Gene"]

However, my min and max columns outputted from the code look like this:

Gene   Score1.min  Score1.max                     
Gene1    1             5 
Gene2    3             5

Expected output actually is:

Gene   Score1.min  Score1.max                     
Gene1    -0.3          0.03 
Gene2    0.1           0.2

The values are nothing like the actual values I had at the start, I have no idea how my code is getting these as the output - is there something my code making the values no longer be treated as the numbers they originally were?

Input data:

structure(list(Gene = c("Gene1", "Gene2"), Score1 = c("NA, NA, NA, 0.03, -0.3", 
"NA, 0.2, 0.1, ., .")), row.names = c(NA, -2L), class = c("data.table", 
"data.frame"))

type.convert only considers strings in na.strings as missing values. By default, this is "NA" . You set na.strings = "." , which means "NA" are no longer counted as missing. Instead, you need na.strings = c(".", "NA") because both appear in your data.

## The string split result is `character`, of course, with both `"."` and `"NA"` values
(ss = str_split(dt$Score1, ",\\s+", simplify = TRUE))
#      [,1] [,2]  [,3]  [,4]   [,5]  
# [1,] "NA" "NA"  "NA"  "0.03" "-0.3"
# [2,] "NA" "0.2" "0.1" "."    "."   

## What you have creates a factor with `"NA"` as a level
type.convert(ss, na.strings = c("."))
#      [,1] [,2] [,3] [,4] [,5]
# [1,] NA   NA   NA   0.03 -0.3
# [2,] NA   0.2  0.1  <NA> <NA>
# Levels: -0.3 0.03 0.1 0.2 NA

## Here is the solution to get it to be numeric with `type.convert`
type.convert(ss, na.strings = c(".", "NA"))
#      [,1] [,2] [,3] [,4] [,5]
# [1,]   NA   NA   NA 0.03 -0.3
# [2,]   NA  0.2  0.1   NA   NA

The function tstrsplit with data.table is quite convenient:

library(data.table)
dt <- data.table(
  Gene = c("Gene1", "Gene2"), 
  Score1 = c("NA, NA, NA, 0.03, -0.3", "NA, 0.2, 0.1, ., .")
)
# split the Score 1, transpose it and create one row per (gene, score)
# as mentioned earlier, force as numeric using as.numeric
dt <- dt[, .(Score1 = as.numeric(unlist(tstrsplit(Score1, ",")))), by = Gene]

# then take the min and the max per gene
dt[ , .(Min = min(Score1, na.rm = T), Max = max(Score1, na.rm = T)), by= Gene]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM