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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:39:24+00:00 2026-06-12T02:39:24+00:00

Right now I have a vector called closest.labels that has the following data in

  • 0

Right now I have a vector called closest.labels that has the following data in it:

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    2    2    2    2    2    2    2    2    2     2
[2,]    0    0    0    0    0    0    0    0    0     0
[3,]    9    9    9    9    9    9    9    7    7     4

What I would like to do is return the row data as well as the index of that row where there are more than two unique values. In the above example this would only be the third row. So far I have been partially successful using apply and a function that I created. See below:

colCountFx <- function(col){
    result <- subset(list(index=col,count=length(unique(col))),length(unique(col))>2)
    return(result)
}
apply(closest.labels,1, colCountFx)

My issue is that this returns what appears to be an empty row for the first two records as well. Output:

[[1]]
named list()

[[2]]
named list()

[[3]]
[[3]]$index
 [1] 9 9 9 9 9 9 9 7 7 4

[[3]]$count
[1] 3

What would I need to change to have nothing returned for the rows that are currently returning named list()? Also, I am fairly new to R so if you think there is a better way to go at this I am open to that as well.

  • 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-06-12T02:39:25+00:00Added an answer on June 12, 2026 at 2:39 am

    If it is a list you’re going for, you can try something like this. Personally, though, I find nested lists somewhat cumbersome.

    First, some data (I’ve added an extra row for clarity):

    closest.labels <- structure(c(2, 0, 9, 8, 2, 0, 9, 8, 2, 0, 9, 8, 2, 0, 9, 8, 2, 
                                  0, 9, 8, 2, 0, 9, 5, 2, 0, 7, 6, 2, 0, 7, 7, 2, 0, 
                                  4, 8, 2, 0, 4, 9), .Dim = c(4L, 10L))
    

    Next, a modified function:

    colCountFx <- function(data) {
      temp = apply(data, 1, function(x) length(unique(x)))
      result = which(temp > 2)
      out = vector("list")
      for (i in 1:length(result)) {
        out[[i]] = list(index = data[result[i], ], count = temp[result[i]])
      }
      names(out) = paste("row", result, sep = "_")
      out
    }
    

    Let’s test it:

    colCountFx(closest.labels)
    # $row_3
    # $row_3$index
    # [1] 9 9 9 9 9 9 7 7 4 4
    # 
    # $row_3$count
    # [1] 3
    # 
    # 
    # $row_4
    # $row_4$index
    # [1] 8 8 8 8 8 5 6 7 8 9
    # 
    # $row_4$count
    # [1] 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector that that stores Node*. A Node has the member properties:row,col,value.
Simple question; right now I have something like this: typedef void(*MyFunctionPointer)(int); typedef std::vector <
Right now I have two data frames in R, contains some data that looks
Right now I have a script that will get the last five files in
Right now I have a function, in a class that is used to listen
Right now I have a link that causes a hidden div to appear when
Right now I have a foreach loop that grabs the first link with an
Right now I have a PHP file that does a MYSQL query and then
Have a vector of vectors that looks something like this 3 1 2 0
First I want to say that, I have a vector which has thousand of

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.