Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5943959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:28:11+00:00 2026-05-22T16:28:11+00:00

hopefully this is not a too dumb question, but still being an R beginner

  • 0

hopefully this is not a too dumb question, but still being an R beginner I have a serious problem with tapply. Lets say

factors <- as.factor( c("a", "b", "c", "a", "b", "c", "a", "b", "c") )
values  <- c( 1, 2, 3, 4, 5, NA, 7, NA, NA )
tapply(
  values,
  factors,
  function(x){
    if( sum(is.na(x)) == 1 ){
      x[ is.na(x) ] <- 0
    }
    return(x)
  }
)

The result is

$a
[1] 1 4 7

$b
[1] 2 5 0

$c
[1]  3 NA NA

However, what I need is to get a vector back which preserves the original order of values, i.e.:

c( 1,2,3,4,5,NA,7,0,NA )

Many thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:28:12+00:00Added an answer on May 22, 2026 at 4:28 pm

    In that case you should use the ave function:

    > ave(values, factors, FUN=function(x) {
    +     if( sum(is.na(x)) == 1 ){
    +       x[ is.na(x) ] <- 0
    +     }
    +     return(x)
    +   }
    + )
    [1]  1  2  3  4  5 NA  7  0 NA
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hopefully this one is not too hard to understand but I just want the
Hopefully this won't be too difficult, but I'm not too skilled in regular expressions.
I have a question about sharing data between views, hopefully it's not too basic.
I'm not too sure on how to explain this but hopefully you get what
hopefully this doesn't sound ridiculous, but are some parts of the web.config not required
Hello! I'm not even sure if this is possible, but hopefully it is in
Hopefully this question isn't as subjective as I think it may be. I have
This is not quite a programming question, but it's about a programming environment, so
Hopefully this question won't be too convoluted or vague. I know what I want
Hopefully not too tricky this one... I am trying to get just the context

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.