Hello R-users, The below is a snippet of my data: fid crop year value 5_1_1 SWHE 1995 171 5_1_1 SWHE 1997 696 5_1_1 BARL 1996 114 5_1_1 BARL 1997 344 5_2_2 SWHE 1995 120 5_2_2 SWHE 1996 511 5_2_2 BARL 1996 239 5_2_2 BARL 1997 349 Here, I want to create dummy variables with the names of the content of a column 'crop' in a way that the new variable 'SWHE' would receive a value of 1 if the column 'crop' contains 'SWHE' and 0 otherwise. So, I would have two new variables SWHE and BARL as below: fid crop year value SWHE BARL 5_1_1 SWHE 1995 171 1 0 5_1_1 SWHE 1997 696 1 0 5_1_1 BARL 1996 114 0 1 5_1_1 BARL 1997 344 0 1 5_2_2 SWHE 1995 120 1 0 5_2_2 SWHE 1996 511 1 0 5_2_2 BARL 1996 239 0 1 5_2_2 BARL 1997 349 0 1 Cheers, Shyam Nepal [[alternative HTML version deleted]]
Dummy variables are not needed in R. Bert Sent from my iPhone -- please excuse typos. On Apr 20, 2013, at 11:23 AM, shyam basnet <shyamabc2002 at yahoo.com> wrote:> Hello R-users, > > The below is a snippet of my data: > > > fid crop year value > 5_1_1 SWHE 1995 171 > 5_1_1 SWHE 1997 696 > 5_1_1 BARL 1996 114 > 5_1_1 BARL 1997 344 > 5_2_2 SWHE 1995 120 > 5_2_2 SWHE 1996 511 > 5_2_2 BARL 1996 239 > 5_2_2 BARL 1997 349 > > Here, I want to create dummy variables with the names of the content of a column 'crop' in a way that the new variable 'SWHE' would receive a value of 1 if the column 'crop' contains 'SWHE' and 0 otherwise. So, I would have two new variables SWHE and BARL as below: > > > fid crop year value SWHE BARL > 5_1_1 SWHE 1995 171 1 0 > 5_1_1 SWHE 1997 696 1 0 > 5_1_1 BARL 1996 114 0 1 > 5_1_1 BARL 1997 344 0 1 > 5_2_2 SWHE 1995 120 1 0 > 5_2_2 SWHE 1996 511 1 0 > 5_2_2 BARL 1996 239 0 1 > 5_2_2 BARL 1997 349 0 1 > > > Cheers, > Shyam > Nepal > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
Hello Shyam,
if your data is stored in variable "dataset", for example, the
following
code will create the desired dummy-coded variables and attach them to the
dataset:
######################
#init vars
SWHE=BARL <- vector(length=nrow(dataset))
SWHE[]=BARL[] <- 0 #initialize dummy-coded vars with all 0s
#fill in variables
SWHE[grep("SWHE", dataset$crop)] <- 1 #grep returns the indices
where a
match is found, see ?grep
BARL[grep("BARL", dataset$crop)] <- 1
#attach new dummy codes to dataset
dataset$SWHE <- SWHE
dataset$BARL <- BARL
######################
Hope this helps,
Patrick
2013/4/20 shyam basnet <shyamabc2002@yahoo.com>
> Hello R-users,
>
> The below is a snippet of my data:
>
>
> fid crop year value
> 5_1_1 SWHE 1995 171
> 5_1_1 SWHE 1997 696
> 5_1_1 BARL 1996 114
> 5_1_1 BARL 1997 344
> 5_2_2 SWHE 1995 120
> 5_2_2 SWHE 1996 511
> 5_2_2 BARL 1996 239
> 5_2_2 BARL 1997 349
>
> Here, I want to create dummy variables with the names of the content of a
> column 'crop' in a way that the new variable 'SWHE' would
receive a value
> of 1 if the column 'crop' contains 'SWHE' and 0 otherwise.
So, I would have
> two new variables SWHE and BARL as below:
>
>
> fid crop year value SWHE BARL
> 5_1_1 SWHE 1995 171 1 0
> 5_1_1 SWHE 1997 696 1 0
> 5_1_1 BARL 1996 114 0 1
> 5_1_1 BARL 1997 344 0 1
> 5_2_2 SWHE 1995 120 1 0
> 5_2_2 SWHE 1996 511 1 0
> 5_2_2 BARL 1996 239 0 1
> 5_2_2 BARL 1997 349 0 1
>
>
> Cheers,
> Shyam
> Nepal
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help@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.
>
>
[[alternative HTML version deleted]]