为每组中的每一行运行一个 wilcox 函数 [英] Run a wilcox function for each row in each group

查看:19
本文介绍了为每组中的每一行运行一个 wilcox 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的数据框:

I have a dataframe as follows:

df <- data.frame(group = c("A", "B", "C", "D", "E"), 
                 country=c("US","UK"),
                 md = runif(10,0,10), 
                 og = runif(10, 0, 10))

并希望在每一行中应用 wilcox 函数来比较每个组和每个国家的 md 和 og.

and want to apply wilcox function in each row to compare md and og in each group and each country.

results <- apply(df,1,function(x){
 df <- data.frame(x)
 wres<-wilcox.test(df$md,df$og)
 df$test<-format(wres$p.value,scientific = F)
 })

我想要另一列包含 P 值.但是当我运行它时,它给了我以下错误:

I want to have another column consists of P-value. but when I run it it gives me the following error:

Error in wilcox.test.default(df$mean_modified, df$mean_original) : 
  'x' must be numeric

推荐答案

我们可以使用 mapply 对每个值应用 wilcox.test 然后提取 p.value 来自它

We can use mapply to apply wilcox.test for every value and then extract p.value from it

df$p.value <- mapply(function(x, y) wilcox.test(x, y)$p.value, df$md, df$og)

这篇关于为每组中的每一行运行一个 wilcox 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆