[R] How to add specific column to data.set?

R. Michael Weylandt michael.weylandt at gmail.com
Tue Apr 17 19:55:02 CEST 2012


Reproducible example not provided... try this -- it should generalize
to multiple columns:

EU <- c("UK","FR","DE") # Yes, I know there are more....
countries <- data.frame( country1 = c("US","CH","UK","AU"), country2 =
c( "CA", "MX", "FR", "DE"), stringsAsFactors = FALSE)

cbind(countries, EU.Membership = Reduce(`&`, lapply(countries,
function(x) x %in% EU)))

Michael

On Tue, Apr 17, 2012 at 1:24 PM, phillip03 <phillipbrigham at hotmail.com> wrote:
> Hi Michael
>
> Sorry it is a data frame where to of the columns are 22 countries arranged
> in random. I now want to add and ekstra column that is 1 if the country par
> for the specific row is both european countries and 0 if not.
>
> Regards
>
> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-add-specific-column-to-data-set-tp4565341p4565466.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