[R] [FORGED] data$variable=factor(....) <NA> <NA> <NA>

Rolf Turner r.turner at auckland.ac.nz
Sun Jul 12 01:00:26 CEST 2015


Try:

ggg <- c("F","M","F",M")
data$gender <- factor(ggg[data$gender])

This in effect converts the (spurious) " F" and " M" levels into "F" and 
"M" respectively, giving you a factor with the two levels that you 
really want.

cheers,

Rolf Turner

P. S.  *Not* a good idea to use "data" as the name of your data frame.
See fortune("dog").

R. T.

-- 
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

On 12/07/15 07:21, Dagmar Juranková 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.



More information about the R-help mailing list