简体   繁体   中英

How to create a new variable in R that returns 1 if a case has a missing value while another variable has an observed value?

I have two variables containing missing data loon and profstat . For a better overview of the data that are missing and are needed to impute, I wanted to create an additional variable problem in the data frame, that would return for each case 1 if loon is missing and profstat is observed, and 0 if otherwise. I have generated the following code, which only gives me as output x[] = 1 . Any solution to this problem?

 {
  problem <- dim(length(t))
  for (i in 1:nrow(dflapopofficial))
  {
    if (is.na(dflapopofficial$loon[i])==TRUE & is.na(dflapopofficial$profstat[i])==FALSE) {  
      dflapopofficial$problem[i]=1
    } else {
      dflapopofficial$problem[i]=0
    }
    return(problem)
  }

There are a few things that could be improved here:

  1. Remember, many operations in R are vectorized. You don't need to loop through each element in a vector when doing logical checks etc.
  2. is.na(some_condition) == TRUE is just the same as is.na(some_condition) and is.na(some_condition) == FALSE is the same as .is.na(some_condition)
  3. If you want to write a new column inside a dataframe, and you are referring to several variables in that dataframe, using within can save you a lot of typing - particularly if your dataframe has a long name
  4. You are returning problem , yet in your loop, you are writing to dflapipofficial$problem which is a different variable.
  5. If you want to write 1s and 0s, you can implicitly convert logical to numeric using +(logical_vector)

Putting all this together, you can replace your whole loop with a single line:

within(dflapopofficial, problem <- +(is.na(loon) & !is.na(profstat)))

Remember to store the result, either back to the dataframe or to a copy of it, like

df <- within(dflapopofficial, problem <- +(is.na(loon) & !is.na(profstat)))

So that df is just a vopy of dflapopofficial with your extra column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM