0

We have the following piecewise equality for a dataset:

tenure_new == tenure iftenure <= exper 
tenure_new == exper iftenure> exper

So the code i wrote is:

tenure_new <- ifelse(wage2$tenure <= wage2$exper,wage2$tenure, wage2$exper)

We want to get the mean value, but the answer we got is not being accepted - not sure what else to do.

I don't want the answer, just some directions on what i might be overlooking.

Here is the problem question:

PROBLEM QUESTION

user438383
  • 5,716
  • 8
  • 28
  • 43
  • 1
    Please provide a [mcve]. See [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269/4996248) for what this would mean in R. Note that an image of code, data, or problem statement is not very useful for others. – John Coleman Jan 24 '21 at 19:30
  • 3
    The definition of `tenure_new` looks fine. If the properly rounded value of its mean isn't being accepted by some automatic grading system, the problem might lie with the grading system. Your question lacks adequate context. It isn't really answerable based on the limited information that you have provided. – John Coleman Jan 24 '21 at 19:44
  • 1
    If `round(mean(tenure_new), 2)` is giving you 6.51 then the problem is not you. – Rui Barradas Jan 24 '21 at 19:50
  • Please show the code you used to get the mean value and state what that value is. – Gregor Thomas Feb 14 '22 at 19:04

1 Answers1

0

There are multiple ways that you can do it. But to keep things simple I'll show the traditional for loop approach with base R, the mapply approach with base R, and share the tidyverse approach also.

Base R Approach - Using for loops

library(wooldridge)

data(wage2)

# Traditional solution
# Make new variable
wage2$tenure_new<- NA

for(i in 1:length(wage2$tenure)){
  if(wage2$tenure[i]<= wage2$exper[i]){
    wage2$tenure_new[i]<- wage2$tenure[i]
  }else{
    wage2$tenure_new[i]<- wage2$exper[i]
  }
}

# mean of tenure_new
round(mean(wage2$tenure_new),2)
# Output> [1] 6.51

Base R Approach- using mapply

A really small amount of code, but hard to read if you don't know R well.

round(mean(mapply(function(x,y) if(x<=y){x}else{y},x=wage2$tenure,y=wage2$exper)),2)

tidyverse Approach

(Actually you can use dplyr package alone but I personally like to load tidyverse in its entirety.)

library(tidyverse)

wage2 %>% 
  transmute(tenure_new=ifelse(tenure<=exper,tenure,exper)) %>% 
  colMeans() %>% 
  round(2)

# Output> tenure_new 
#               6.51
Bensstats
  • 988
  • 5
  • 17
  • Downvoting because your 2 base R approaches are terribly unidiomatic. They are less efficient versions of the code OP has in the question. There is nothing "traditional" in R about using a for loop instead of vectorized functions. – Gregor Thomas Feb 14 '22 at 18:32
  • @GregorThomas you're entitled to your opinion, but the base R examples are my attempts to educate and provide intuition. – Bensstats Feb 14 '22 at 18:35
  • I would expect some commentary then, as I'm not clear on what concepts you're trying to illustrate or what intuition those examples provide... The for loop could be "if you're using a different language that's not vectorized, you might approach the problem this way...". The `mapply` seems like a way to obfuscate the simple `ifelse(wage2$tenure <= wage2$exper,wage2$tenure, wage2$exper)` OP posts in the question? – Gregor Thomas Feb 14 '22 at 18:39
  • I'm not trying to be mean, but your base R examples seem less readable and less efficient than what OP has in the question. So I really am confused about why you're including them. – Gregor Thomas Feb 14 '22 at 18:41
  • `round(mean(mapply(function(x,y) if(x<=y {x}else{y},x=wage2$tenure,y=wage2$exper)),2)`, which you identify as "really small amount of code but hard to read" isn't as short as OP's very readable `ifelse(wage2$tenure <= wage2$exper,wage2$tenure, wage2$exper)` (which could, of course, be wrapped in `mean()` just as easily. And `mapply` will almost certainly have more overhead than `ifelse` in this case, making it less efficient too. – Gregor Thomas Feb 14 '22 at 18:43