As Chris points out, we do not know what the factor labels were before you tried to change them. The levels() function should have worked as long as it included all of the factor levels in the same order. The names() function lists the names of an object. For a data frame that is the column headings. Vectors like ind.davis$Ageclass do not have names:
> set.seed(42) # Create some random data > ind.davis <- data.frame(ID=1:10, Ageclass=sample(c("A", "J", "S"), 10, > replace=TRUE)) > str(ind.davis) 'data.frame': 10 obs. of 2 variables: $ ID : int 1 2 3 4 5 6 7 8 9 10 $ Ageclass: Factor w/ 3 levels "A","J","S": 3 3 1 3 2 2 3 1 2 3 > levels(ind.davis$Ageclass) [1] "A" "J" "S" > names(ind.davis$Ageclass) NULL > names(ind.davis) [1] "ID" "Ageclass" > levels(ind.davis$Ageclass) <- c("Adult", "Juvenile", "Sub-adult") > levels(ind.davis$Ageclass) [1] "Adult" "Juvenile" "Sub-adult" > str(ind.davis) 'data.frame': 10 obs. of 2 variables: $ ID : int 1 2 3 4 5 6 7 8 9 10 $ Ageclass: Factor w/ 3 levels "Adult","Juvenile",..: 3 3 1 3 2 2 3 1 2 3 ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Christopher W Ryan Sent: Tuesday, June 20, 2017 8:23 AM To: R-help <r-help@r-project.org> Subject: Re: [R] Help Leonardo-- R-help can be a very useful resource. Some suggestions to use it well: 1. use an informative subject line, not "help" 2. include a "minimal working example:" a *little* data, the code that, with those data, reproduces your problem, and the error message that resulted. As to your particular question, at this point I can only guess, but for starters it would probably help to show the output of str(ind.davis) --Chris Ryan On Tue, Jun 20, 2017 at 1:06 AM, Leonardo Malaguti < leonardomalagut...@gmail.com> wrote: > Dear expert friends, > I'm pretty young of this world and my question at your eyes can be petty > easy. > I'll need to change the name of the levels inside a column of my data-frame > > levels(ind.davis$Ageclass) <- c("adult", "Juvanile", "sub-adult") > names(ind.davis$Ageclass) <- c("Adult", "Juvenile", "Sub-adult") > that is what I tried but of course doesn't work. > > Thanks, > have a wonderful day, > Leo > > [[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. > [[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.