HI!!! I have a table containing qualitative and quantitative data; one of the columns contains "Level of education", and the possibilities are "none", "High school", "college"; I want to give the value 0 to "none", the value 1 to "High school", and 2 to "college", but I got the following error: In `[<-.factor`(`*tmp*`, educa = "college", value = 2) : invalid factor level, NAs generated WHAT IS WRONG????? :S :S :S THANKS A LOT!!!!!!!!!!!!!! -- View this message in context: http://r.789695.n4.nabble.com/PROBLEMS-with-In-factor-tmp-ORDERED-FACTOR-tp4631736.html Sent from the R help mailing list archive at Nabble.com.
R. Michael Weylandt
2012-May-29 20:04 UTC
[R] PROBLEMS with In `[<-.factor`(`*tmp*`, (¿ORDERED FACTOR?)
Read this: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Read the posting guide: http://www.R-project.org/posting-guide.html and try again. It's entirely impossible to help you as is. Michael On Tue, May 29, 2012 at 2:01 PM, RBB <raulbajob at gmail.com> wrote:> HI!!! > > I have a table containing qualitative and quantitative data; one of the > columns contains ?"Level of education", and the possibilities are "none", > "High school", "college"; I want to give the value 0 to "none", the value 1 > to "High school", and 2 to "college", but I got the following error: > > In `[<-.factor`(`*tmp*`, educa = "college", value = 2) : > ?invalid factor level, NAs generated > > > WHAT IS WRONG????? :S :S :S > > > THANKS A LOT!!!!!!!!!!!!!! > > -- > View this message in context: http://r.789695.n4.nabble.com/PROBLEMS-with-In-factor-tmp-ORDERED-FACTOR-tp4631736.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
2012-May-29 20:21 UTC
[R] PROBLEMS with In `[<-.factor`(`*tmp*`, (¿ORDERED FACTOR?)
On May 29, 2012, at 11:01 AM, RBB wrote:> HI!!! > > I have a table containing qualitative and quantitative data; one of > the > columns contains "Level of education", and the possibilities are > "none", > "High school", "college"; I want to give the value 0 to "none", the > value 1 > to "High school", and 2 to "college", but I got the following error: > > In `[<-.factor`(`*tmp*`, educa = "college", value = 2) : > invalid factor level, NAs generated > > > WHAT IS WRONG????? :S :S :S >Hard to say. Although we can be fairly sure you have a factor and that your do NOT have an R contingency table. If you really had a table it would be one thing. A dataframe is another thing and one that is much more common. dfrm <- data.frame(lev=factor(c("none", "high", "low", "high", "low"))) > dfrm lev 1 none 2 high 3 low 4 high 5 low > levels(dfrm$lev)<- 2:0 > dfrm lev 1 0 2 2 3 1 4 2 5 1 Notice that a factor variable's levels are ordered alphabetically, so "high" < "low" < "none" and if you wnat the numbers to match up you need to mentally figure out the right order to make the matching work properly. (It would be better practice not to overwrite the levels of an existing factor, since that action makes it very easy to really hash up the data. -- David.> > THANKS A LOT!!!!!!!!!!!!!! > > -- > View this message in context: http://r.789695.n4.nabble.com/PROBLEMS-with-In-factor-tmp-ORDERED-FACTOR-tp4631736.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.
HI, Do you want to replace the column ("level of education") with values or just add values as second column?? I guess you are looking for the latter. Try this, dat<-data.frame(Levelofeducation=rep(c("none","HighSchool","College"),c(3,3,3)),value=rep(c(0,1,2),c(3,3,3))) str(dat) data.frame':??? 9 obs. of? 2 variables: ?$ Levelofeducation: Factor w/ 3 levels "College","HighSchool",..: 3 3 3 2 2 2 1 1 1 ?$ value?????????? : num? 0 0 0 1 1 1 2 2 2> dat? Levelofeducation value 1???????????? none???? 0 2???????????? none???? 0 3???????????? none???? 0 4?????? HighSchool???? 1 5?????? HighSchool???? 1 6?????? HighSchool???? 1 7????????? College???? 2 8????????? College???? 2 9????????? College???? 2 A.K. ----- Original Message ----- From: RBB <raulbajob at gmail.com> To: r-help at r-project.org Cc: Sent: Tuesday, May 29, 2012 2:01 PM Subject: [R] PROBLEMS with In `[<-.factor`(`*tmp*`, (?ORDERED FACTOR?) HI!!! I have a table containing qualitative and quantitative data; one of the columns contains? "Level of education", and the possibilities are "none", "High school", "college"; I want to give the value 0 to "none", the value 1 to "High school", and 2 to "college", but I got the following error: In `[<-.factor`(`*tmp*`, educa = "college", value = 2) : ? invalid factor level, NAs generated WHAT IS WRONG????? :S :S :S THANKS A LOT!!!!!!!!!!!!!! -- View this message in context: http://r.789695.n4.nabble.com/PROBLEMS-with-In-factor-tmp-ORDERED-FACTOR-tp4631736.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.