Re: [R] cannot turn some columns in a data frame into factors

From: jim holtman <jholtman_at_gmail.com>
Date: Fri 12 May 2006 - 02:27:39 EST

try '<<-' as the assignment to make it global.

     df[[pos]] <<- factor(df[[pos]])

On 5/11/06, Sam Steingold <sds@podval.org> wrote:
>
> Hi,
> I have a data frame df and a list of names of columns that I want to
> turn into factors:
>
> df.names <- attr(df,"names")
> sapply(factors, function (name) {
> pos <- match(name,df.names)
> if (is.na(pos)) stop(paste(name,": no such column\n"))
> df[[pos]] <- factor(df[[pos]])
> cat(name,"(",pos,"):",is.factor(df[[pos]]),"\n")
> })
> cat("factors:",sapply(df,is.factor),"\n")
>
> the output is:
>
>
> Month ( 1 ): TRUE
> factors: FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
>
>
> i.e., there is a column named "Month" (the 1st column), and it is indeed
> turned into a factor inside sapply(), but after that it is numerical
> again!
>
> what am I doing wrong?
>
> --
> Sam Steingold (http://www.podval.org/~sds) on Fedora Core release 5
> (Bordeaux)
> http://honestreporting.com http://truepeace.org
> http://openvotingconsortium.org
> http://thereligionofpeace.com http://memri.org http://palestinefacts.org
> UNIX, car: hard to learn/easy to use; Windows, bike: hard to learn/hard to
> use.
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>

-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390 (Cell)
+1 513 247 0281 (Home)

What is the problem you are trying to solve?

	[[alternative HTML version deleted]]

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Fri May 12 02:32:07 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 12 May 2006 - 04:10:07 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.