How to get columns from XLS files in R

Asked

Viewed 62 times

3

Using R tool, I was able to open an XLS file as follows:

require(xlsx)
coln = function(x) { 
  y = rbind(seq(1, ncol(x)))
  colnames(y) = colnames(x)
  rownames(y) = "col.number"
  return(y)
}
data = read.xlsx2(path, 1)  
coln(data)
x = 3 
data = read.xlsx2(path, 1, colClasses = c(rep("character", x), rep("numeric", ncol(data)-x+1)))

I can see everything on my spreadsheet. But I would like to get only the data from column A and then use the data from column B to calculate a function.

How can I get this separate information?

1 answer

1


The function read.xlsx2 has a parameter called colIndex where you can specify which columns (by position) you want to extract.

Thus, in the code below, for example, putting colIndex = c(1,3,4) you would extract columns 1, 3 and 4.

data = read.xlsx2(path, 1, 
                  colClasses = c(rep("character", x), 
                                 rep("numeric", ncol(data)-x+1)), 
                  colIndex = c(1,3,4))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.