Dear R Experts, I am having some trouble creating a variable in R. I have data on self-placement of voters, their placement of parties, and which party they feel closest to. The data is structured like this: Party_Close lrplaceself lrplaceParty1 lrplaceParty2 ... party1 2 4 5 party2 5 6 4 party1 6 2 1 etc... I want to format the data set so it looks like this: Party_Close lrplacepartyclose lrplaceself party1 4 2 party2 4 5 party1 2 6 Any help is greatly appreciated! With kind regards, Thomas
I am not entirely sure what you are trying to do. Are you wanting to re-arrange the columns or apply some sort of transformation? On Tue, Mar 23, 2010 at 4:47 AM, Thomas Jensen <thomas.jensen at eup.gess.ethz.ch> wrote:> Dear R Experts, > > I am having some trouble creating a variable in R. I have data on > self-placement of voters, their placement of parties, and which party > they feel closest to. The data is structured like this: > > ?Party_Close ? ?lrplaceself ? ? lrplaceParty1 ? lrplaceParty2 ... > ? party1 ? ? ? ? ? ? ? 2 ? ? ? ? ? ? ? 4 ? ? ? ? ? ? ? 5 > ? party2 ? ? ? ? ? ? ? 5 ? ? ? ? ? ? ? 6 ? ? ? ? ? ? ? 4 > ? party1 ? ? ? ? ? ? ? 6 ? ? ? ? ? ? ? 2 ? ? ? ? ? ? ? 1 > > etc... > > I want to format the data set so it looks like this: > > Party_Close ? ? lrplacepartyclose ? ? ? ? ? lrplaceself > ?party1 ? ? ? ? ? ? ? ?4 ? ? ? ? ? ? ? ? ? ? ? 2 > ?party2 ? ? ? ? ? ? ? ?4 ? ? ? ? ? ? ? ? ? ? ? 5 > ?party1 ? ? ? ? ? ? ? ?2 ? ? ? ? ? ? ? ? ? ? ? 6 > > Any help is greatly appreciated! > > With kind regards, > Thomas > > ______________________________________________ > 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. >-- Stephen Sefick Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis
Looks like this way, dat$lrplacepartyclose <- NA dat$lrplacepartyclose[dat$Party_Close=="party1"] <- dat$lrplaceParty1[dat$Party_Close=="party1"] dat$lrplacepartyclose[dat$Party_Close=="party2"] <- dat$lrplaceParty2[dat$Party_Close=="party2"] and goes on and on. On 23 March 2010 17:47, Thomas Jensen <thomas.jensen at eup.gess.ethz.ch> wrote:> Dear R Experts, > > I am having some trouble creating a variable in R. I have data on > self-placement of voters, their placement of parties, and which party > they feel closest to. The data is structured like this: > > ?Party_Close ? ?lrplaceself ? ? lrplaceParty1 ? lrplaceParty2 ... > ? party1 ? ? ? ? ? ? ? 2 ? ? ? ? ? ? ? 4 ? ? ? ? ? ? ? 5 > ? party2 ? ? ? ? ? ? ? 5 ? ? ? ? ? ? ? 6 ? ? ? ? ? ? ? 4 > ? party1 ? ? ? ? ? ? ? 6 ? ? ? ? ? ? ? 2 ? ? ? ? ? ? ? 1 > > etc... > > I want to format the data set so it looks like this: > > Party_Close ? ? lrplacepartyclose ? ? ? ? ? lrplaceself > ?party1 ? ? ? ? ? ? ? ?4 ? ? ? ? ? ? ? ? ? ? ? 2 > ?party2 ? ? ? ? ? ? ? ?4 ? ? ? ? ? ? ? ? ? ? ? 5 > ?party1 ? ? ? ? ? ? ? ?2 ? ? ? ? ? ? ? ? ? ? ? 6 > > Any help is greatly appreciated! > > With kind regards, > Thomas > > ______________________________________________ > 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. >-- Wincent Ronggui HUANG Doctoral Candidate Dept of Public and Social Administration City University of Hong Kong http://asrr.r-forge.r-project.org/rghuang.html