Re: [R] Add column to dataframe based on code in other column

2013-08-08 Thread MacQueen, Don
Assuming your data frame of users is named 'users', and using your mapping vectors: users$regions <- '' users$regions[ users$State_Code) %in% NorthEast ] <- 'NorthEast' and repeat for the other regions Or, if you put your mappings in a data frame then it is as simple as merge(yourdataframe

Re: [R] Add column to dataframe based on code in other column

2013-08-08 Thread arun
c(2,1,3)] #   Name State_Code    REGION #1   Tom 20 NorthEast #2 Harry 56 Other #3   Ben  5  West #4 Sally  4     South A.K. - Original Message ----- From: Dark To: r-help@r-project.org Cc: Sent: Thursday, August 8, 2013 5:33 AM Subject: [R] Add column to dat

Re: [R] Add column to dataframe based on code in other column

2013-08-08 Thread Bert Gunter
Dark: 1. In future, please use dput() to post data to enable us to more easily read them from your email. 2. As Berend demonstrates, using a more appropriate data structure is what's required. Here is a slightly shorter, but perhaps trickier alternative to his solution: > df ## Your example da

Re: [R] Add column to dataframe based on code in other column

2013-08-08 Thread Berend Hasselman
On 08-08-2013, at 11:33, Dark wrote: > Hi all, > > I have a dataframe of users which contain US-state codes. > Now I want to add a column named REGION based on the state code. I have > already done a mapping: > > NorthEast <- c(07, 20, 22, 30, 31, 33, 39, 41, 47) > MidWest <- c(14, 15, 16, 17

[R] Add column to dataframe based on code in other column

2013-08-08 Thread Dark
Hi all, I have a dataframe of users which contain US-state codes. Now I want to add a column named REGION based on the state code. I have already done a mapping: NorthEast <- c(07, 20, 22, 30, 31, 33, 39, 41, 47) MidWest <- c(14, 15, 16, 17, 23, 24, 26, 28, 35, 36, 43, 52) South <- c(01, 04, 08,