简体   繁体   English

如何在表中为(Range)创建一行以将(Min:Max)一起包含在R中?

[英]How to create a row in a table for (Range) to include (Min:Max) together in R?

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.缺少第一行 (Mean) 和第二行 (SD) 的标签。 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.我想为 (Range) 创建一行以将 (Min:Max) 包含在一起,用冒号分隔。 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
  1. You can use named arguments for rows in rbind to name the rows.您可以使用rbind中行的命名参数来命名行。
  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.如果希望 Range 是用冒号分隔的两个数字,则可以使用paste ,但整个表格将变成字符矩阵。 This may not be what you want.这可能不是你想要的。 However, you can still print it nicely using noquote但是,您仍然可以使用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)reprex 包于 2022-06-16 创建 (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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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