Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206316

R function to combine four variables?

$
0
0

I have 4 variables (races, asian_news,black_news,nhpi_news, and latino_news).

'races' is a factor with 6 levels: White, Asians, NHPI, Black, Latino, Multiracial.

'asian_news','black_news','nhpi_news', and 'latino_news' are a series of survey questions that have 4 outcomes: [1] ethnic, [2] mainstream, [3] both, and [4] DK.

These questions ask respondents if they primarily get their news through ethnic sources or through U.S mainstream media. These survey questions operate as follows:

  • only the Asian-Americans group are asked asian_news
  • only NHPI are asked nhpi_news
  • only Latinos are asked latino_news
  • only Blacks are asked black_news

The replication data can be downloaded here:

library(foreign)
pre<-read.csv("https://www.dropbox.com/s/wzitbwr6q2i26gt/sampledata.csv?dl=1")

As of now, the cross-tab between races and asian_news look like this:

> with(pre,table(races,asian_news,useNA="always"))
                                   asian_news
races                           ethnic mainstream both  DK <NA>
  3. WHITES                          0          0    0   0  500
  1. ASIAN AMERICANS               770        863  294  41  142
  2. PACIFIC ISLANDERS               0          0    0   0  410
  4.BLACKS OR AFRICAN AMERICANS      0          0    0   0  520
  6. latinos                         0          0    0   0  514
  9. MULTIRACIAL AMERICANS           0          0    0   0    0
  <NA>                               0          0    0   0    0

Similarly, the cross-tab between races and black_news look like this:

> with(pre,table(races,black_news,useNA="always"))
                               black_news
races                           ethnic mainstream both   DK <NA>
  3. WHITES                          0          0    0    0  500
  1. ASIAN AMERICANS                 0          0    0    0 2110
  2. PACIFIC ISLANDERS               0          0    0    0  410
  4.BLACKS OR AFRICAN AMERICANS     53        366   67   12   22
  6. latinos                         0          0    0    0  514
  9. MULTIRACIAL AMERICANS           0          0    0    0    0
  <NA>                               0          0    0    0    0

One could generate similar crosstabs with the following codes:

with(pre,table(races,latino_news,useNA="always"))
with(pre,table(races,nhpi_news,useNA="always"))

I want to combine these four survey questions to one unified variable. Ideally, the crosstabs between races and the desired variable would look like this

> with(pre,table(races,desired_variable,useNA="always"))
                               desired_variable
races                           ethnic mainstream both   DK <NA>
  3. WHITES                          0        500    0    0    0
  1. ASIAN AMERICANS               770        863  294   41  142
  2. PACIFIC ISLANDERS              22        332   24   13   19
  4.BLACKS OR AFRICAN AMERICANS     53        366   67   12   22
  6. latinos                       142        302   47    1   22 
  9. MULTIRACIAL AMERICANS           0          0    0    0    0
  <NA>                               0          0    0    0    0

How do I generate the "desired_variable" variable? Thanks so much in advance.


Viewing all articles
Browse latest Browse all 206316

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>