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,
On 08-08-2013, at 11:33, Dark i...@software-solutions.nl 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 -
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
.
- Original Message -
From: Dark i...@software-solutions.nl
To: r-help@r-project.org
Cc:
Sent: Thursday, August 8, 2013 5:33 AM
Subject: [R] Add column to dataframe based on code in other column
Hi all,
I have a dataframe of users which contain US-state codes.
Now I want to add a column named
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,
5 matches
Mail list logo