Hi I have a data.set with 7 lists, with over 7000 observations in each list. to of the lists contain country names. like: aus dnk fra aus usa aut itl usa . . . etc. My dilemma is that I want to add an extra column/list to my data.set. If the countries are both european it should be assigned 1 or true AND if one or both the countries are non-european it should be assigned 0 or false. Please et me know if you dont understand my dilemma. Thanks -- View this message in context: http://r.789695.n4.nabble.com/How-to-add-specific-column-to-data-set-tp4565341p4565341.html Sent from the R help mailing list archive at Nabble.com.
Do you have lists or something like a data.frame? Your printout suggests you don't actually have a list. (well, data.frames really are lists but let's just not talk about that) Perhaps %in% is what you are looking for... EU <- c("UK","FR","DE") # Yes, I know there are more.... countries <- c("US","CH","UK", "CA", "MX", "FR", "DE") countries %in% EU or more to your question cbind(countries, countries %in% EU) or data.frame(Country = countries, EU.Member = countries %in% EU) Michael On Tue, Apr 17, 2012 at 12:40 PM, phillip03 <phillipbrigham at hotmail.com> wrote:> Hi > I have a data.set with 7 lists, with over 7000 observations in each list. to > of the lists contain country names. > like: > aus ? dnk > fra ? aus > usa ?aut > itl ? ?usa > . > . > . > etc. > > My dilemma is that I want to add an extra column/list to my data.set. If the > countries are both european it should be assigned 1 or true AND if one or > both the countries are non-european it should be assigned 0 or false. > > Please et me know if you dont understand my dilemma. > > Thanks > > -- > View this message in context: http://r.789695.n4.nabble.com/How-to-add-specific-column-to-data-set-tp4565341p4565341.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.
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.
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.
This is the first and last part of my data frame. There is 3465 (not 7000) observations so the "1 or 0" column has to be 3465 rows long aswell year country1 country2 contig comlang pop1 gdp1 pop2 gdp2 rta dist avgflow 1 1992 AUS AUT 0 0 17.4950008 321708.281 7.7825189 194684.078 0 15608.4 1.075999e+02 2 1992 AUS BEL 0 0 17.4950008 321708.281 10.0450001 231762.094 0 16319.2 4.767162e+02 3 1992 AUS CAN 0 1 17.4950008 321708.281 28.5195980 570291.188 0 15391.1 7.456945e+02 4 1992 AUS CHE 0 0 17.4950008 321708.281 6.8750000 249471.422 0 16170.1 4.625214e+02 5 1992 AUS DEU 0 0 17.4950008 321708.281 80.6240005 2062141.500 0 15935.1 2.047573e+03 . . . . . . 3461 2006 NZL SWE 0 0 4.1253757 103873.211 9.0457277 384927.281 0 17389.6 1.371846e+02 3462 2006 NZL USA 0 1 4.1253757 103873.211 298.9880981 13201819.000 0 12765.8 3.273475e+03 3463 2006 PRT SWE 0 0 10.5896521 192571.922 9.0457277 384927.281 1 2821.6 6.624816e+02 3464 2006 PRT USA 0 0 10.5896521 192571.922 298.9880981 13201819.000 0 7004.5 2.381285e+03 3465 2006 SWE USA 0 0 9.0457277 384927.281 298.9880981 13201819.000 0 7440.5 9.777735e+03 -- View this message in context: http://r.789695.n4.nabble.com/How-to-add-specific-column-to-data-set-tp4565341p4566008.html Sent from the R help mailing list archive at Nabble.com.
Wow it works :) Thank you SO much!! I am very new at R and was thinking if you would explain what these to codes do: countries<-data.frame(country1,country2,stringsAsFactors=FALSE) # I know this i a vector with both my county lists but what does stringsAsFacors= FALSE do ?. What if I wantede a row with 1=TRUE and 0=FALSE and cbind(countries,EMU=Reduce(`&`, lapply(countries, + function(x) x %in% euro))) # here I columnbind my new defined vector and produce a new one ? What does the Reduce and lapply do ? I am also not quite sure of the dput() you wrote michael sorry. I understand if you dont have the time to help further :) Ph -- View this message in context: http://r.789695.n4.nabble.com/How-to-add-specific-column-to-data-set-tp4565341p4566822.html Sent from the R help mailing list archive at Nabble.com.
So the lapply function checks every country pair row for row (in my country1 and country2 column) and send back a true if both countries are EU? How do I get a 1 instead of true and 2 instead of false? You have been a BIG help. I have a WHOLE assignment to get done, so maybe I'll post some more questions :) -- View this message in context: http://r.789695.n4.nabble.com/How-to-add-specific-column-to-data-set-tp4565341p4568491.html Sent from the R help mailing list archive at Nabble.com.