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 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 6 1992 AUS DNK 0 0 17.4950008 321708.281 5.1700001 150195.484 0 15725.5 1.453406e+02 7 1992 AUS ESP 0 0 17.4950008 321708.281 39.0677490 612585.250 0 17072.9 2.106880e+02 8 1992 AUS FIN 0 0 17.4950008 321708.281 5.0419998 109859.438 0 14849.5 2.025125e+02 9 1992 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.3699999 102022.352 0 14845.6 4.164985e+01 12 1992 AUS IRL 0 1 17.4950008 321708.281 3.5490999 54272.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.5316999 40706.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. Michael Weylandt <michael.weylandt@gmail.com>
2012-Apr-22 19:32 UTC
[R] Assignment problems
Look at ?ifelse, a combination of logical subscripting and mean(), or even better ?ave -- I can't say too much more; there's a no homework policy on this list and I recognize that first solution as mine already... (I should have noted that the first time) Michael On Apr 22, 2012, at 2:54 PM, phillip03 <phillipbrigham at hotmail.com> wrote:> 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 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 > 6 1992 AUS DNK 0 0 17.4950008 321708.281 > 5.1700001 150195.484 0 15725.5 1.453406e+02 > 7 1992 AUS ESP 0 0 17.4950008 321708.281 > 39.0677490 612585.250 0 17072.9 2.106880e+02 > 8 1992 AUS FIN 0 0 17.4950008 321708.281 > 5.0419998 109859.438 0 14849.5 2.025125e+02 > 9 1992 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.3699999 102022.352 0 14845.6 4.164985e+01 > 12 1992 AUS IRL 0 1 17.4950008 321708.281 > 3.5490999 54272.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.5316999 40706.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 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, phillip03 wrote> > 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 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 > 6 1992 AUS DNK 0 0 17.4950008 321708.281 > 5.1700001 150195.484 0 15725.5 1.453406e+02 > 7 1992 AUS ESP 0 0 17.4950008 321708.281 > 39.0677490 612585.250 0 17072.9 2.106880e+02 > 8 1992 AUS FIN 0 0 17.4950008 321708.281 > 5.0419998 109859.438 0 14849.5 2.025125e+02 > 9 1992 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.3699999 102022.352 0 14845.6 4.164985e+01 > 12 1992 AUS IRL 0 1 17.4950008 321708.281 > 3.5490999 54272.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.5316999 40706.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. ??? >You're forgeting the index in the conditon, EMU[i] == 1. Note that since EMU is a logical vector, you don't need the explicit comparison. If you just want the mean of avgflow where EMU == TRUE, this is much simpler, but returns one value, not a vector. mean(avgflow[ EMU ]) Hope this helps, Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/Assignment-problems-tp4578672p4578739.html Sent from the R help mailing list archive at Nabble.com.
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.
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.
phillip03 wrote> > 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]) ??? >Answer: yes. Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/Assignment-problems-tp4578672p4578772.html Sent from the R help mailing list archive at Nabble.com.
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.
> > If I want to make a new data.frame where it is the NONEURO avgflows. how > do I do that ? >Exactly like above, but without the negation (the exclamation mark). You must also start to use the help system, for instance: ?"!" Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/Assignment-problems-tp4578672p4581434.html Sent from the R help mailing list archive at Nabble.com.
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.
On 23-04-2012, at 21:37, phillip03 wrote:> Thank you! > > Do you know why ifelse() sometimes returns "NULL" ?Please provide a reproducible example for this phenomenon. Berend
Hi> EMU1993<-(for (i in 1:nrow(data)){+ ifelse(year==1992,sum(avgflowEMU),0) + })>EMU1993NULL -- 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.