设置循环以在r中使用函数

时间:2019-12-05 01:23:57

标签: r loops

我有一个包含三列的数据框,我想为媒体中的每个变量计算95%的上公差水平。数据如下所示: 因此,对于塑料和水,我需要分别计算每个变量的公差水平,并将其写为第四列。我正在使用

nptol.int(data$result, alpha = 0.05, P = 0.95, side = 1, method=c("WILKS")) 

功能。

media   variable    result
plastic A   2.3
plastic B   4
plastic C   4.6
plastic D   3
plastic A   2
plastic B   5
plastic C   6.7
plastic A   8
plastic B   5
plastic C   4
water   A   2
water   B   4
water   C   5
water   A   8.2
water   B   4
water   C   5
plastic A   6
plastic B   7
plastic C   11.2

谢谢

1 个答案:

答案 0 :(得分:1)

使用 const [LocalCodeMutation] = useMutation(LOCALCODE_MUTATION, { refetchQueries: () => [ { query: GET_LOCALCODES }, ], }); export const LOCALCODE_MUTATION = gql` mutation LocalCodeMutation($data: LocalCodeRequestParamsInput) { localCodeMutation(data: $data) { ok errors localCodeInsertedId } } `; ,您可以执行以下操作:

dplyr

如果您希望将其写为第4列并保留所有结果,则可以执行以下操作:

library(dplyr)
library(tolerance)
df %>% group_by(media, variable) %>% summarize(Upper = nptol.int(result, alpha = 0.05, P = 0.95, side = 1, method=c("WILKS"))$`1-sided.upper`)

# A tibble: 6 x 3
# Groups:   media [2]
  media   variable Upper
  <fct>   <fct>    <dbl>
1 plastic A         11.2
2 plastic B          8  
3 plastic C          7  
4 water   A          5  
5 water   B          8.2
6 water   C          4  

数据 您的数据只有一个D值,显然这是df %>% group_by(media, variable) %>% mutate(Upper = nptol.int(result, alpha = 0.05, P = 0.95, side = 1, method=c("WILKS"))$`1-sided.upper`) # A tibble: 19 x 4 # Groups: media, variable [6] media variable result Upper <fct> <fct> <dbl> <dbl> 1 plastic A 2.3 11.2 2 plastic B 4 8 3 plastic C 4.6 7 4 plastic A 3 11.2 5 plastic B 2 8 6 plastic C 5 7 7 plastic A 6.7 11.2 8 plastic B 8 8 9 plastic C 5 7 10 plastic A 4 11.2 11 water B 2 8.2 12 water C 4 4 13 water A 5 5 14 water B 8.2 8.2 15 water C 4 4 16 water A 5 5 17 plastic B 6 8 18 plastic C 7 7 19 plastic A 11.2 11.2 的问题,因此我改用了此数据:

npol.int