Dear everyone I am a bit of a computer imbecile and are having problems with R. I am using R in my research project to do chi-square tests on data imported from excel . However I have som missing data in one of my variables (columns) and I need R to "exclude" these and make chi-square test on the data that I have. I use a formula to make 2x2 tables which is: data <- matrix(c(sum(!Variable[Group....==1]), sum(Variable[DAAC....==1]), sum(!Variable[Group....==0]), sum(Variable[DAAC....==0])),2,2) How can I get R to ignore Na's in this formula? Many Regards -- View this message in context: http://r.789695.n4.nabble.com/Missing-data-Na-and-chi-square-tests-tp4645167.html Sent from the R help mailing list archive at Nabble.com.
Hello, There are two ways, 1. ?sum # see argument na.rm sum(whatever, na.rm = TRUE) 2. ?table # produces the 2x2 contingency table, if there are only 2 values Also, you should provide us with a data example, especially since your code clearly doesn't work. Use ?dput like this dput( head(MyData, 20) ) # Then paste the output of this in a post. Hope this helps, Rui Barradas Em 05-10-2012 14:26, Rerda escreveu:> Dear everyone > > I am a bit of a computer imbecile and are having problems with R. > I am using R in my research project to do chi-square tests on data imported > from excel . > However I have som missing data in one of my variables (columns) and I need > R to "exclude" these and make chi-square test on the data that I have. > > I use a formula to make 2x2 tables which is: > data <- matrix(c(sum(!Variable[Group....==1]), sum(Variable[DAAC....==1]), > sum(!Variable[Group....==0]), sum(Variable[DAAC....==0])),2,2) > > How can I get R to ignore Na's in this formula? > > Many Regards > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Missing-data-Na-and-chi-square-tests-tp4645167.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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.
On Oct 5, 2012, at 6:26 AM, Rerda wrote:> Dear everyone > > I am a bit of a computer imbecile and are having problems with R. > I am using R in my research project to do chi-square tests on data imported > from excel . > However I have som missing data in one of my variables (columns) and I need > R to "exclude" these and make chi-square test on the data that I have. > > I use a formula to make 2x2 tables which is: > data <- matrix(c(sum(!Variable[Group....==1]), sum(Variable[DAAC....==1]), > sum(!Variable[Group....==0]), sum(Variable[DAAC....==0])),2,2)This might be as simple as: with(Variable, table( Grp.1= Group == 1, DAAC.1= DAAC == 1) ) table will produce an object that inherits from matrix and the counts of logical values will get cross tabulated. The NA values will neither be equal to 1 nor not-equal to 1, but will rather be ...NA. So they will not appear in the counts. This, of course, depends on my guess regarding you data structure being correct and my guess regarding you goals also beingcorrect. Neither of those critical bits of information have yet been offered. -- David.> > How can I get R to ignore Na's in this formula? > > Many Regards > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Missing-data-Na-and-chi-square-tests-tp4645167.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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.David Winsemius, MD Alameda, CA, USA
Dear Rui and David Thank you very much for taking your time to look at my problem. However, I still cannot seem to figure it out. I think that you David are corect in your assumption of how my data is structured. The data in the two columns that I need to cross-table is either 1 or 0. I made a mistanke in the formula that I sent to you. The one I use is: data <- matrix(c(sum(!Variable[Group....==1]), sum(Variable[Group....==1]), sum(!Variable[Group....==0]), sum(Variable[Group....==0])),2,2) This is the output of >dput( head(MyData, 20) ): structure(list(Patient.nr = c(1L, 3L, 4L, 5L, 6L, 7L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 19L, 20L, 21L, 22L, 23L, 24L), DAAC.... = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), TAA = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Sex = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L), Alder = c(1L, 6L, 3L, 3L, 6L, 6L, 2L, 6L, 5L, 2L, 6L, 4L, 6L, 2L, 2L, 5L, 3L, 6L, 6L, 6L), Reak.1 = structure(c(2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L), .Label = c("0", "1", "na"), class = "factor"), Reak.2 = structure(c(1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L), .Label = c("0", "2", "na"), class = "factor"), Reak.3 = structure(c(1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L), .Label = c("0", "3", "na"), class = "factor"), Reak.4 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", "4", "na"), class = "factor"), Tryptase = structure(c(1L, 1L, 1L, 3L, 1L, 3L, 1L, 2L, 2L, 1L, 3L, 3L, 1L, 3L, 3L, 3L, 3L, 3L, 2L, 1L), .Label = c("0", "1", "na"), class = "factor"), Hypotension = c(0L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L), Tachycardia = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L), Br.spasm. = c(0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Angioedema = c(0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L), Urticaria = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Flush. = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Rash = c(1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L), Pruritus = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L), Transf. = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("Patient.nr", "DAAC....", "TAA", "Sex", "Alder", "Reak.1", "Reak.2", "Reak.3", "Reak.4", "Tryptase", "Hypotension", "Tachycardia", "Br.spasm.", "Angioedema", "Urticaria", "Flush.", "Rash", "Pruritus", "Transf."), row.names = c(NA, 20L ), class = "data.frame") And I still can get it to work. Is it possible to put is.na(Variable) or something into my formula? I understand if it is to difficult to figure it put. Thank you very much Kind Regards Gerda -- View this message in context: http://r.789695.n4.nabble.com/Missing-data-Na-and-chi-square-tests-tp4645167p4645464.html Sent from the R help mailing list archive at Nabble.com.
On Oct 8, 2012, at 9:06 AM, Rerda wrote:> Dear Rui and David > Thank you very much for taking your time to look at my problem. > However, I still cannot seem to figure it out. > > I think that you David are corect in your assumption of how my data is > structured. The data in the two columns that I need to cross-table is either > 1 or 0. > I made a mistanke in the formula that I sent to you. The one I use is: > data <- matrix(c(sum(!Variable[Group....==1]), sum(Variable[Group....==1]), > sum(!Variable[Group....==0]), sum(Variable[Group....==0])),2,2)Hardly seems surprising that is not working, ... the is no "Group...." column in the data. Furthermore you would not need to use sum( !Variable[Group....==1] as well as sum(Variable[Group....==1]). The `table` function will do that for you. Given my personal memory of having a hypotensive episode as I stood at the counter of a pharmacy asking for epinephrine and a syringe to treat my urticarial reaction to shellfish, I picked these two:> with( MyData, table( Rash= as.logical(Rash), Hypotension = as.logical(Hypotension) ) )Hypotension Rash FALSE TRUE FALSE 3 7 TRUE 7 3 Best; David.> > > This is the output of >dput( head(MyData, 20) ): > > structure(list(Patient.nr = c(1L, 3L, 4L, 5L, 6L, 7L, 9L, 10L, > 11L, 12L, 13L, 14L, 15L, 16L, 19L, 20L, 21L, 22L, 23L, 24L), > DAAC.... = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, > 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), TAA = c(0L, 0L, 0L, > 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, > 0L, 0L), Sex = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, > 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L), Alder = c(1L, 6L, > 3L, 3L, 6L, 6L, 2L, 6L, 5L, 2L, 6L, 4L, 6L, 2L, 2L, 5L, 3L, > 6L, 6L, 6L), Reak.1 = structure(c(2L, 1L, 2L, 2L, 1L, 2L, > 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L), .Label = c("0", > "1", "na"), class = "factor"), Reak.2 = structure(c(1L, 1L, > 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, > 2L, 1L, 1L), .Label = c("0", "2", "na"), class = "factor"), > Reak.3 = structure(c(1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, > 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L), .Label = c("0", > "3", "na"), class = "factor"), Reak.4 = structure(c(1L, 1L, > 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, > 1L, 1L, 1L), .Label = c("0", "4", "na"), class = "factor"), > Tryptase = structure(c(1L, 1L, 1L, 3L, 1L, 3L, 1L, 2L, 2L, > 1L, 3L, 3L, 1L, 3L, 3L, 3L, 3L, 3L, 2L, 1L), .Label = c("0", > "1", "na"), class = "factor"), Hypotension = c(0L, 1L, 0L, > 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, > 1L, 0L), Tachycardia = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, > 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L), Br.spasm. = c(0L, > 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, > 0L, 0L, 0L, 0L), Angioedema = c(0L, 0L, 0L, 1L, 0L, 1L, 0L, > 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L), Urticaria = c(0L, > 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, > 0L, 0L, 0L, 0L), Flush. = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, > 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Rash = c(1L, > 0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, > 1L, 1L, 0L, 1L), Pruritus = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, > 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L), Transf. = c(0L, > 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, > 0L, 0L, 0L, 0L)), .Names = c("Patient.nr", "DAAC....", "TAA", > "Sex", "Alder", "Reak.1", "Reak.2", "Reak.3", "Reak.4", "Tryptase", > "Hypotension", "Tachycardia", "Br.spasm.", "Angioedema", "Urticaria", > "Flush.", "Rash", "Pruritus", "Transf."), row.names = c(NA, 20L > ), class = "data.frame") > > And I still can get it to work. > Is it possible to put is.na(Variable) or something into my formula? > > I understand if it is to difficult to figure it put. > Thank you very much > > Kind Regards Gerda > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Missing-data-Na-and-chi-square-tests-tp4645167p4645464.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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.David Winsemius, MD Alameda, CA, USA
Hello, Also, since the values are always 0/1, this should also do it. with( dat, table( Rash, Hypotension ) ) Hope this helps, Rui Barradas Em 09-10-2012 00:36, David Winsemius escreveu:> On Oct 8, 2012, at 9:06 AM, Rerda wrote: > >> Dear Rui and David >> Thank you very much for taking your time to look at my problem. >> However, I still cannot seem to figure it out. >> >> I think that you David are corect in your assumption of how my data is >> structured. The data in the two columns that I need to cross-table is either >> 1 or 0. >> I made a mistanke in the formula that I sent to you. The one I use is: >> data <- matrix(c(sum(!Variable[Group....==1]), sum(Variable[Group....==1]), >> sum(!Variable[Group....==0]), sum(Variable[Group....==0])),2,2) > Hardly seems surprising that is not working, ... the is no "Group...." column in the data. Furthermore you would not need to use sum( !Variable[Group....==1] as well as sum(Variable[Group....==1]). The `table` function will do that for you. > > Given my personal memory of having a hypotensive episode as I stood at the counter of a pharmacy asking for epinephrine and a syringe to treat my urticarial reaction to shellfish, I picked these two: > >> with( MyData, table( Rash= as.logical(Rash), Hypotension = as.logical(Hypotension) ) ) > Hypotension > Rash FALSE TRUE > FALSE 3 7 > TRUE 7 3 > > Best; > David. > > >> >> This is the output of >dput( head(MyData, 20) ): >> >> structure(list(Patient.nr = c(1L, 3L, 4L, 5L, 6L, 7L, 9L, 10L, >> 11L, 12L, 13L, 14L, 15L, 16L, 19L, 20L, 21L, 22L, 23L, 24L), >> DAAC.... = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, >> 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), TAA = c(0L, 0L, 0L, >> 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, >> 0L, 0L), Sex = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, >> 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L), Alder = c(1L, 6L, >> 3L, 3L, 6L, 6L, 2L, 6L, 5L, 2L, 6L, 4L, 6L, 2L, 2L, 5L, 3L, >> 6L, 6L, 6L), Reak.1 = structure(c(2L, 1L, 2L, 2L, 1L, 2L, >> 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L), .Label = c("0", >> "1", "na"), class = "factor"), Reak.2 = structure(c(1L, 1L, >> 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, >> 2L, 1L, 1L), .Label = c("0", "2", "na"), class = "factor"), >> Reak.3 = structure(c(1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, >> 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L), .Label = c("0", >> "3", "na"), class = "factor"), Reak.4 = structure(c(1L, 1L, >> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, >> 1L, 1L, 1L), .Label = c("0", "4", "na"), class = "factor"), >> Tryptase = structure(c(1L, 1L, 1L, 3L, 1L, 3L, 1L, 2L, 2L, >> 1L, 3L, 3L, 1L, 3L, 3L, 3L, 3L, 3L, 2L, 1L), .Label = c("0", >> "1", "na"), class = "factor"), Hypotension = c(0L, 1L, 0L, >> 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, >> 1L, 0L), Tachycardia = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, >> 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L), Br.spasm. = c(0L, >> 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, >> 0L, 0L, 0L, 0L), Angioedema = c(0L, 0L, 0L, 1L, 0L, 1L, 0L, >> 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L), Urticaria = c(0L, >> 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, >> 0L, 0L, 0L, 0L), Flush. = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, >> 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Rash = c(1L, >> 0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, >> 1L, 1L, 0L, 1L), Pruritus = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, >> 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L), Transf. = c(0L, >> 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, >> 0L, 0L, 0L, 0L)), .Names = c("Patient.nr", "DAAC....", "TAA", >> "Sex", "Alder", "Reak.1", "Reak.2", "Reak.3", "Reak.4", "Tryptase", >> "Hypotension", "Tachycardia", "Br.spasm.", "Angioedema", "Urticaria", >> "Flush.", "Rash", "Pruritus", "Transf."), row.names = c(NA, 20L >> ), class = "data.frame") >> >> And I still can get it to work. >> Is it possible to put is.na(Variable) or something into my formula? >> >> I understand if it is to difficult to figure it put. >> Thank you very much >> >> Kind Regards Gerda >> >> >> >> -- >> View this message in context: http://r.789695.n4.nabble.com/Missing-data-Na-and-chi-square-tests-tp4645167p4645464.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> R-help at 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. > David Winsemius, MD > Alameda, CA, USA > > ______________________________________________ > R-help at 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.
Possibly Parallel Threads
- [Bug 585] sshd core dumping on IRIX 6.5.18 with VerifyReverseMapping enabled
- [Bug 585] sshd core dumping on IRIX 6.5.18 with VerifyReverseMapping enabled
- [Bug 294] tcp wrapper access changed between 2.9.9p2 and 3.3p1
- Download data from NASA for multiple locations - RCurl
- Help resizing mages