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

霸气de小男生 提交于 2021-02-10 06:15:20

问题


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. n.b. 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.


回答1:


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

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


来源:https://stackoverflow.com/questions/47001448/create-new-column-in-dataframe-using-if-else-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!