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

  • Home
  • SEARCH
  • 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 9219269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:04:38+00:00 2026-06-18T03:04:38+00:00

Background: I’m using some Census public use microdata samples (the American Community Survey in

  • 0

Background: I’m using some Census public use microdata samples (the American Community Survey in particular) across several years to examine the behavior of people who have completed different degrees (e.g., high school diploma, bachelor’s degree, master’s degree). The variable with that public use file is called "Schooling". The problem is that the codes that are contained within the variable "Schooling" have changed from year to year. For example, for the files up through 2007, a value of "13" reflects completing a bachelor’s degree, but starting in 2008 the value changes to "21" when someone has completed their bachelor’s degree.

Goal: To create a new "Degree Competed" variable that translates the "Schooling" codes to reflect the degree level completed, taking into account the year of the file.
Logistics: The files for all years have been concatenated and, for review purposes, I have to work with the file as is rather than correcting it before it gets to this point.

Existing Code: Here is what I tried.

if      (original.file$year %in% c(2000,2001)) {
    if      (original.file$Schooling <= 08) {original.file$degree.completed <- 0}
    else if (original.file$Schooling <= 10) {original.file$degree.completed <- 1}
    else if (original.file$Schooling <= 12) {original.file$degree.completed <- 2}
    else if (original.file$Schooling == 13) {original.file$degree.completed <- 3}
    else if (original.file$Schooling == 14) {original.file$degree.completed <- 4}
    else if (original.file$Schooling == 15) {original.file$degree.completed <- 5}
    else if (original.file$Schooling == 16) {original.file$degree.completed <- 6}
    }
else if (original.file$year %in% c(2002,2003,2004,2005,2006,2007)) {
    if      (original.file$Schooling <= 08) {original.file$degree.completed <- 0}
    else if (original.file$Schooling <= 11) {original.file$degree.completed <- 1}
    else if (original.file$Schooling == 12) {original.file$degree.completed <- 2}
    else if (original.file$Schooling == 13) {original.file$degree.completed <- 3}
    else if (original.file$Schooling == 14) {original.file$degree.completed <- 4}
    else if (original.file$Schooling == 15) {original.file$degree.completed <- 5}
    else if (original.file$Schooling == 16) {original.file$degree.completed <- 6}
    }
else if (original.file$year %in% c(2008,2009,2010,2011)) {
    if      (original.file$Schooling <= 15) {original.file$degree.completed <- 0}
    else if (original.file$Schooling <= 19) {original.file$degree.completed <- 1}
    else if (original.file$Schooling == 20) {original.file$degree.completed <- 2}
    else if (original.file$Schooling == 21) {original.file$degree.completed <- 3}
    else if (original.file$Schooling == 22) {original.file$degree.completed <- 4}
    else if (original.file$Schooling == 23) {original.file$degree.completed <- 5}
    else if (original.file$Schooling == 24) {original.file$degree.completed <- 6}
    }

Problem: I get the following warning messages of this type.

Warning messages:

1: In if (original.file$year %in% c(2000, 2001)) { : the condition has length > 1 and only the first element will be used

2: In if (original.file$Schooling <= 8) { : the condition has length > 1 and only the first element will be used

3: In if (original.file$Schooling <= 10) { : the condition has length > 1 and only the first element will be used

Question: I know that there is a vector vs scalar issue here with the "if", as I’ve seen from other questions on StackOverflow, but the answers do not seem to apply to this situation. What is the solution here?

  • 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-18T03:04:40+00:00Added an answer on June 18, 2026 at 3:04 am

    First, use cut or a table instead of all those if‘s and else‘s:

    CutOffs1 <- c(0,8,10,12,13,14,15,16)
    CutOffs2 <- c(0,8,11,12,13,14,15,16)
    CutOffs3 <- c(0,15,19,20,21,22,23,24)
    CutOffs <- cbind(CutOffs1, CutOffs2, CutOffs3)
    MyTable <- apply(CutOffs, 2, function(X) cut(1:24, X, FALSE)-1)
    
          CutOffs1 CutOffs2 CutOffs3
     [1,]        0        0        0
     [2,]        0        0        0
     [3,]        0        0        0
     [4,]        0        0        0
     [5,]        0        0        0
     [6,]        0        0        0
     [7,]        0        0        0
     [8,]        0        0        0
     [9,]        1        1        0
    [10,]        1        1        0
    [11,]        2        1        0
    [12,]        2        2        0
    [13,]        3        3        0
    [14,]        4        4        0
    [15,]        5        5        0
    [16,]        6        6        1
    [17,]       NA       NA        1
    [18,]       NA       NA        1
    [19,]       NA       NA        1
    [20,]       NA       NA        2
    [21,]       NA       NA        3
    [22,]       NA       NA        4
    [23,]       NA       NA        5
    [24,]       NA       NA        6
    

    You will also want to cut the years into factors.

    original.file$Period <- cut(original.file$year, c(2000,2001, 2007, 2011), FALSE,   
                                include.lowest=TRUE) 
    ## To demonstrate:
        > cbind(2000:2011, cut(2000:2011, c(2000,2001, 2007, 2011), FALSE,   
    +     include.lowest=TRUE))
          [,1] [,2]
     [1,] 2000    1
     [2,] 2001    1
     [3,] 2002    2
     [4,] 2003    2
     [5,] 2004    2
     [6,] 2005    2
     [7,] 2006    2
     [8,] 2007    2
     [9,] 2008    3
    [10,] 2009    3
    [11,] 2010    3
    [12,] 2011    3
    

    Then you should be able to do:

    Degrees <- apply(original.file, 1, function(X) MyTable[X['Schooling'], X['Period']])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background I've recently started to develop some code using the NDK, and I've thought
Background: I am using HtmlAgilityPack (.Net), so I'm forced to use XPath 1.0, which
Background: I'm using the (fantastic) Vim plugin python-mode , which includes the pep8 linter.
Background - I am using paramiko to put files on a bunch of remote
Background : I'm trying to convert some JavaScript code which uses the the Crossfilter
Background: Using unix, codeigniter from localhost. I'd like to run a controllers via a
Background I am trying to build a Java program that make use of an
BackGround : I have developed a SenchaTouch application using sencha 2.1. Now I am
BACKGROUND I am automating an PowerPoint 2007 via C# I am writing unittests using
Background: I have uploaded a xls file using the FileField. Now I want to

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.