hello, all. in my previous research, i have always used existing data. i am trying something new as an exploratory exercise and have never create my own variable form scratch. essentially, i am creating a variable for party affiliation. here is an example. var =party. levels= democrat, republican, other. respondents will indicate which category they fall under. for the sake of ease, i will use small data as an example. i was thinking the levels would need to be created first- dem <- c(1,1,1,1,0,0,0,0) rep <- c(1,1,1,0,0,0,0,0) other <- c(1,0,0,0,0,0,0,0) then, i could do: party <-cbind(den, rep, other) par1 <-factor(party) this is where i am getting stuck... any thoughts would be appreciated. i promise this isn't homework. i am trying to understand how i would go about creating variables if i choose to go in this direction in the future... and work out the kinks now.
Hi Nicole, On Mon, Feb 18, 2013 at 7:01 PM, Nicole Ford <nicole.ford at me.com> wrote:> hello, all. > > in my previous research, i have always used existing data. i am trying something new as an exploratory exercise and have never create my own variable form scratch. > > essentially, i am creating a variable for party affiliation. > > here is an example. > > var =party. > > levels= democrat, republican, other. > > respondents will indicate which category they fall under. > > for the sake of ease, i will use small data as an example. > > i was thinking the levels would need to be created first- > > dem <- c(1,1,1,1,0,0,0,0) > > rep <- c(1,1,1,0,0,0,0,0) > > other <- c(1,0,0,0,0,0,0,0) > > then, i could do: > > party <-cbind(den, rep, other)I don't really understand what you're trying to do. At this point you have dem rep other [1,] 1 1 1 [2,] 1 1 0 [3,] 1 1 0 [4,] 1 0 0 [5,] 0 0 0 [6,] 0 0 0 [7,] 0 0 0 [8,] 0 0 0 so observation 1 is a dem, rep, and other, and observation 8 is none of these. Doesn't really make sense to me.> > par1 <-factor(party) > > this is where i am getting stuck... any thoughts would be appreciated.Start with a clear idea of what the resulting data should look like. Best, Ista> > i promise this isn't homework. i am trying to understand how i would go about creating variables if i choose to go in this direction in the future... and work out the kinks now. > > ______________________________________________ > 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.
On 02/19/2013 11:01 AM, Nicole Ford wrote:> hello, all. > > in my previous research, i have always used existing data. i am trying something new as an exploratory exercise and have never create my own variable form scratch. > > essentially, i am creating a variable for party affiliation. > > here is an example. > > var =party. > > levels= democrat, republican, other. > ... > this is where i am getting stuck... any thoughts would be appreciated. >Hi Nicole, The usual way something like this is done is to create a factor of a given size with the appropriate levels: party<-factor(sample(c("dem","rep","oth"),100,TRUE)) party [1] dem dem dem rep oth dem dem oth rep dem rep rep oth dem oth oth dem dem [19] oth rep oth rep dem oth rep dem oth oth oth oth rep dem oth dem oth rep [37] dem oth dem oth rep oth rep oth rep dem dem rep rep rep dem oth dem dem [55] oth dem rep dem rep oth rep oth dem dem dem rep dem rep dem oth rep rep [73] rep dem rep rep rep dem oth oth oth rep oth rep oth oth dem oth rep dem [91] rep rep oth dem rep dem dem oth dem dem Levels: dem oth rep Jim
Hello, Try the following. levels <- c("democrat", "republican", "other") dem <- c(1,1,1,1,0,0,0,0) rep <- c(1,1,1,0,0,0,0,0) other <- c(1,0,0,0,0,0,0,0) party <- factor(rep(levels, c(sum(dem), sum(rep), sum(other)))) party Hope this helps, Rui Barradas Em 19-02-2013 00:01, Nicole Ford escreveu:> hello, all. > > in my previous research, i have always used existing data. i am trying something new as an exploratory exercise and have never create my own variable form scratch. > > essentially, i am creating a variable for party affiliation. > > here is an example. > > var =party. > > levels= democrat, republican, other. > > respondents will indicate which category they fall under. > > for the sake of ease, i will use small data as an example. > > i was thinking the levels would need to be created first- > > dem <- c(1,1,1,1,0,0,0,0) > > rep <- c(1,1,1,0,0,0,0,0) > > other <- c(1,0,0,0,0,0,0,0) > > then, i could do: > > party <-cbind(den, rep, other) > > par1 <-factor(party) > > this is where i am getting stuck... any thoughts would be appreciated. > > i promise this isn't homework. i am trying to understand how i would go about creating variables if i choose to go in this direction in the future... and work out the kinks now. > > ______________________________________________ > 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. >