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 7763581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:41:42+00:00 2026-06-01T14:41:42+00:00

I am trying to clean up some data that has been incorrectly entered. The

  • 0

I am trying to clean up some data that has been incorrectly entered. The question for the variable allows for multiple responses out of five choices, numbered as 1 to 5. The data has been entered in the following manner (this is just an example–there are many more variables and many more observations in the actual data frame):

data
          V1
1    1, 2, 3
2    1, 2, 4
3 2, 3, 4, 5
4    1, 3, 4
5    1, 3, 5
6 2, 3, 4, 5

Here’s some code to recreate that example data:

data = data.frame(V1 = c("1, 2, 3", "1, 2, 4", "2, 3, 4, 5", 
                         "1, 3, 4", "1, 3, 5", "2, 3, 4, 5"))

What I actually need is the data to be treated more… binary–like a set of “yes/no” questions–entered in a data frame that looks more like:

data
    V1.1  V1.2  V1.3  V1.4  V1.5
1      1     1     1    NA    NA
2      1     1    NA     1    NA
3     NA     1     1     1     1
4      1    NA     1     1    NA
5      1    NA     1    NA     1
6     NA     1     1     1     1

The actual variable names don’t matter at the moment–I can easily fix that. Also, it doesn’t matter too much whether the missing elements are “O”, “NA”, or blank–again, that’s something I can fix later.

I’ve tried using the transform function from the reshape package as well as a fed different things with strsplit, but I can’t get either to do what I am looking for.
I’ve also looked at many other related questions on Stackoverflow, but they don’t seem to be quite the same problem.

  • 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-06-01T14:41:43+00:00Added an answer on June 1, 2026 at 2:41 pm

    You just need to write a function and use apply. First some dummy data:

    ##Make sure you're not using factors
    dd = data.frame(V1 = c("1, 2, 3", "1, 2, 4", "2, 3, 4, 5", 
                             "1, 3, 4", "1, 3, 5", "2, 3, 4, 5"), 
                         stringsAsFactors=FALSE)
    

    Next, create a function that takes in a row and transforms as necessary

    make_row = function(i, ncol=5) {
      ##Could make the default NA if needed
      m = numeric(ncol)
      v = as.numeric(strsplit(i, ",")[[1]])
      m[v] = 1
      return(m)
    }
    

    Then use apply and transpose the result

    t(apply(dd, 1, make_row))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to clean up an older ASP.NET WebForms site that has ViewState enabled
I was trying to clean up some accessability stuff in my code, and inadvertently
I am trying to clean up some email addresses in sql server. I can
I am trying to clean up some of the JavaScript throughout my views and
I'm trying to clean up some warnings in some old Java code (in Eclipse),
We just moved over to Git from SVN. In trying to clean up some
I know this sort of question has been asked before , but I still
I have some CSV data files that I want to import into mySQL. I
I'm trying to support multiple databases for an app that I'm writing. The app
Suppose that I have two data types Foo and Bar. Foo has fields x

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.