0

This maybe sounds a bit simple, but I cannot get the answer.

I have a dataset in R that has 26 samples in rows and many variables (>20) in columns. Some of them are categorical, so what I need to do is to carry out a Kruskal Wallis test for each numerical variable depending on each categorical one, so I do:

env_fact <- read.csv("environ_facts.csv")

kruskal.test(env_fact-1 ~ Categorical_var-1,  data=env_fact)

But with this I can only do the test to the numerical variables one by one, which is tiresome.

Is there any way to carry all the Kruskal-Wallis tests for all numerical variables at once? I can repeat it by each categorical variable, since I only have 4, but for the numerical one I have more than 20!!

Thanks a lot

  • we will need to have a sample of the data set. Please use `dput` on the data frame to upload the sample. Thanks! – Omry Atia Jul 29 '19 at 15:33

2 Answers2

0

Since I do not have sample of the data set I can only answer "theoretically".

First, you need to recognize which are the numeric columns. The way to do this is the following:

df = tibble(x = rnorm(10), y = rnorm(10), z = "a", w = rnorm(10))
NumericCols = sapply(df, function(x) is.numeric(x))
df_Numeric = df[, Types == TRUE]

Now you take the numeric part of df, df_Numeric, and apply your function blabla on each column at a time:

sapply(df_Numeric, function(x) blabla(x))
Omry Atia
  • 2,411
  • 2
  • 14
  • 27
0

Thank you very much Omry.

Working with a colleague we reached an incomplete different solution to yours:

my.variables <- colnames(env_fact)
for(i in 1:length(my.variables)) {
    if(my.variables[i] == 'Categorical_var') {
        next
    } else {
        kruskal.test(env_fact[,i], env_fact$Categorical_var)
    }
}

However, we haven't been able to print on screen/get an output with the results for each of 'my.variables' by the 'Categorical_var' analyzed. We could only get a result for all the 'my.variables' as a whole.

Any idea??

Thank you very much

P.S.: My data looks like this:

Sample,Nunatak,Slope,Altitude,Depth,Fluoride,Acetate,Formiate,Chloride,Nitrate
m4,1,1,1,1,0.044,0.884,0.522,0.198,0.021
m6,1,1,1,2,0.059,0.852,0.733,0.664,0.038
m7,1,1,1,3,0.082,0.339,1.496,0.592,0.034
m8,1,1,2,1,0.112,0.812,2.709,0.357,0.014
m10,1,1,2,2,0.088,0.768,2.535,0.379,0
m11,1,1,3,1,0.101,0.336,4.504,0.229,0
m13,1,1,3,2,0.092,0.681,1.862,0.671,0.018
m14,1,2,2,1,0.12,1.055,3.018,0.771,0
m16,1,2,2,2,0.102,1.019,1.679,1.435,0
m17,1,2,2,3,0.26,0.631,0.505,0.574,0.008

Where Nunatak, Slope, Altitude and Depth are categorical and the rest are numerical. Hope this helps