1

I've got a vector (ndx) and table (predict_all)

Vector ndx contains indexes of the predict all table

ndx is as follows:

V1 
1  
4  
5  
6 

the predict_all data table is as follows:

V1   V2   V3   V4   V5   V6
0.01 0    0.2  0.4  0.1  0
0.2  0.01 0.1  0.3  0.6  0.3

[...]

When I do: predict_all[1,1] I get 0.01 but if I do predict_all[1, ndx[1]] I get 1 when I should get 0.01.

just need to fix this.

1 Answer 1

2

We need to show the row/column index of the same length. Here, we are trying to get the value of cell at 1, 1,. The row index is correct, but column index is a data.frame with one column (ndx[1] - based on the structure showed in the OP's post). We need to extract the 'V1' column and get the first element as column index

predict_all[1, ndx$V1[1]]
#[1] 0.01

NOTE: We assume predict_all as a data.frame

If it is a data.table, then use with = FALSE

predict_all[1, ndx$V1[1], with = FALSE]
#    V1
#1: 0.01

data

ndx <- structure(list(V1 = c(1L, 4L, 5L, 6L)), .Names = "V1", 
 class = "data.frame", row.names = c(NA, -4L))

predict_all <- structure(list(V1 = c(0.01, 0.2), V2 = c(0, 0.01), 
 V3 = c(0.2, 0.1), V4 = c(0.4, 0.3), V5 = c(0.1, 0.6), 
 V6 = c(0, 0.3)), .Names = c("V1", 
 "V2", "V3", "V4", "V5", "V6"), class = "data.frame",
 row.names = c(NA, -2L))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.