Question: R- Kruskal-Wallis test on multiple columns at once
0
mafernandez0 wrote:

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

modified 5 months ago • written 5 months ago by mafernandez0
2

I don't see bioinformatic relevance, but might this help?

A Tutorial on Loops in R - Usage and Alternatives

Correct for multiple testing after this

1

Hi, just as a side note, your problem might be "under-defined" to estimate variable importance because you have approximately the same number of observations and variables, 26 samples and ">20 variables". I am not sure if your statistics will be robust enough to draw reliable conclusions.

Thanks Michael

If you try to get all your variables at once the result may be not robust enough. However, what I want to do is to test each variable separately, so we will have one variable and 26 observations, classified by other different parameters into groups (3 or more groups, thus Kruskal-Wallis is needed instead of Mann-Whitney).

My problem is to find a command that allows me to do this for each variable at once, not to write the same code for every one (i.e. writing the same code 20 times).

Regards

As already suggested this seems like it can be solved with a simple for loop or a vectorized equivalent. If not then you'll have to explain why.

As you can presume, I am very new at programming, so I do not know how to creat a for loop with my data.

Let's say, I have variables (both numerical and categorical) in columns and samples in rows. Then I have to test each numeric variable based on each categorical one with the samples as the observations.

Then, I should try something like:

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

for i in env_fact
kruskal.test(i ~ Categorical_var-1,  data=env_fact)
``````

Right??

1

Almost. So this is an R programming question. Have a look at the tutorial linked above and/or any tutorial on R programming to get started. You need to know how to access variables in an R data frame. Look at something like this (not using the formula version of the function and assuming variables have names in column headers):

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

Sounds Great! However, I still cannot see all the Kruskal-Wallis tests.

If I try something similar to:

``````KW_test<-kruskal.test(env_fact[,i], env_fact\$Categorical_var)
print(KW_test)
``````

I only get a p-value, that I suppose, correspond to the test carried out as a whole and not variable per variable...

How could I get all the tests (i.e. each variable separately) on screen?

Thank you very much, your help is really carrying my analysis off!

I think you need to go and read about how to program in R to understand and reuse pieces of code. At this stage, I think it would be a disservice to code it entirely for you.

Thank you very much Jean-Karim.

I will try to figure out how to finish the analysis in the way I need. As you see, I am very new to R, so within your code I could understand almost everything what you've written, but I won't be able to write it not even in a million years ;-)

Cheers

Thanks, @carambakaracho

Yes, I did not point out the bioinformatic point of view of the topic.

I need this analysis to be carried out on a microbial ecology study to figure out what environmental factors have any influence (or not) on the community composition. I thought my doubt could be shared by anyone else.

Nonetheless, I am going to try what you suggested and post the result afterwards.

Cheers
{"ft_lang_label":"__label__en","ft_lang_prob":0.9201252,"math_prob":0.5984334,"size":3989,"snap":"2019-51-2020-05","text_gpt3_token_len":930,"char_repetition_ratio":0.10790464,"word_repetition_ratio":0.008344923,"special_character_ratio":0.2246177,"punctuation_ratio":0.10403917,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97279197,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-24T13:47:04Z\",\"WARC-Record-ID\":\"<urn:uuid:1f08dfce-01fe-46d3-9a3c-334c49103ec5>\",\"Content-Length\":\"38379\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2934df09-0c71-4127-8327-7632f4b80b5a>\",\"WARC-Concurrent-To\":\"<urn:uuid:13d87445-03c1-4d18-9e8b-1515742118e6>\",\"WARC-IP-Address\":\"69.164.220.180\",\"WARC-Target-URI\":\"https://www.biostars.org/p/392033/\",\"WARC-Payload-Digest\":\"sha1:QNXNSSOCQEHXWG2SS7F2LKESGJQTHQEH\",\"WARC-Block-Digest\":\"sha1:HAO4UAU2AY4QNSMGBF6XMOAHIEIWHQYH\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250620381.59_warc_CC-MAIN-20200124130719-20200124155719-00179.warc.gz\"}"}