[R] generating factors from the edit function

Joshua Wiley jwiley.psych at gmail.com
Sat Mar 5 07:25:33 CET 2011


Hi Erin,

I am assuming this is for pedagogical purposes (i.e., make R less
intimidating by not reading data in).  If that is true, you may just
want to automate the whole process.  I am typically twitchy about
using assign(), but fix does it and again if only for introducing
students to R.....

## mostly automated, still needs assignment
fact1 <- function() {
  dat <- data.frame()
  dat <- edit(dat)
  as.data.frame(as.list(dat))
}

test2.df <- fact1()

## assigns to users' workspace too
sfix <- function(x) {
  dat <- data.frame()
  dat <- edit(dat)
  assign(x, as.data.frame(as.list(dat)), envir = .GlobalEnv)
}

sfix("test3.df")

Cheers,

Josh

On Fri, Mar 4, 2011 at 8:45 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:
> Here is a little function that I put together:
>
>> fact1
> function(x) {
> n <- ncol(x)
> for(i in 1:n) {
> if(mode(x[,i])=="character")x[,i] <- factor(x[,i])
> }
> return(x)
> }
>>
>
> It does the trick.  I'm sure that there are better ways, but this seems ok.
>
> Thank you!!!
>
> Sincerely,
> Erin
>
>
> On Fri, Mar 4, 2011 at 10:42 PM, Ista Zahn <izahn at psych.rochester.edu> wrote:
>> Hi Erin,
>> I would set up the data.frame the way you want it before calling
>> fix(). Something like
>>
>> test2df <- data.frame(v1=numeric(), v2=factor())
>> test2df <- fix(test2df)
>>
>> Best,
>> Ista
>>
>> On Sat, Mar 5, 2011 at 4:34 AM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:
>>> Dear R People:
>>>
>>> If I use the fix or edit function for a new data frame, I would like
>>> to have my character data as factors.
>>>
>>> Is there a "built in" way to do this, please?
>>>
>>> Here is what I did:
>>>
>>>> test2.df <- data.frame()
>>>> test2.df <- fix(test2.df,factor.mode="character")
>>>> str(test2.df)
>>> 'data.frame':   5 obs. of  2 variables:
>>>  $ var1: num  1 2 3 4 5
>>>  $ var2: chr  "a" "a" "g" "g" ...
>>>>
>>> The character data is simply that.  I would like create factors when I
>>> enter character data.
>>>
>>> Thank you!
>>>
>>> Sincerely,
>>>
>>> Erin
>>>
>>>
>>> --
>>> Erin Hodgess
>>> Associate Professor
>>> Department of Computer and Mathematical Sciences
>>> University of Houston - Downtown
>>> mailto: erinm.hodgess at gmail.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.
>>>
>>
>>
>>
>> --
>> Ista Zahn
>> Graduate student
>> University of Rochester
>> Department of Clinical and Social Psychology
>> http://yourpsyche.org
>>
>
>
>
> --
> Erin Hodgess
> Associate Professor
> Department of Computer and Mathematical Sciences
> University of Houston - Downtown
> mailto: erinm.hodgess at gmail.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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list