Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

In R, how can I replace the values in one column with the values of another column whenever the replacing column is not empty?

$
0
0

I'm working on automating a report in R. One thing we do is look at clients who enter a shop and estimate their age record that as Estimate Age. We are instructed to use the Estimated Age as Age in the report so that all parties have an age. If we are able to record someone's Actual Age that age then becomes the Age. Most records do not have a value for Actual Age. For the records that do have an Actual Age value I need to replace the Estimated Age value with the Actual Age value whenever it exists. The records without Actual Age should remain unchanged.

I'm a newbie and have been stuck on this step for months. Asking the stackoverflow gods for a blessing. See image if it helps.

Replacing Estimate Age with Actual Age

I've already tried: Tried several variations of 2 different methods for replacing Estimated Age with Actual Age, again to no avail:

1) Age <- ifelse(is.null(MyReport$ActualAge), MyReport$ActualAge, MyReport$EstimatedAge)

View(MyReport) 2) Also something like this but I tweaked it so much so not exactly like this I messed it up

select <- is.null(MainReportload$ActualAge) < 0.01
df[select,MyReport$EstimatedAge] <- df[select, MyReport$ActualAge]

3)

if(is.null(MyReport$ActualAge)) {
  MyReport$Age <- MyReport$EstimatedAge
} else {
  MyReport$Age <- MyReport$ActualAge
  }
MyReport$Age
View(MyReport)

8.6.19 Alternative based on brain and minimal SQL knowledge, just do a coalesce, coalesce is available in the dplyr library. Result: same issue as the above attempt will continue with research.


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>