[R] Read excel specific column

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Wed Aug 24 07:40:00 CEST 2022


Hello,

The examples below use packages readxl and cellranger.


# to read the 1st column of a xlsx file named filename:
library(readxl)

read_xlsx(filename, range = cellranger::cell_cols("A"))

# to read 1st column of all files in filenames_vec
# result is a list of data.frames each of them with
# one column only
gene_names_list <- lapply(filenames_vec, \(x) {
   read_xlsx(x, range = cellranger::cell_cols("A"))
})

# to read 1st column of all files in filenames_vec
# result is a vector
gene_names_vec <- lapply(filenames_vec, \(x) {
   read_xlsx(x, range = cellranger::cell_cols("A"))[[1]]
})
gene_names_vec <- unlist(gene_names_vec)



If the files are xls, not xlsx, use read_xls; if you don't know, 
read_excel will call the right function.


Hope this helps,

Rui Barradas


Às 00:49 de 24/08/2022, Anas Jamshed escreveu:
> I have .xlsx files with gene names in first column.How can read and load in
> R?
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list