Home > Mobile >  Taking the mean of a column within a function and a for loop
Taking the mean of a column within a function and a for loop

Time:10-21

I have the below function :

  compute_treatment_effects <- function(dataset, outcome, baseline_outcome, 
                                      covariates, 
                                      standardize){
  
  
  baseline_covariates <- c(baseline_outcome, covariates)
  
    
  dataset <- dataset %>%
    mutate(treat =ifelse(treatment_group == "trt", 1, 
                           ifelse(treatment_group == "control", 0, NA))) %>%
    filter(!is.na(treat))  
    
  if (standardize){
    dataset[,outcome] <- (dataset[,outcome] - mean(dataset[dataset$treat==0,outcome], na.rm=TRUE))/
      sd(dataset[dataset$treat==0,outcome], na.rm=TRUE)
  }
}

Now the issue, is whenever it gets to the standardization procedure, I get an error :

"Error in is.data.frame(x) : 'list' object cannot be coerced to type 'double' In addition: Warning message: In mean.default(dataset[dataset$treat == 0, outcome], na.rm = TRUE)"

I am really not sure why this is the case, I dont believe the syntax is wrong anywhere ?

Here is an example of a dataframe to use with the code:

dataframe <- data.frame("var1" = c(1, 2, 5, 1, 642, 5, 1, 2, 5, 9, NA, 8, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 ),
                 "Var2"  = c(1, 3, 5, 1, 642, 5, NA, NA, NA, NA, NA, NA, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 ), 
                 "var3"   = c(1, 2, 635, 9, NA, 1, 2, 5, NA, NA, 12, NA, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10),
                 "var4"  = c(1, 21, 15, 19, NA, 1, 26656, 56,6 , NA, 512, NA, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10), 
                 "cov1" =  c(1, 22,335, 29, NA, NA, NA, 645, NA, NA, 12, NA, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10),
                 "cov2" =  c(44251, 2322,5, 29, 45, 35, 42, 645, 55, 525, NA, NA, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10),
                 "cov3" =  c(154, 2552,35, 53529, 5, 3, 53542, 645, 25, 2, 12, 23, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10))


dataframe <- dataframe %>%
  mutate(treatment_group = ifelse(var3 == 2, "trt", ifelse(var3 == 10, "control", NA)))
dataset <- dataframe
outcome <- "Var2"
baseline_outcome <- "var1"
covariates = c("cov1", "cov2","cov3")

Thank you so much!!!

CodePudding user response:

It is possible that the OP's original dataset is either tibble or data.table because both of them doesn't subset the columns into a vector when we do , column as it is drop = FALSE in both when compared to data.frame (which is drop = TRUE)

> compute_treatment_effects(as_tibble(dataset), outcome, baseline_outcome, covariates, standardize = TRUE)

Error in is.data.frame(x) : 'list' object cannot be coerced to type 'double' In addition: Warning message: In mean.default(dataset[dataset$treat == 0, outcome], na.rm = TRUE) : argument is not numeric or logical: returning NA


The fix would be to either convert to data.frame with as.data.frame

compute_treatment_effects(as.data.frame(dataset), outcome, baseline_outcome, covariates, standardize = TRUE)

-output

var1 Var2 var3  var4 cov1 cov2  cov3 treatment_group treat
1     2 -Inf    2    21   22 2322  2552             trt     1
2     1   NA    2 26656   NA   42 53542             trt     1
3    10  NaN   10    10   10   10    10         control     0
4    10  NaN   10    10   10   10    10         control     0
5    10  NaN   10    10   10   10    10         control     0
6    10  NaN   10    10   10   10    10         control     0
7    10  NaN   10    10   10   10    10         control     0
8    10  NaN   10    10   10   10    10         control     0
9    10  NaN   10    10   10   10    10         control     0
10   10  NaN   10    10   10   10    10         control     0
11   10  NaN   10    10   10   10    10         control     0
12   10  NaN   10    10   10   10    10         control     0

Or make the changes in the function by using [[ instead of [ for subsetting the column i.e.

compute_treatment_effects <- function(dataset, outcome, baseline_outcome, 
                                       covariates, 
                                       standardize){
  
  
   baseline_covariates <- c(baseline_outcome, covariates)
  
    
   dataset <- dataset %>%
     mutate(treat =ifelse(treatment_group == "trt", 1, 
                            ifelse(treatment_group == "control", 0, NA))) %>%
     filter(!is.na(treat))  
    
   if (standardize){
     dataset[[outcome]] <- (dataset[[outcome]] - 
       mean(dataset[[outcome]][dataset$treat==0], na.rm=TRUE))/
   
       sd(dataset[[outcome]][dataset$treat==0], na.rm=TRUE)
   }
   dataset
 }
  • Related