Home > other >  How to create a row in a table for (Range) to include (Min:Max) together in R?
How to create a row in a table for (Range) to include (Min:Max) together in R?

Time:06-16

I want to create a table which includes(Mean, SD and Range) for stop types. I used the following but I got two problems clear in the result table:

  1. The labels of the first row (Mean) and the second row (SD) are missing. How can I include them with keeping the values rounded?

  2. I want to create a row for (Range) to include (Min:Max) together, separated by a colon. It gives wrong results. how can I correct that?

df<-NKurdish
attach(df)
Mean<-tapply(NKurdish$VOT, NKurdish$Stop, mean)
SD<-tapply(NKurdish$VOT, NKurdish$Stop, sd)
Min<-tapply(NKurdish$VOT,NKurdish$Stop, min)
Max<-tapply(NKurdish$VOT,NKurdish$Stop, max)
Range<-tapply(Min, Max)
rbind(round(Mean),round(SD),Min,Max, Range)
detach(df)

result

        b    d    g   k  p   t
      -117 -130 -117  84 55  66
        37   33   28  18 12  18
Min   -220 -219 -200  50 39  40
Max    -60  -70  -42 118 84 111
Range    2    1    3   6  4   5

CodePudding user response:

  1. You can use named arguments for rows in rbind to name the rows.
  2. If you want the Range to be two numbers separated by a colon, then you can use paste, but the entire table will become a character matrix. This may not be what you want. However, you can still print it nicely using noquote
result <- rbind(Mean = round(Mean), SD = round(SD), Min, Max,
                Range = paste(Min, Max, sep = ":"))

noquote(result)
#>       b        d        g        k      p     t     
#> Mean  -117     -130     -117     84     55    66    
#> SD    37       33       28       18     12    18    
#> Min   -220     -219     -200     50     39    40    
#> Max   -60      -70      -42      118    84    111   
#> Range -220:-60 -219:-70 -200:-42 50:118 39:84 40:111

Created on 2022-06-16 by the reprex package (v2.0.1)


Data used (taken from question)

Mean <- c(b = -117, d = -130, g = -117, k = 84, p = 55, t = 66)
SD <- c(b = 37, d = 33, g = 28, k = 18, p = 12, t = 18)
Min <- c(b = -220, d = -219, g = -200, k = 50, p = 39, t = 40)
Max <- c(b = -60, d = -70, g = -42, k = 118, p = 84, t = 111)
  •  Tags:  
  • r
  • Related