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:
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)
within
can save you a lot of typing - particularly if your dataframe has a long nameproblem
, yet in your loop, you are writing to dflapipofficial$problem
which is a different variable.+(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.