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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:31:08+00:00 2026-06-03T08:31:08+00:00

I want to create a new variable that is equal to the value of

  • 0

I want to create a new variable that is equal to the value of one of two other variables, conditional on the values of still other variables. Here’s a toy example with fake data.

Each row of the data frame represents a student. Each student can be studying up to two subjects (subj1 and subj2), and can be pursuing a degree ("BA") or a minor ("MN") in each subject. My real data includes thousands of students, several types of degree, about 50 subjects, and students can have up to five majors/minors.

df <- data.frame(
  ID = 1:20,
  subj1 = factor(c(
    "SCI", NA, "BUS", "ENG", "ENG", "SCI", "ENG", "BUS", "ENG",
    "ENG", "BUS", "ENG", "BUS", "BUS", "BUS", "SCI", "SCI", "BUS",
    "ENG", "BUS"
  )),
  degree1 = factor(rep(c("MN", NA, "BA", "MN", "BA"), c(1L, 1L, 3L, 2L, 13L))),
  subj2 = factor(c(
    "BUS", "ENG", NA, NA, "BUS", NA, "SCI", "ENG", NA, "ENG", "ENG",
    "BUS", "SCI", NA, "ENG", "BUS", "BUS", NA, "ENG", "ENG"
  )),
  degree2 = factor(c(
    "MN", "MN", NA, NA, "MN", NA, "BA", "MN", NA, "MN", "BA", "BA",
    "MN", NA, "BA", "MN", "MN", NA, "BA", "MN"
  ))
)
df
#>    ID subj1 degree1 subj2 degree2
#> 1   1   SCI      MN   BUS      MN
#> 2   2  <NA>    <NA>   ENG      MN
#> 3   3   BUS      BA  <NA>    <NA>
#> 4   4   ENG      BA  <NA>    <NA>
#> 5   5   ENG      BA   BUS      MN
#> 6   6   SCI      MN  <NA>    <NA>
#> 7   7   ENG      MN   SCI      BA
#> 8   8   BUS      BA   ENG      MN
#> 9   9   ENG      BA  <NA>    <NA>
#> 10 10   ENG      BA   ENG      MN
#> 11 11   BUS      BA   ENG      BA
#> 12 12   ENG      BA   BUS      BA
#> 13 13   BUS      BA   SCI      MN
#> 14 14   BUS      BA  <NA>    <NA>
#> 15 15   BUS      BA   ENG      BA
#> 16 16   SCI      BA   BUS      MN
#> 17 17   SCI      BA   BUS      MN
#> 18 18   BUS      BA  <NA>    <NA>
#> 19 19   ENG      BA   ENG      BA
#> 20 20   BUS      BA   ENG      MN

Now I want to create a sixth variable, df$major, that equals the value of subj1 if subj1 is the student’s primary major, or the value of subj2 if subj2 is the primary major. The primary major is the first subject with degree equal to "BA". I tried the following code:

df$major[df$degree1 == "BA"] = df$subj1
df$major[df$degree1 != "BA" & df$degree2 == "BA"] = df$subj2

Unfortunately, I got an error message:

> df$major[df$degree1 == "BA"] = df$subj1
Error in df$major[df$degree1 == "BA"] = df$subj1 : 
  NAs are not allowed in subscripted assignments

I assume this means that a vectorized assignment can’t be used if the assignment evaluates to NA for at least one row.

I feel like I must be missing something basic here, but the code above seemed like the obvious thing to do and I haven’t been able to come up with an alternative.

  • 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-03T08:31:09+00:00Added an answer on June 3, 2026 at 8:31 am

    Your original method of assignment is failing for at least two reasons.

    1) A problem with the subscripted assignment df$major[df$degree1 == "BA"] <-. Using == can produce NA, which is what prompted the error. From ?"[<-": “When replacing (that is using indexing on the lhs of an assignment) NA does not select any element to be replaced. As there is ambiguity as to whether an element of the rhs should be used or not, this is only allowed if the rhs value is of length one (so the two interpretations would have the same outcome).” There are many ways to get around this, but I prefer using which:

    df$major[which(df$degree1 == "BA")] <-
    

    The difference is that == returns TRUE, FALSE and NA, while which returns the indices of an object that are TRUE

    > df$degree1 == "BA"
     [1] FALSE    NA  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
    
    > which(df$degree1 == "BA")
     [1]  3  4  5  8  9 10 11 12 13 14 15 16 17 18 19 20
    

    2) When you perform a subscripted assignment, the right hand side needs to fit into the left hand side sensibly (this is the way I think of it). This can mean left and right hand sides of equal length, which is what your example seems to imply. Therefore, you would need to subset the right hand side of the assignment as well:

    df$major[which(df$degree1 == "BA")] <- df$subj1[which(df$degree1 == "BA")]
    

    I hope that clarifies why your original attempt produced an error.

    Using ifelse, as suggested by @DavidRobinson, is a good way of doing this type of assignment. My take on it:

    df$major2 <- ifelse(df$degree1 == "BA", df$subj1, ifelse(df$degree2 == "BA",
      df$subj2,NA))
    

    This is equivalent to

    df$major[which(df$degree1 == "BA")] <- df$subj1[which(df$degree1 == "BA")]
    df$major[which(df$degree1 != "BA" & df$degree2 == "BA")] <- 
      df$subj2[which(df$degree1 != "BA" & df$degree2 == "BA")]
    

    Depending on the depth of the nested ifelse statements, another approach might be better for your real data.


    EDIT:

    I was going to write a third reason for the original code failing (namely that df$major wasn’t yet assigned), but it works for me without having to do that. This was a problem I remember having in the past, though. What version of R are you running? (2.15.0 for me.) This step is not necessary if you use the ifelse() approach. Your solution is fine when using [, although I would have chosen

    df$major <- NA
    

    To get the character values of the subjects, instead of the factor level index, use as.character() (which for factors is equivalent to and calls levels(x)[x]):

    df$major[which(df$degree1 == "BA")] <- as.character(df$subj1)[which(df$degree1 == "BA")]
    df$major[which(df$degree1 != "BA" & df$degree2 == "BA")] <- 
      as.character(df$subj2)[which(df$degree1 != "BA" & df$degree2 == "BA")]
    

    Same for the ifelse() way:

    df$major2 <- ifelse(df$degree1 == "BA", as.character(df$subj1),
      ifelse(df$degree2 == "BA", as.character(df$subj2), NA))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two variables that are LPCWSTR s. I want to create a new
I want to create a new variable based on whatever decimal values a variable
I want to create a new column in a MYSQL table that has Fn
I want to create a new class that is a special type of string.
I want to create a multilne variable that will be split over several-lines, indenting
I want to create new node of BeanTreeView, and when I add some node
I want to create a new business application using the Django framework. Any suggestions
I want to create a new Subversion repository through Aptana Studio. Both Aptana SVN
I want to create a new Menu in the admin panel of Wordpress for
i want to create a new '.b' div appendTo document.body, and it can dragable

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.