I think it makes a difference if I want to use a classification method like rpart () or if I use a modelling approach like glm().
Many thanks for the kind and fast help. I am still very untrained and it is difficult for me to create such codes. B. Eik Vettorazzi wrote: > > Ok, then treat them as factors - but if they are really binary and coded > 0 and 1, which kind of calculation would lead to different results for a > "factor" instead of a numeric variable? > > Anyway, > > ABC<-as.data.frame(cbind(A,B,C)) > aggregate(ABC[,2:3],by=list(A),FUN=function(x)sum(x=='1')) # '1' is > here the level of the corresponding factor to be counted. > > will work. A more sophisticated version could include some "factor to > numeric" conversion, see FAQ 7.10. > hth. > > Birgitle schrieb: >> Thanks for your answer. >> >> It is intended, that the variables are treated as class factor, because >> these are binary variables with, for example, the presence or the absence >> of >> a plant organ. >> As far as I understood, I have to treat them for other calculations as >> factor. Therefore I classified these variables as factors in my original >> dataframe. >> >> If I do what you suggested, I have to change the classes of all my >> variables. Is there a possibility to do something similar without >> changing >> the classes? >> >> B. >> >> >> Eik Vettorazzi wrote: >> >>> First of all your construction of ABC leads to a structure with 3 factor >>> variables due to the way cbind processes the input variables - which is >>> not intended I think. >>> >>> You can do sth like >>> >>> ABC<-data.frame(A,B,C) >>> aggregate(ABC[,2:3],by=list(A),sum) >>> >>> hth. >>> >>> Birgitle schrieb: >>> >>>> Hello R-Users! >>>> >>>> I need a little help to build up a contingency table out of several >>>> variables. >>>> >>>> A<-c("F","M","M","F","F","F","F","M","F","M","F","F") >>>> B<-c(0,0,0,0,0,0,1,1,1,1,0,1) >>>> C<-c(0,1,1,1,1,1,1,1,1,0,0,0) >>>> >>>> ABC<-as.data.frame(cbind(A,B,C)) >>>> >>>> ABC >>>> >>>> A B C >>>> 1 F 0 0 >>>> 2 M 0 1 >>>> 3 M 0 1 >>>> 4 F 0 1 >>>> 5 F 0 1 >>>> 6 F 0 1 >>>> 7 F 1 1 >>>> 8 M 1 1 >>>> 9 F 1 1 >>>> 10 M 1 0 >>>> 11 F 0 0 >>>> 12 F 1 0 >>>> >>>> I would like to count in each variable B and C the frequencies for M >>>> and >>>> F >>>> (variable A) and finally get the following table: >>>> >>>> B C >>>> F 3 5 >>>> >>>> M 2 3 >>>> >>>> Is there a function that can do that in one step? >>>> Tried ?structable, ?ftable, ?xtabs, ?table but could not get what I >>>> would >>>> like to have. >>>> >>>> Maybe I did not use the tried functions in the right way. >>>> >>>> Many thanks in advance for any help. >>>> >>>> B. >>>> >>>> >>>> >>>> ----- >>>> The art of living is more like wrestling than dancing. >>>> (Marcus Aurelius) >>>> >>>> >>> -- >>> Eik Vettorazzi >>> Institut für Medizinische Biometrie und Epidemiologie >>> Universitätsklinikum Hamburg-Eppendorf >>> >>> Martinistr. 52 >>> 20246 Hamburg >>> >>> T ++49/40/42803-8243 >>> F ++49/40/42803-7790 >>> >>> ______________________________________________ >>> R-help@r-project.org mailing list >>> https://stat.ethz.ch/mailman/listinfo/r-help >>> PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html >>> and provide commented, minimal, self-contained, reproducible code. >>> >>> >>> >> >> >> ----- >> The art of living is more like wrestling than dancing. >> (Marcus Aurelius) >> > > -- > Eik Vettorazzi > Institut für Medizinische Biometrie und Epidemiologie > Universitätsklinikum Hamburg-Eppendorf > > Martinistr. 52 > 20246 Hamburg > > T ++49/40/42803-8243 > F ++49/40/42803-7790 > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > ----- The art of living is more like wrestling than dancing. (Marcus Aurelius) -- View this message in context: http://www.nabble.com/contingency-table%2C-several-variables-from-dataframe-tp19775083p19778153.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.