简体   繁体   中英

How do I fix this "Error in .subset2(x, i, exact = exact)" problem in R?

I'm using the following dataframe in R:

ID <- c(LETTERS[1:10])
GLUC <- c(88,NA,110,NA,90,88,120,110,NA,90)
TGL <- c(NA,150,NA,200,210,NA,164,170,190,NA)
HDL <- c(32,60,NA,65,NA,32,NA,70,NA,75)
LDL <- c(99,NA,120,165,150,210,NA,188,190,NA)

patient_num <- data.frame(ID,GLUC,TGL,HDL,LDL)

And I want to create a matrix that has GLUC, TGL, HDL and LDL as the row names and mean, median, sd, n and n_miss as the column names. When I put in the following code:

  r <- c(mean(patient_num[[varname]],na.rm=TRUE), 
    median(patient_num[[varname]],na.rm=TRUE), 
    sd(patient_num[[varname]],na.rm=TRUE),
    sum(!is.na(patient_num[[varname]])),
    sum(is.na(patient_num[[varname]]))
    )
  if (length(varname) == 1){
    r <- matrix(r,nrow=T)
  } else{
    for (index in 2:length(varname)){
      oneRow = table1(patient_num,varname[[index]])
      r <- rbind(r,oneRow)
    }
  }
  rownames(r) <- varname
  colnames(r) <- c("mean","median","sd","n","n_miss")
  return(r)
}

table1(patient_num,c("GLUC","TGL","HDL","LDL")) 

I get an error message:

Error in .subset2(x, i, exact = exact) : recursive indexing failed at level 2

Can't seem to figure out what's wrong

There's a simpler solution using sapply() from base R :

new_df <- sapply(patient_num, function(x) list(
  mean = mean(x, na.rm = T),
  sd = sd(x, na.rm = T),
  n = sum(!is.na(x)),
  is_na = sum(is.na(x))))

t(new_df)

#>     mean     sd       n  is_na
#>ID   NA       NA       10 0    
#>GLUC 99.42857 13.45185 7  3    
#>TGL  180.6667 23.0362  6  4    
#>HDL  55.66667 19.00175 6  4    
#>LDL  160.2857 40.06126 7  3 

If you want only the count of non-NA entries in each row, you can just remove ID from patient_num and run the same code.

Note that you might want to transform new_df back to a data.frame .

You can select only one column at a time using [[ .

Here is an alternative way using dplyr functions.

library(dplyr)

table1 <- function(data, varname) {

  data %>%
    select(all_of(varname)) %>%
    tidyr::pivot_longer(cols = everything()) %>%
    group_by(name) %>%
    summarise(mean = mean(value, na.rm = TRUE), 
              median = median(value, na.rm = TRUE), 
              sd = sd(value, na.rm = TRUE), 
              n = sum(!is.na(value)), 
              n_miss = sum(is.na(value)))
}

table1(patient_num,c("GLUC","TGL","HDL","LDL")) 

# A tibble: 4 x 6
#  name   mean median    sd     n n_miss
#  <chr> <dbl>  <dbl> <dbl> <int>  <int>
#1 GLUC   99.4   90    13.5     7      3
#2 HDL    55.7   62.5  19.0     6      4
#3 LDL   160.   165    40.1     7      3
#4 TGL   181.   180    23.0     6      4

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