简体   繁体   中英

Create new column in dataframe using if {} else {} in R

I'm trying to add a conditional column to a dataframe, but not getting the results I'm expecting.

I have a dataframe with values recorded for the column "steps" across 5-minute intervals over various days. I'm trying to impute missing values in the 'steps' column by using the mean number of steps for a given 5-minute interval on the days that do have measurements. nb I tried using the MICE package for this but it just crashed my computer so I opted for a more manual workaround.

As an intermediate stage, I have bound an additional column to the existing dataframe with the mean number of steps for that interval. What I want to do next is create a column that returns that mean if the raw number of steps is NULL, and just uses the raw value if not null. Here's my code for that part:

activityTimeAvgs$stepsImp <- if(is.na(activityTimeAvgs$steps)){
  activityTimeAvgs$avgsteps
} else {
  activityTimeAvgs$steps
}

What I expected to happen is that the if statement would evaluate as TRUE if 'steps' is NA and consequently give 'avgsteps'; in cases where 'steps' is not NA I would expect it to just use the raw value for 'steps'. However, the output just gives the value for 'avgsteps' in every row, which is not much use. I also get the following warning:

Warning message:
In if (is.na(activityTimeAvgs$steps)) { :
  the condition has length > 1 and only the first element will be used

Any ideas where I'm going wrong?

Thanks in advance.

The if statement is not suitable for this. You need to use ifelse :

activityTimeAvgs$stepsImp <- ifelse(is.na(activityTimeAvgs$steps), activityTimeAvgs$avgsteps, activityTimeAvgs$steps)

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