Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6873655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:05:46+00:00 2026-05-27T04:05:46+00:00

I am working on a large dataset analysing survey data. From the data, I

  • 0

I am working on a large dataset analysing survey data. From the data, I am producing cross-tabulations for different variables (c1, c2, c3). I am writing code whereby R will automatically pick out the “yes” value in the xtab, and give this a letter to use in subsequent analysis.

My problem starts where there are just yes or no values in the dataframe. When the cross tabulation is done, obviously, only the “no” or “yes” value is picked out.

Df1 <- data.frame(c = 1:4, c1 = c("yes","yes","yes","yes"), c2 = c("yes", "no", "no", "no"), c3 = c("no", "no", "no", "no"), weight = c(1.1, 1.2, 1.4, 0.8))
x<-xtabs(weight~c3,data=Df1)
y<-xtabs(weight~c2,data=Df1)
z<-xtabs(weight~c1,data=Df1)

When I try to assign a letter, the the output of the cross tabs, obviously it only works for the xtab that has both yes and no answers (b).

a<-x[2]
b<-y[2]
c<-z[2]

To get round this I tried using an “if” function, but it still is working yet. So, if there are yes answers in the xtab, this value should always be used, and just the no value is given, then a 0 should be assigned.

x1<-as.data.frame(x)
a<-if(x1$c3=="yes") x[2] else 0

y1<-as.data.frame(y)
b<-if(y1$c2=="yes") y[2] else 0

z1<-as.data.frame(z)
c<-if(z1$c1=="yes") z[2] else 0

I should get the answers a=0, b=1.1 and c=0, but so far, but limited r knowledge is not getting me very far indeed. any help would be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:05:47+00:00Added an answer on May 27, 2026 at 4:05 am

    A factor a day keeps the doctor away. If you convert your data to factors, the R mechanism to keep track of categorical data, your task will be much easier.

    To convert a vector to a factor, use factor. If you know in advance what the factor levels should be, specify that with the levels argument.

    > factor(Df1$c3, levels=c("yes", "no"))
    [1] no no no no
    Levels: yes no
    

    You can apply this in a single statement to all of the necessary vectors with lapply:

    > Df1[, 2:4] <- lapply(Df1[, 2:4], function(x)factor(x, levels=c("yes", "no")))
    > str(Df1)
    'data.frame':   4 obs. of  5 variables:
     $ c     : int  1 2 3 4
     $ c1    : Factor w/ 2 levels "yes","no": 1 1 1 1
     $ c2    : Factor w/ 2 levels "yes","no": 1 2 2 2
     $ c3    : Factor w/ 2 levels "yes","no": 2 2 2 2
     $ weight: num  1.1 1.2 1.4 0.8
    

    Then your xtab will return the cross table with all the factor levels:

    > xtabs(weight~c3, data=Df1)
    c3
    yes  no 
    0.0 4.5 
    
    > xtabs(weight~c1, data=Df1)
    c1
    yes  no 
    4.5 0.0 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a large dataset which comprises travel behaviour data over a
We are working with large amounts of data, all tagged in UTC (in Java).
Background: I am writing a C++ program working with large amounts of geodata, and
I often jump from a file to file, while working a large codebase. I
I am working with a large Java web application from a commercial vendor. I've
Working with a relatively large data set I have a situation where I need
I'm working with a large hierarchical data set in sql server - modelled using
We're often working on a project where we've been handed a large data set
i am working with a very large data set (786,432 rows to be precise).
Im working with a large dataset of point of interest (POI) which all have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.