Hi everyone, It has been a couple months now of using R and I am slowly trying to switch from using Statistica to R in order to process my data (for PhD purposes). I did some basic statistics and now I would like to try Kruskal-Wallis (following this guideline: http://www.sthda.com/english/wiki/kruskal-wallis-test-in-r ) but I have been stuck for days as I can't seem to find a reason why the levels function keeps giving me NULL. My data is a data frame: my_data Temperature Sampling 1 9.90 Dec17 2 10.00 Dec17 3 10.00 Dec17 4 10.10 Dec17 5 9.90 Dec17 6 10.10 Dec17 7 10.20 Dec17 8 10.10 Jan18 9 10.50 Jan18 10 10.80 Jan18 11 10.30 Jan18 12 10.30 Jan18 13 10.20 Jan18 14 10.50 Jan18 15 20.90 Jun18 16 20.80 Jun18 17 20.50 Jun18 18 20.50 Jun18 19 20.70 Jun18 20 20.60 Jun18 21 20.50 Jun18 22 10.50 Mar18 23 11.40 Mar18 24 10.90 Mar18 25 10.60 Mar18 26 11.60 Mar18 27 11.20 Mar18 28 12.20 Mar18 29 19.40 May18 30 19.00 May18 31 20.80 May18 32 19.50 May18 33 18.60 May18 34 18.10 May18 35 19.20 May18 36 16.80 Nov18 37 16.50 Nov18 38 18.40 Nov18 39 18.10 Nov18 40 16.70 Nov18 41 16.70 Nov18 42 18.50 Nov18 43 15.50 Oct17 44 15.10 Oct17 45 15.00 Oct17 46 15.80 Oct17 47 15.20 Oct17 48 15.10 Oct17 49 15.20 Oct17 50 23.40 Sep18 51 24.05 Sep18 52 23.70 Sep18 53 23.40 Sep18 54 24.05 Sep18 55 23.80 Sep18 56 24.15 Sep18 When I hit levels(my_data$Sampling) it gives me NULL I would very much appreciate any help. Kind regards, Vesna Vesna Gulin, Research Assistant Department of Biology Faculty of Science University of Zagreb Rooseveltov trg 6, 10000 Zagreb, Croatia
LEVELS function NULL
5 messages · Torsten Hauffe, Peter Solymos, Vesna Gulin +1 more
Hi, What is the output of class(my_data$Sampling)? If it is not factor (but character), you could write my_data$Sampling <- as.factor(my_data$Sampling) Then levels(my_data$Sampling) should return Sep18 etc. Cheers!
On Tue, Jun 23, 2020 at 6:00 PM Vesna Gulin <vesna.gulin at biol.pmf.hr> wrote:
Hi everyone, It has been a couple months now of using R and I am slowly trying to switch from using Statistica to R in order to process my data (for PhD purposes). I did some basic statistics and now I would like to try Kruskal-Wallis (following this guideline: http://www.sthda.com/english/wiki/kruskal-wallis-test-in-r ) but I have been stuck for days as I can't seem to find a reason why the levels function keeps giving me NULL. My data is a data frame: my_data Temperature Sampling 1 9.90 Dec17 2 10.00 Dec17 3 10.00 Dec17 4 10.10 Dec17 5 9.90 Dec17 6 10.10 Dec17 7 10.20 Dec17 8 10.10 Jan18 9 10.50 Jan18 10 10.80 Jan18 11 10.30 Jan18 12 10.30 Jan18 13 10.20 Jan18 14 10.50 Jan18 15 20.90 Jun18 16 20.80 Jun18 17 20.50 Jun18 18 20.50 Jun18 19 20.70 Jun18 20 20.60 Jun18 21 20.50 Jun18 22 10.50 Mar18 23 11.40 Mar18 24 10.90 Mar18 25 10.60 Mar18 26 11.60 Mar18 27 11.20 Mar18 28 12.20 Mar18 29 19.40 May18 30 19.00 May18 31 20.80 May18 32 19.50 May18 33 18.60 May18 34 18.10 May18 35 19.20 May18 36 16.80 Nov18 37 16.50 Nov18 38 18.40 Nov18 39 18.10 Nov18 40 16.70 Nov18 41 16.70 Nov18 42 18.50 Nov18 43 15.50 Oct17 44 15.10 Oct17 45 15.00 Oct17 46 15.80 Oct17 47 15.20 Oct17 48 15.10 Oct17 49 15.20 Oct17 50 23.40 Sep18 51 24.05 Sep18 52 23.70 Sep18 53 23.40 Sep18 54 24.05 Sep18 55 23.80 Sep18 56 24.15 Sep18 When I hit levels(my_data$Sampling) it gives me NULL I would very much appreciate any help. Kind regards, Vesna Vesna Gulin, Research Assistant Department of Biology Faculty of Science University of Zagreb Rooseveltov trg 6, 10000 Zagreb, Croatia [[alternative HTML version deleted]]
_______________________________________________ R-sig-ecology mailing list R-sig-ecology at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
R >= 4.0 will treat categorical variables as character (stringsAsFactors=FALSE is the new default) when importing via e.g. read.table(). If you want factors, you have to make it explicit either as Torsten showed or by setting stringsAsFactors=TRUE. Cheers, Peter On Tue, Jun 23, 2020 at 10:05 AM Torsten Hauffe <torsten.hauffe at gmail.com> wrote:
Hi, What is the output of class(my_data$Sampling)? If it is not factor (but character), you could write my_data$Sampling <- as.factor(my_data$Sampling) Then levels(my_data$Sampling) should return Sep18 etc. Cheers! On Tue, Jun 23, 2020 at 6:00 PM Vesna Gulin <vesna.gulin at biol.pmf.hr> wrote:
Hi everyone, It has been a couple months now of using R and I am slowly trying to
switch
from using Statistica to R in order to process my data (for PhD
purposes).
I did some basic statistics and now I would like to try Kruskal-Wallis (following this guideline: http://www.sthda.com/english/wiki/kruskal-wallis-test-in-r ) but I have been stuck for days as I can't seem to find a reason why the levels function keeps giving me NULL. My data is a data frame: my_data Temperature Sampling 1 9.90 Dec17 2 10.00 Dec17 3 10.00 Dec17 4 10.10 Dec17 5 9.90 Dec17 6 10.10 Dec17 7 10.20 Dec17 8 10.10 Jan18 9 10.50 Jan18 10 10.80 Jan18 11 10.30 Jan18 12 10.30 Jan18 13 10.20 Jan18 14 10.50 Jan18 15 20.90 Jun18 16 20.80 Jun18 17 20.50 Jun18 18 20.50 Jun18 19 20.70 Jun18 20 20.60 Jun18 21 20.50 Jun18 22 10.50 Mar18 23 11.40 Mar18 24 10.90 Mar18 25 10.60 Mar18 26 11.60 Mar18 27 11.20 Mar18 28 12.20 Mar18 29 19.40 May18 30 19.00 May18 31 20.80 May18 32 19.50 May18 33 18.60 May18 34 18.10 May18 35 19.20 May18 36 16.80 Nov18 37 16.50 Nov18 38 18.40 Nov18 39 18.10 Nov18 40 16.70 Nov18 41 16.70 Nov18 42 18.50 Nov18 43 15.50 Oct17 44 15.10 Oct17 45 15.00 Oct17 46 15.80 Oct17 47 15.20 Oct17 48 15.10 Oct17 49 15.20 Oct17 50 23.40 Sep18 51 24.05 Sep18 52 23.70 Sep18 53 23.40 Sep18 54 24.05 Sep18 55 23.80 Sep18 56 24.15 Sep18 When I hit levels(my_data$Sampling) it gives me NULL I would very much appreciate any help. Kind regards, Vesna Vesna Gulin, Research Assistant Department of Biology Faculty of Science University of Zagreb Rooseveltov trg 6, 10000 Zagreb, Croatia [[alternative HTML version deleted]]
_______________________________________________ R-sig-ecology mailing list R-sig-ecology at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
[[alternative HTML version deleted]]
_______________________________________________ R-sig-ecology mailing list R-sig-ecology at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
P?ter S?lymos, PhD Senior Statistical Ecologist solymos at ualberta.ca 780-492-8534 Alberta Biodiversity Monitoring Institute http://www.abmi.ca Boreal Avian Modelling Project http://www.borealbirds.ca [[alternative HTML version deleted]]
Hi, Thank you! It was indeed a character :) I used my_data$Sampling <- as.factor(my_data$Sampling) and now it works:
levels(my_data$Sampling)
[1] "Dec17" "Jan18" "Jun18" "Mar18" "May18" "Nov18" "Oct17" "Sep18" Kr, Vesna From: Torsten Hauffe <torsten.hauffe at gmail.com> Sent: Tuesday, June 23, 2020 6:06 PM To: Vesna Gulin <vesna.gulin at biol.pmf.hr> Cc: r-sig-ecology at r-project.org Subject: Re: [R-sig-eco] LEVELS function NULL Hi, What is the output of class(my_data$Sampling)? If it is not factor (but character), you could write my_data$Sampling <- as.factor(my_data$Sampling) Then levels(my_data$Sampling) should return Sep18 etc. Cheers!
On Tue, Jun 23, 2020 at 6:00 PM Vesna Gulin <vesna.gulin at biol.pmf.hr <mailto:vesna.gulin at biol.pmf.hr> > wrote:
Hi everyone, It has been a couple months now of using R and I am slowly trying to switch from using Statistica to R in order to process my data (for PhD purposes). I did some basic statistics and now I would like to try Kruskal-Wallis (following this guideline: http://www.sthda.com/english/wiki/kruskal-wallis-test-in-r ) but I have been stuck for days as I can't seem to find a reason why the levels function keeps giving me NULL. My data is a data frame: my_data Temperature Sampling 1 9.90 Dec17 2 10.00 Dec17 3 10.00 Dec17 4 10.10 Dec17 5 9.90 Dec17 6 10.10 Dec17 7 10.20 Dec17 8 10.10 Jan18 9 10.50 Jan18 10 10.80 Jan18 11 10.30 Jan18 12 10.30 Jan18 13 10.20 Jan18 14 10.50 Jan18 15 20.90 Jun18 16 20.80 Jun18 17 20.50 Jun18 18 20.50 Jun18 19 20.70 Jun18 20 20.60 Jun18 21 20.50 Jun18 22 10.50 Mar18 23 11.40 Mar18 24 10.90 Mar18 25 10.60 Mar18 26 11.60 Mar18 27 11.20 Mar18 28 12.20 Mar18 29 19.40 May18 30 19.00 May18 31 20.80 May18 32 19.50 May18 33 18.60 May18 34 18.10 May18 35 19.20 May18 36 16.80 Nov18 37 16.50 Nov18 38 18.40 Nov18 39 18.10 Nov18 40 16.70 Nov18 41 16.70 Nov18 42 18.50 Nov18 43 15.50 Oct17 44 15.10 Oct17 45 15.00 Oct17 46 15.80 Oct17 47 15.20 Oct17 48 15.10 Oct17 49 15.20 Oct17 50 23.40 Sep18 51 24.05 Sep18 52 23.70 Sep18 53 23.40 Sep18 54 24.05 Sep18 55 23.80 Sep18 56 24.15 Sep18 When I hit levels(my_data$Sampling) it gives me NULL I would very much appreciate any help. Kind regards, Vesna Vesna Gulin, Research Assistant Department of Biology Faculty of Science University of Zagreb Rooseveltov trg 6, 10000 Zagreb, Croatia _______________________________________________ R-sig-ecology mailing list R-sig-ecology at r-project.org <mailto:R-sig-ecology at r-project.org> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
Notice that the levels are ordered alphabetically. Use factor() and it level argument to get them in a more sensible order. Best regards, ir. Thierry Onkelinx Statisticus / Statistician Vlaamse Overheid / Government of Flanders INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND FOREST Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance thierry.onkelinx at inbo.be Havenlaan 88 bus 73, 1000 Brussel www.inbo.be /////////////////////////////////////////////////////////////////////////////////////////// To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey /////////////////////////////////////////////////////////////////////////////////////////// <https://www.inbo.be> Op di 23 jun. 2020 om 18:12 schreef Vesna Gulin <vesna.gulin at biol.pmf.hr>:
Hi, Thank you! It was indeed a character :) I used my_data$Sampling <- as.factor(my_data$Sampling) and now it works:
levels(my_data$Sampling)
[1] "Dec17" "Jan18" "Jun18" "Mar18" "May18" "Nov18" "Oct17" "Sep18" Kr, Vesna From: Torsten Hauffe <torsten.hauffe at gmail.com> Sent: Tuesday, June 23, 2020 6:06 PM To: Vesna Gulin <vesna.gulin at biol.pmf.hr> Cc: r-sig-ecology at r-project.org Subject: Re: [R-sig-eco] LEVELS function NULL Hi, What is the output of class(my_data$Sampling)? If it is not factor (but character), you could write my_data$Sampling <- as.factor(my_data$Sampling) Then levels(my_data$Sampling) should return Sep18 etc. Cheers! On Tue, Jun 23, 2020 at 6:00 PM Vesna Gulin <vesna.gulin at biol.pmf.hr <mailto:vesna.gulin at biol.pmf.hr> > wrote: Hi everyone, It has been a couple months now of using R and I am slowly trying to switch from using Statistica to R in order to process my data (for PhD purposes). I did some basic statistics and now I would like to try Kruskal-Wallis (following this guideline: http://www.sthda.com/english/wiki/kruskal-wallis-test-in-r ) but I have been stuck for days as I can't seem to find a reason why the levels function keeps giving me NULL. My data is a data frame: my_data Temperature Sampling 1 9.90 Dec17 2 10.00 Dec17 3 10.00 Dec17 4 10.10 Dec17 5 9.90 Dec17 6 10.10 Dec17 7 10.20 Dec17 8 10.10 Jan18 9 10.50 Jan18 10 10.80 Jan18 11 10.30 Jan18 12 10.30 Jan18 13 10.20 Jan18 14 10.50 Jan18 15 20.90 Jun18 16 20.80 Jun18 17 20.50 Jun18 18 20.50 Jun18 19 20.70 Jun18 20 20.60 Jun18 21 20.50 Jun18 22 10.50 Mar18 23 11.40 Mar18 24 10.90 Mar18 25 10.60 Mar18 26 11.60 Mar18 27 11.20 Mar18 28 12.20 Mar18 29 19.40 May18 30 19.00 May18 31 20.80 May18 32 19.50 May18 33 18.60 May18 34 18.10 May18 35 19.20 May18 36 16.80 Nov18 37 16.50 Nov18 38 18.40 Nov18 39 18.10 Nov18 40 16.70 Nov18 41 16.70 Nov18 42 18.50 Nov18 43 15.50 Oct17 44 15.10 Oct17 45 15.00 Oct17 46 15.80 Oct17 47 15.20 Oct17 48 15.10 Oct17 49 15.20 Oct17 50 23.40 Sep18 51 24.05 Sep18 52 23.70 Sep18 53 23.40 Sep18 54 24.05 Sep18 55 23.80 Sep18 56 24.15 Sep18 When I hit levels(my_data$Sampling) it gives me NULL I would very much appreciate any help. Kind regards, Vesna Vesna Gulin, Research Assistant Department of Biology Faculty of Science University of Zagreb Rooseveltov trg 6, 10000 Zagreb, Croatia [[alternative HTML version deleted]]
_______________________________________________ R-sig-ecology mailing list R-sig-ecology at r-project.org <mailto:R-sig-ecology at r-project.org> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology [[alternative HTML version deleted]] _______________________________________________ R-sig-ecology mailing list R-sig-ecology at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology