[R] creating a new variable.

Jim Lemon jim at bitwrit.com.au
Tue Feb 19 03:17:09 CET 2013


On 02/19/2013 11:01 AM, Nicole Ford wrote:
> hello, all.
>
> in my previous research, i have always used existing data.  i am trying something new as an exploratory exercise and have never create my own variable form scratch.
>
> essentially, i am creating a variable for party affiliation.
>
> here is an example.
>
> var =party.
>
> levels= democrat, republican, other.
> ...
> this is where i am getting stuck...  any thoughts would be appreciated.
>
Hi Nicole,
The usual way something like this is done is to create a factor of a 
given size with the appropriate levels:

party<-factor(sample(c("dem","rep","oth"),100,TRUE))
  party
   [1] dem dem dem rep oth dem dem oth rep dem rep rep oth dem oth oth 
dem dem
  [19] oth rep oth rep dem oth rep dem oth oth oth oth rep dem oth dem 
oth rep
  [37] dem oth dem oth rep oth rep oth rep dem dem rep rep rep dem oth 
dem dem
  [55] oth dem rep dem rep oth rep oth dem dem dem rep dem rep dem oth 
rep rep
  [73] rep dem rep rep rep dem oth oth oth rep oth rep oth oth dem oth 
rep dem
  [91] rep rep oth dem rep dem dem oth dem dem
Levels: dem oth rep

Jim



More information about the R-help mailing list