Re: [R] creating a derived variable in a data frame

From: ronggui <042045003_at_fudan.edu.cn>
Date: Thu 20 Oct 2005 - 11:00:23 EST


I suggest you use the recode function in car package to do your job.         

>Hello,
>
>I have read through the manuals and can't seem to find an answer.
>
>I have a categorical, character variable that has hundreds of values. I want to group the existing values of this variable into a new, derived (categorical) variable by applying conditions to the values in the data.
>
>For example, suppose I have a data frame with variables: date, country, x, y, and z.
>
>x,y,z are numeric and country is a 2-digit character string. I want to create a new derived variable named "continent" that would also exist in the data frame. The Continent variable would have values of "Asia", "Europe", "North America", etc...
>
>How would this best be done for a large dataset (>10MB) ?
>I have tried many variations on following without success (note in a real example I would have a longer list of countries and continent values):
>
>> mydata$continent <- mydata[ mydata$country==list('US','CA','MX'), ] -> "North America"
>
>I have read about factors, but I am not sure how they apply here.
>
>Can anyone help me with the syntax? I am sure it is trivial and a common thing to do.
>The ultimate goal is to compute percentages of x by continent.
>
>Thanks for any help in advance.
>
>-Avram
>
>______________________________________________
>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

2005-10-20



Deparment of Sociology
Fudan University

My new mail addres is ronggui.huang@gmail.com Blog:http://sociology.yculblog.com



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 Thu Oct 20 11:06:10 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 19:07:26 EST