Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 多列t检验的一个微妙问题_R_Dplyr_Statistics_T Test - Fatal编程技术网

R 多列t检验的一个微妙问题

R 多列t检验的一个微妙问题,r,dplyr,statistics,t-test,R,Dplyr,Statistics,T Test,我有一个回答多个问题的数据框架(下面有两个问题的可复制示例) 我得到的错误消息是合乎逻辑的,但这是我在实践中可能遇到的情况-一些子集的大小可能为1或0,一些问题的所有回答者可能给出相同的答案等等。我如何使代码优雅地降级,只是在输出TIBLE中的那些单元格中输入一个空格或NA,因为这样或那样的原因无法计算出答案 诚恳 Thomas Philips也许,您可以使用tryCatch来处理错误: library(dplyr) library(tidyr) df %>% filter(Answ

我有一个回答多个问题的数据框架(下面有两个问题的可复制示例)

我得到的错误消息是合乎逻辑的,但这是我在实践中可能遇到的情况-一些子集的大小可能为1或0,一些问题的所有回答者可能给出相同的答案等等。我如何使代码优雅地降级,只是在输出TIBLE中的那些单元格中输入一个空格或NA,因为这样或那样的原因无法计算出答案

诚恳


Thomas Philips

也许,您可以使用
tryCatch
来处理错误:

library(dplyr)
library(tidyr)

df %>%
  filter(Answer_Date == First_Answer_Date) %>%
  select(questions, Sex) %>%
  filter(Sex != "No_Response") %>%
  pivot_longer(cols = -Sex, names_to = "variable") %>%
  group_by(Sex, variable) %>%
  summarize(value = list(value)) %>%
  pivot_wider(names_from = Sex, values_from = value) %>%
  group_by(variable) %>%
  mutate( p_Female = tryCatch(t.test(unlist(Female))$p.value, error = function(e) return(NA)),
          p_Male   = tryCatch(t.test(unlist(Male) )$p.value, error = function(e) return(NA)),
          t_Female = tryCatch(t.test(unlist(Female))$statistic, error = function(e) return(NA)),
          t_Male   = tryCatch(t.test(unlist(Male))$statistic,error = function(e) return(NA))) %>%
  ungroup %>%
  mutate( Female = lengths(Female),
          Male   = lengths(Male))

很漂亮,谢谢。我很惊讶在return(NA)周围没有大括号,即
error=function(e){return(NA)}
,但是我检查了有大括号和没有大括号的情况,它工作得很好。
questions <- c("Q1", "Q2")
df %>%
  select(questions, Sex) %>%
  filter(Sex != "No_Response") %>%
  gather(key = variable, value = value, -Sex) %>%
  group_by(Sex, variable) %>%
  summarize(value = list(value)) %>%
  spread(Sex, value) %>%
  group_by(variable) %>%
  mutate( p_Female = t.test(unlist(Female))$p.value,
          p_Male   = t.test(unlist(Male)  )$p.value,
          t_Female = t.test(unlist(Female))$statistic,
          t_Male   = t.test(unlist(Male)  )$statistic) %>%
  mutate( Female = length(unlist(Female)),
          Male   = length(unlist(Male))
  )
# A tibble: 2 x 7
# Groups:   variable [2]
  variable Female  Male  p_Female p__Male t_Female t_Male
  <chr>     <int> <int>     <dbl>   <dbl>    <dbl>  <dbl>
1 Q1            8     4 0.0000501 0.00137     8.78  11.6 
2 Q2            8     4 0.00217   0.0115      4.71   5.55
df %>%
  filter(Answer_Date == First_Answer_Date) %>%
  select(questions, Sex) %>%
  filter(Sex != "No_Response") %>%

    # A tibble: 3 x 3
         Q1    Q2 Sex   
      <int> <int> <chr> 
    1     9     5 Female
    2     2     5 Female
    3     1     9 Male 
df %>%
  filter(Answer_Date == First_Answer_Date) %>%
  select(questions, Sex) %>%
  filter(Sex != "No_Response") %>%
  gather(key = variable, value = value, -Sex) %>%
  group_by(Sex, variable) %>%
  summarize(value = list(value)) %>%
  spread(Sex, value) %>%
  group_by(variable) %>%
  mutate( p_Female = t.test(unlist(Female))$p.value,
          p__Male = t.test(unlist(Male))$p.value,
          t_Female = t.test(unlist(Female))$statistic,
          t_Male = t.test(unlist(Male))$statistic) %>%
  mutate( Female = length(unlist(Female)),
          Male   = length(unlist(Male)))

Error: Problem with `mutate()` input `p_Female`.
x data are essentially constant
i Input `p_Female` is `t.test(unlist(Female))$p.value`.
i The error occurred in group 2: variable = "Q2".
library(dplyr)
library(tidyr)

df %>%
  filter(Answer_Date == First_Answer_Date) %>%
  select(questions, Sex) %>%
  filter(Sex != "No_Response") %>%
  pivot_longer(cols = -Sex, names_to = "variable") %>%
  group_by(Sex, variable) %>%
  summarize(value = list(value)) %>%
  pivot_wider(names_from = Sex, values_from = value) %>%
  group_by(variable) %>%
  mutate( p_Female = tryCatch(t.test(unlist(Female))$p.value, error = function(e) return(NA)),
          p_Male   = tryCatch(t.test(unlist(Male) )$p.value, error = function(e) return(NA)),
          t_Female = tryCatch(t.test(unlist(Female))$statistic, error = function(e) return(NA)),
          t_Male   = tryCatch(t.test(unlist(Male))$statistic,error = function(e) return(NA))) %>%
  ungroup %>%
  mutate( Female = lengths(Female),
          Male   = lengths(Male))