I'm trying to find a way to store 2 values in one index of a vector.
I have a matrix that I'm translating into vector coordinates, so that I can take a random sample of that vector and then translate the location of those samples back into matrix coordinates.
filter_function<-function(df,perc){
rows<-dim(df)[1]
cols<-dim(df)[2]
vec<-vector("list",rows*cols)
for(i in 1:rows){
for(j in 1:cols){
vec[(i-1)*cols+j]<-df[i,j]
}
}
n<-rows*cols
filter<-sample(vec,n*perc)
}
The problem I'm having is that the function sample doesn't return the vector coordinate and also I don't know how to get the row and column values translated back to me. I'm wondering if there's an alternate method where I would change line 8 to look something like this:
vec[(i-1)*cols+j]<-c(i,j)
This obviously gives me the error message
In vec[(i - 1) * cols + j] <- c(i, j) : number of items to replace is not a multiple of replacement length
So I'm wondering if there's something similar I can do? Once I have the coordinates, I need to ideally be able to remove the values in those positions in a quick step, so something like
df<-df[-filter]
Note: My data has a lot of repeats of 0s and 1s and everything in between, so it wouldn't work to take a random sample and then use the which or match functions.
Please help!
df(I,j)This clearly defies the syntax of R.. I doubt whether df is a function..as per your definition. since you do havedim(df)- Onyambu