-3

I have two lists A and B

A is as follows:

Index | Value
   16 | 17
   22 | 17
    1 | 6
   32 | 17

List B is as follows:

Index | Value
   16 | 17
   22 | 17
    1 | 17
   32 | 17

In the list, number of times where A[i] == B[i] is 3. How to display this value? If the arrays were in order (indexes = 0,1,2,3) I would do the following:

count = 0
for(i in 0:3){
    if(A[i] == B[i]){
        count = count + 1
    }
}

But since the list indexes are not in order I am not sure how to proceed.

EDIT (details about what I am trying to do):

Here A and B are dataframes that refer to n_pred and n_test (see code below).

I am using the Iris dataset in my code. Here, I am attempting to build a neuralnet to test prediction accuracy for the dataset. I have split the data into testing and training data (20:80 ratio). The code for this is as follows:

n_data <- cbind(data, as.data.frame.matrix(table(rownames(data), data$Species)))

n_trainIndex <- sample(1:nrow(n_data),  0.8 *   nrow(n_data))
n_train <- n_data[n_trainIndex,]
n_test <- n_data[-n_trainIndex,]

This is how I get the random indexes for the array.

Next I have built the neural net as follows:

nn = neuralnet(setosa + versicolor + virginica ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, data =n_train, hidden = c(2))
plot(nn)
n_pred = compute(nn, n_test[1:4])$net.result

getValue = function(list){
  if(list[1] == list[4]){
    return ('setosa')
  }
  else if(list[2] == list[4]){
    return ('versicolor')
  }
  else{
    return ('virginica')
  }
}
n_pred = as.data.frame(n_pred)
n_pred[,'result'] = apply(n_pred[1:3], 1, max)
n_pred[,'Species'] = apply(n_pred, 1, getValue)

I want to compare n_pred$Species with n_test$Species so I can calculate the accuracy of the model.

Arat254
  • 449
  • 2
  • 5
  • 17
  • Are these data.frames? If so, indexing them with a single number will return a column, not a row. If not, specify what data structure you've got. Better yet, make your example [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – alistaire Oct 17 '16 at 22:25
  • They are dataframes. I will edit my question shortly. – Arat254 Oct 17 '16 at 22:27

3 Answers3

2

Here's a possible solution.

> length(intersect(paste0(df1$Index, df1$Value), paste0(df2$Index, df2$Value)))
[1] 3

where df1 and df2 are data.frames not list, and they look like:

> df1
  Index Value
1    16    17
2    22    17
3     1     6
4    32    17
> df2
  Index Value
1    16    17
2    22    17
3     1    17
4    32    17
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
0

merge to find the inner join of the two data.frames, and count the rows:

nrow(merge(A, B))
## [1] 3

Data

A <- structure(list(Index = c(16L, 22L, 1L, 32L), Value = c(17L, 17L, 
    6L, 17L)), .Names = c("Index", "Value"), class = "data.frame", row.names = c("1", 
    "2", "3", "4"))

B <- structure(list(Index = c(16L, 22L, 1L, 32L), Value = c(17L, 17L, 
    17L, 17L)), .Names = c("Index", "Value"), class = "data.frame", row.names = c("1", 
    "2", "3", "4"))
alistaire
  • 42,459
  • 4
  • 77
  • 117
0

Try this:

sum(rowSums(A==B)==ncol(A))
#[1] 3
989
  • 12,579
  • 5
  • 31
  • 53