I have a problem where I must delete certain columns in an Excel file depending on some cell values using R.
I imported the Excel file
data = as.matrix(read.csv('data.csv', header = FALSE))
Dimensions of the matrix are:
dim(data) = 106474 * 81
What I need to do
- If a value in any of the cells
[row = 1, column = i] = 'A' ->
delete column - If a value in any of the cells
[row = 1, column = i] = 'B' ->
delete column - If a value in any of the cells
[row = 2, column = i] > 30 ->
delete column
- If a value in any of the cells
What I did so far
for (i in 1:ncol(data)) { if (data[1,i]=='A') { data = data [,-i] } else if (data[1,i]=='B') { data = data [,-i] } else if (data[2,i] > 30) { data = data [,-i] } }
Problems I ran into are as follows:
When I read the matrix to r, it read everything as characters? how to convert data[2,i] row to numeric. I have provided how I converted it to numeric. But is there any better way?
row2 = as.numeric(as.matrix(fly_data[2,])) fly_data[2,] <- row2
My for loop end abruptly and it doesn't run to the full length of
ncol(data)
It always gives the
Error in data[1, i] : subscript out of bounds