R: how can I create a table with mean and sd according to experimental group alongside p-values?

前端 未结 4 1680
心在旅途
心在旅途 2021-01-03 09:15

I know how I can do all that for individual variables but I need to report this information for a large number of variables and would like to know if there is an efficient w

4条回答
  •  天命终不由人
    2021-01-03 09:34

    First let's make some example data. For each sample, we have a unique ID, its experimental group, and some variables for which we want to calculate the mean and SD.

    ## Make a data frame called "Data" with five columns
    Data <- as.data.frame(cbind(1:100, sample(1:2), rnorm(100), rnorm(100), rnorm(100), rnorm(100)))
    names(Data) <- c("ID", "Group", "V1", "V2", "V3", "V4")
    
    ## Now we will take a peak at the top of our data frame
    > head(Data)
    
      ID Group         V1         V2         V3         V4
    1  1     2  0.3681539 -0.5008400  1.2060665 -0.7352376
    2  2     1 -0.1043180  2.2038190 -1.4367898  2.1961246
    3  3     2 -0.2720279 -0.5923554 -1.4628190 -1.8776453
    4  4     1 -2.3299662 -0.1216227  0.4200776  1.5504020
    5  5     2 -0.3670578 -1.5903221 -0.6287083 -1.0543262
    6  6     1  0.4840047 -0.3181554 -1.4596980 -0.4261827
    

    Now we can run a for loop through the variables, pull their means and SDs and p values, and dump them all in an object called "Results".

    ## Define object which will receive our results
    Results <- NULL
    Results <- as.data.frame(Results)
    
    ## Open for loop
    for (i in 3:6) {
    
    ## Run the t.test() and save it in a temporary object
    temp <- t.test(Data[which(Data$Group == 1), i], Data[which(Data$Group == 2), i])
    
    ## Put the name of our variable in our results object
    Results[i-2,1] <- names(Data)[i]
    
    ## Group 1 mean and SD
    Results[i-2,2] <- temp$estimate[1]
    Results[i-2,3] <- sd(Data[which(Data$Group == 1), i])
    
    ## Group 2 mean and SD
    Results[i-2,4] <- temp$estimate[2]
    Results[i-2,5] <- sd(Data[which(Data$Group == 2), i])
    
    ## P value for difference
    Results[i-2,6] <- temp$p.value
    
    rm(temp)
    }
    

    Now we can make our results pretty and print them.

    ## Add column names
    names(Results) <- c("Variable", "Group.1.Mean", "Group.1.SD", "Group.2.Mean", "Group.2.SD", "P.Value")
    
    ## View our results
    > Results
    
      Variable Group.1.Mean Group.1.SD Group.2.Mean Group.2.SD   P.Value
    1       V1   0.21544390  0.9404104  -0.01426226  1.0570324 0.2537820
    2       V2   0.26287585  1.0048291   0.22992285  0.9709686 0.8679038
    3       V3  -0.06112963  0.9855287   0.17423440  1.0198694 0.2434507
    4       V4   0.33848678  0.9360016   0.07905932  0.9106595 0.1632705
    

提交回复
热议问题