There are two issues here. First, your original factor seems to have 4 levels: " F", " M", "F", "M". Note the extra space in front of the first two F and M. You may want to fix that first:
gender.fixed = sub(" ", "", as.character(data$gender)) Check that everything is correct by typing table(gender.fixed) or table(data$gender, gender.fixed) Then you can convert the fixed gender back to a factor, but pay attention to the levels: data$gender = factor(gender.fixed, levels = c("F", "M")) Hopefully this works, Peter On Sat, Jul 11, 2015 at 12:21 PM, Dagmar Juranková <dagmar.jura...@gmail.com> wrote: > Hello everybody, I have a problem with R. > > > I uploaded a questionnaire saved as csv into R and I tried to test > independence between two variables. > > > > data <- read.csv("C:/Users/Me/Desktop/data.csv")> View(data)> df = > read.csv("C:/Users/Me/Desktop/data.csv")> ls() > [1] "df" "data"> attributes(data$gender) > $levels > [1] " F" " M" "F" "M" > > $class > [1] "factor" > > > I changed my variable "gender" into a factor using: > > > data$gender=factor(data$gender, levels=c(1:2), labels= c( "F", "M"), > exclude= NA, nmax= NA). > > > Then I wrote data$gender and the only thing i got was: > > > [1] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> > <NA> <NA> <NA> <NA> <NA> <NA> > > [21] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> > <NA> <NA> <NA> <NA> <NA> <NA> > > [41] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> > <NA> <NA> <NA> <NA> <NA> <NA> > > [61] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> > > Levels: F M > > > Does anybody know why? > > > -My csv doc in the column gender is filled out properly. (M=Male, F= Female) > > -My imported dataset in R is complete (all values) > > > ! I have done this with a different excel document and it worked out > without any problems. I am really clueless. I cant go further and compare > the variables and do t-tests without this working. > > > Could someone please help me out? > > Thank you. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.