0

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))

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)
 })

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
user
  • 592
  • 6
  • 26
  • 1
    To apply it for every row you can do `mapply(function(x, y) wilcox.test(x, y)$p.value, df$md, df$og)` but is this what you want? – Ronak Shah Oct 02 '19 at 04:07
  • how can I get a new column name p.value in the function that you mentioned – user Oct 02 '19 at 21:45
  • I have used the following approach to rename it but do you have better option:df1<-data.frame(mapply(function(x, y) wilcox.test(x, y)$p.value, df$mean_md, df$mean_od)) colnames(df1)[1]="p_value" df2<- cbind(df,df1) – user Oct 02 '19 at 22:28

2 Answers2

1

I am trying to learn apply myself. It seems you wanted to compare md and og for each group or country. There are other (better) solutions for this. Here is an exercise of using apply family to compare md and og for each country:

results <- sapply(levels(df$country), function(x){
  df <- subset(df, country== x)
  wilcox.test(df$md,df$og)$p.value})
results

You will get something like:

> results
       UK        US 
0.8412698 0.1507937 
Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27
1

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)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213