0

I have an 80 column Excel spreadsheet. I want to read only four of the columns. The position of the columns may change so I would like to select them by column name. I do this by

df <- read_xlsx("dat")[c("a", "b", "c", "d")]

However, d is guessed incorrectly as a logical while it is in fact a factor with four levels. The type of the columns can be set by col_type but the parameters of this function refer to the original 80 columns. Thus, to set the correct type, I would have to know the column position, which I do not. Even if, it seems cumbersome to set 80 column types (probably with rep()) although I only need four of them. Reading all as text does not work either because one column is in date:time format which gets scrambled by this. Does anyone have an idea how to solve this?

Packages with java and perl are not working on my computer.

1 Answer 1

0

fread is in data.table package and will provide the functionality you're looking for.

Described as the “fast and friendly file finagler”, the popular data.table package is an extremely useful and easy to use. Its fread() function is meant to import data from regular delimited files directly into R, without any detours or nonsense. https://www.datacamp.com/community/tutorials/importing-data-r-part-two

df<-fread("path",select=c("col1","col2",..........)) 
Sign up to request clarification or add additional context in comments.

4 Comments

Why should one use that approach? Please add some explanation to your code such that others can learn from it
hello,yes with fread function from data.table package can read any type of file
fread() function is quite useful,it can use to read large dataset in a speedy manner.
Can you show me a post or a working example where it reads .xlsx files? Because according to other posts, e.g. stackoverflow.com/questions/48313025/…, it cannot read xlsx files and google brought up no examples. It also returns an error when I try to read my file with fread().

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.