[R] Creating binary variable depending on strings of two dataframes

Santosh Srinivas santosh.srinivas at gmail.com
Tue Dec 7 17:58:44 CET 2010


your question is not clear to me .. but your solution is a variation of
> data1$x.1 <- data1$x1 %in% data2$y1

you can play with your conditions to get the result you want


On Tue, Dec 7, 2010 at 10:00 PM, Pete Pete <noxyport at gmail.com> wrote:
>
> Hi,
> consider the following two dataframes:
> x1=c("232","3454","3455","342","13")
> x2=c("1","1","1","0","0")
> data1=data.frame(x1,x2)
>
> y1=c("232","232","3454","3454","3455","342","13","13","13","13")
> y2=c("E1","F3","F5","E1","E2","H4","F8","G3","E1","H2")
> data2=data.frame(y1,y2)
>
> I need a new column in dataframe data1 (x3), which is either 0 or 1
> depending if the value "E1" in y2 of data2 is true while x1=y1. The result
> of data1 should look like this:
>   x1     x2 x3
> 1 232   1   1
> 2 3454 1   1
> 3 3455 1   0
> 4 342   0   0
> 5 13     0   1
>
> I think a SQL command could help me but I am too inexperienced with it to
> get there.
>
> Thanks for your help!
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Creating-binary-variable-depending-on-strings-of-two-dataframes-tp3076724p3076724.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list