[R] How to add specific column to data.set?
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 itlusa . . . 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@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.
Re: [R] How to add specific column to data.set?
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@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.
Re: [R] How to add specific column to data.set?
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 comlangpop1gdp1 pop2 gdp2 rtadist avgflow 11992 AUS AUT 0 0 17.4950008 321708.281 7.7825189 194684.078 0 15608.4 1.075999e+02 21992 AUS BEL 0 0 17.4950008 321708.281 10.0450001 231762.094 0 16319.2 4.767162e+02 31992 AUS CAN 0 1 17.4950008 321708.281 28.5195980 570291.188 0 15391.1 7.456945e+02 41992 AUS CHE 0 0 17.4950008 321708.281 6.875 249471.422 0 16170.1 4.625214e+02 51992 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.35e+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. __ 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.
Re: [R] How to add specific column to data.set?
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. __ 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.
Re: [R] How to add specific column to data.set?
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. __ 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.
[R] Assignment problems
The text below is a part of, some work I have to do, which is due in 2 days and I am strung up with a lot of other stuff, so I was hoping someone would take 5 mins and help me ?? Here is a part of my data.frame: year country1 country2 contig comlangpop1gdp1 pop2 gdp2 rtadist avgflow 11992 AUS AUT 0 0 17.4950008 321708.281 7.7825189 194684.078 0 15608.4 1.075999e+02 21992 AUS BEL 0 0 17.4950008 321708.281 10.0450001 231762.094 0 16319.2 4.767162e+02 31992 AUS CAN 0 1 17.4950008 321708.281 28.5195980 570291.188 0 15391.1 7.456945e+02 41992 AUS CHE 0 0 17.4950008 321708.281 6.875 249471.422 0 16170.1 4.625214e+02 51992 AUS DEU 0 0 17.4950008 321708.281 80.6240005 2062141.500 0 15935.1 2.047573e+03 61992 AUS DNK 0 0 17.4950008 321708.281 5.171 150195.484 0 15725.5 1.453406e+02 71992 AUS ESP 0 0 17.4950008 321708.281 39.0677490 612585.250 0 17072.9 2.106880e+02 81992 AUS FIN 0 0 17.4950008 321708.281 5.0419998 109859.438 0 14849.5 2.025125e+02 91992 AUS FRA 0 0 17.4950008 321708.281 57.2422981 1371706.000 0 16513.0 1.070802e+03 10 1992 AUS GBR 0 1 17.4950008 321708.281 57.9023476 1071537.375 0 16602.3 2.279130e+03 11 1992 AUS GRC 0 0 17.4950008 321708.281 10.369 102022.352 0 14845.6 4.164985e+01 12 1992 AUS IRL 0 1 17.4950008 321708.281 3.549099954272.410 0 16895.0 1.076323e+02 13 1992 AUS ISL 0 0 17.4950008 321708.281 0.2611000 6976.168 0 16443.6 2.190602e+01 14 1992 AUS ITA 0 0 17.4950008 321708.281 56.7976494 1265800.125 0 15855.4 9.683720e+02 15 1992 AUS JPN 0 0 17.4950008 321708.281 124.2289963 3766884.000 0 7827.1 1.026065e+04 16 1992 AUS NLD 0 0 17.4950008 321708.281 15.1780005 348224.562 0 16227.5 6.510009e+02 17 1992 AUS NOR 0 0 17.4950008 321708.281 4.2863998 127170.328 0 15646.2 9.357240e+01 18 1992 AUS NZL 0 1 17.4950008 321708.281 3.531699940706.199 1 2736.4 2.267670e+03 19 1992 AUS PRT 0 0 17.4950008 321708.281 9.9630003 102890.258 0 17625.3 2.611476e+02 20 1992 AUS SWE 0 0 17.4950008 321708.281 8.6680002 264822.875 0 15385.4 4.653388e+02 there is 3400 observations. 3.1.1. Construct a dummy variable, EMU, that in any given year takes the value 1 if both countries are members of the EMU and 0 otherwise. How big a proportion of the observations are among EMU member countries? This problem is solved with: euro<-c("AUT","BEL","DEU","ESP","FIN","FRA","GRC","IRL","ITA","NLD","PRT") countries<-data.frame(country1,country2,stringsAsFactors=FALSE) data1<-cbind(data,EMU=Reduce(`&`, lapply(countries, function(x) x %in% euro))) data1[EMU==TRUE,13] a<-table(EMU) 3.1.2. Are the member and non-member country-pairs alike? What I need here is: I want to find the mean of avgflow, but only for the data where 2 countries are in the euro vector/if EMU=TRUE ? I have tried with: >avgflowONLY<-cbind(avgflow,EMU) > NEWavgflow<-rep(0,nrow(avgflowONLY)) > for (i in 1:nrow(avgflowONLY)){if > (EMU==1){NEWavgflow[i]<-mean(avgflow[i])}} BUT it gives me: Warning messages: 1: In if (EMU == 1) { ... : the condition has length > 1 and only the first element will be used etc. ??? -- View this message in context: http://r.789695.n4.nabble.com/Assignment-problems-tp4578672p4578672.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] Assignment problems
I have tried ifelse: > trade<-data.frame(avgflow,EMU,stringsAsFactors=FALSE) > avgflowEURO<-rep(0,nrow(trade)) > trade1<-(for (i in > 1:nrow(trade)){ifelse(EMU[i]==1,avgflowEURO[i]<-avgflow[i],NA)}) -- View this message in context: http://r.789695.n4.nabble.com/Assignment-problems-tp4578672p4578754.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] Assignment problems
Does mean(avgflow[EMU]) sum the avgflows for all countrypairs where EMU[i]==TRUE and take the mean ? Practical question: is mean(avgflow[EMU]) = mean(avgflow[EMU==TRUE]) ??? -- View this message in context: http://r.789695.n4.nabble.com/Assignment-problems-tp4578672p4578761.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] Assignment problems
Thank you Rui Can you help me with my ifelse problem - I would like to add a list to my data.frame where avgflow in those rows where ONLY my country pair both are in euro -- View this message in context: http://r.789695.n4.nabble.com/Assignment-problems-tp4578672p4578806.html Sent from the R help mailing list archive at Nabble.com. __ 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.
[R] OLS Estimating
Hi I am looking for a efficient way to estimate all parameters in your data.frame set using a specific function: for example ln(T)=b_0 + b_1*ln(Y_i*Y_j) + b_2*ln()+ ... + etc. Thanks, Ph -- View this message in context: http://r.789695.n4.nabble.com/OLS-Estimating-tp4580055p4580055.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] OLS Estimating
So how would I use the lm() to estimate b_0 and b_1 for example My Y_i and Y_j are data observations how does the lm() use my data.frame ? -- View this message in context: http://r.789695.n4.nabble.com/OLS-Estimating-tp4580055p4581048.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] Assignment problems
Hi RUI Thank you so much ! I know I have a lot to learn : / sorry for that. If I want to make a new data.frame where it is the NONEURO avgflows. how do I do that ? Ph -- View this message in context: http://r.789695.n4.nabble.com/Assignment-problems-tp4578672p4581164.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] Assignment problems
Thank you! Do you know why ifelse() sometimes returns "NULL" ? -- View this message in context: http://r.789695.n4.nabble.com/Assignment-problems-tp4578672p4581491.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] Assignment problems
Hi > EMU1993<-(for (i in 1:nrow(data)){ + ifelse(year==1992,sum(avgflowEMU),0) + }) >EMU1993 NULL -- View this message in context: http://r.789695.n4.nabble.com/Assignment-problems-tp4578672p4581590.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] Assignment problems
Thank you David! That was EXACTLY what I needed. I have one last question: I want to create a mix of EMU and nonEMU country pairs. I have tried with: EMUmix1<-Reduce(`&`, lapply(countries,function(x,y){country1 %in% euro & country2 %in% noneuro})) # Where euro are the EMU countries and noneuro are (obviously) the nonEMU countries EMUmix2<-Reduce(`&`, lapply(countries,function(x,y) {country1 %in% noneuro & country2 %in% euro})) EMUmix3<-Reduce(`&`, lapply(countries,function(x,y) {country1 %in% euro2 & country2 %in% noneuro})) # euro2 are also the EMUcountries, BUT in 2001 Greece became a EMU member. So in the interval (1999:2000) all euro countries are reprsentated and from 2001- all euro+GREECE=euro2 are representated. EMUmix4<-Reduce(`&`, lapply(countries,function(x,y) {country1 %in% noneuro & country2 %in% euro2})) EMUmixnonGRC<-ifelse(year %in% (1999:2000),EMUmix1|EMUmix2,0) EMUmixGRC<-ifelse(year >= 2001,EMUmix3|EMUmix4,0) EMUmix<- EMUmixnonGRC|EMUmixGRC data2<- cbind(data1,EMUmixnonGRC,EMUmixGRC) # This dosent QUITE work. I get 0 in (1992:1998) and from 2001- there is neither a problem, BUT in (1999:2000) there is a problem: every country pair where Greece (nonEMU at the time) in either country1 list or country 2 is paired with an EMU country I recieve a 0, where I of course seek a 1, hence i am looking for EMUmix Anyone ? ps. thanks to everyone for helping me so far: michael, David, Berend, Rui etc. I know I have a LOT to learn -- View this message in context: http://r.789695.n4.nabble.com/Assignment-problems-tp4578672p4584675.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] Assignment problems
Thanks again! I would like to construct 14 new 'year' dummy variables. I have 14 years: 1992:2006 with 231 observations pr. year. The year dummies should assign a 1 if the observation is within the specific year and 0 otherwise. So for example: 1992dummyvariable=1 if year=0 and so on. P -- View this message in context: http://r.789695.n4.nabble.com/Assignment-problems-tp4578672p4586252.html Sent from the R help mailing list archive at Nabble.com. __ 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.