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.
If it is a
listyou’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):
Next, a modified function:
Let’s test it: