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 7039913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:51:20+00:00 2026-05-28T01:51:20+00:00

I have an online survey dataset in which there are multiple complete attempts by

  • 0

I have an online survey dataset in which there are multiple complete attempts by participants and I need to selectively remove several of the cases by row number. The data is stored as a data.frame. I realize I could do this manually, but I want to keep this as a script so that I can use it later if need be or someone can duplicate what I’ve done quickly and efficiently.

What I have tried: I have searched in multiple locations, but my question seems too simple. I have looked at removing rows based on incomplete cases (‘complete.cases’ and ‘na.omit’), but this is not specifically want I want as I am trying to remove a row based on a specific vector within the data.frame

The data:

user_id var1 var2 var3
1         NA   13  bob
3       time   37 fred
4     second   NA lisa
5     second   28 lisa

So, in the above data.frame I have multiple attempts by user lisa. I want to keep her last attempt because it is more complete (no NA in var2), but I need to remove the row based on user_id rather than var3.

  • 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-28T01:51:21+00:00Added an answer on May 28, 2026 at 1:51 am

    Starting with:

    > data
      var1 var2 var3  user
    1    1   NA    2   bob
    2   34    3    1   bob
    3   NA   NA    2   bob
    4    1    2    3  lisa
    5    1   NA    2  lisa
    6    3    4    5   joe
    7    6   NA    4 simon
    

    first compute the completeness score by summing the number of non-NA values in var1 to var3:

    > data$score = apply(data[,c("var1","var2","var3")],1,function(x){sum(!is.na(x))})
    > data
      var1 var2 var3  user score
    1    1   NA    2   bob     2
    2   34    3    1   bob     3
    3   NA   NA    2   bob     1
    4    1    2    3  lisa     3
    5    1   NA    2  lisa     2
    6    3    4    5   joe     3
    7    6   NA    4 simon     2
    

    Then find the row with max(score) in each group. There’s probably an easier way to do this:

    > pick = unlist(tapply(1:7,data$user,
          function(x){x[data[x,"score"]==max(data[x,"score"])]}))
    > pick
      bob   joe  lisa simon 
        2     6     4     7 
    > data[pick,]
      var1 var2 var3  user score
    2   34    3    1   bob     3
    6    3    4    5   joe     3
    4    1    2    3  lisa     3
    7    6   NA    4 simon     2
    

    If someone has two rows with the same score they’ll appear twice:

    > data[2,'var2']=NA
    > data$score = apply(data[,c("var1","var2","var3")],1,function(x){sum(!is.na(x))})
    

    Now if I recompute pick I get bob twice:

    > pick = unlist(tapply(1:7,data$user,
        function(x){x[data[x,"score"]==max(data[x,"score"])]}))
    > pick
     bob1  bob2   joe  lisa simon 
        1     2     6     4     7 
    

    Which can be fixed by just returning the first match in the pick calculation:

    > pick = unlist(tapply(1:7,data$user,
      function(x){x[data[x,"score"]==max(data[x,"score"])][1]}))
    > pick
      bob   joe  lisa simon 
        1     6     4     7 
    

    You didn’t say what you wanted doing with duplicates…

    Someone will probably have a one-liner posted in a tic…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an online research project , and also there is lots of multiple
I have an online form which collects member(s) information and stores it into a
I have an online gateway which requires an HTML form to be submitted with
I have an online form which collects consumer data and stores in a dedicated
I am working on an online survey. Most questions have a scale of 1-5
I am building an online survey system for which I wish to produce statistics.
I have an online order entry system in silverlight. I need to send an
I have an online chair store which is using the open source e-commerce solution
I'm creating an iPhone app which is going to have an online spreadsheet to
I have an online application with PHP & MySQL. I need to provide some

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.