readr: Passing a string of column classes
I want to use readr::read_csv() to read several data sets.
I have a CSV with the description of each file that looks like this:
# A tibble: 5 x 8
Object_Name File_Path Column1 Column2 Column3 Column4 Column5 Column6
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 Object1 data/file1.csv character character character numeric numeric numeric
2 Object2 data/file2.csv character character character numeric NA NA
3 Object3 data/file3.csv character character numeric NA NA NA
4 Object4 data/file4.csv character numeric numeric numeric NA NA
5 Object5 data/file5.csv character numeric numeric NA NA NA
What I wish if for something like this to work:
for (i in 1:nrow(list_of_files)) {
obj_name <- list_of_files[i,]$Object_Name
pth <- list_of_files[i,]$File_Path
col_types <- as.character(list_of_files[i,3:8])
col_types <- col_types[!is.na(col_types)]
# This part doesnt work
tab <- readr::read_csv(file = pth,
col_types = col_types)
assign(obj_name,tab)
}
Usually the guessing works, but I need to make sure.
EDIT
Simplifying the questions, so maybe it's clearer.
To specify the columns types on read_csv, you need a cols() call.
This works:
> cols('c', 'n')
cols(
= col_character(),
= col_number()
)
This doesn't work:
> aa <- c('c','n')
> cols(aa)
Error in switch(x, `_` = , `-` = col_skip(), `?` = col_guess(), c = col_character(), :
EXPR must be a length 1 vector
Because I'm iterating through a bunch of files, I need something that looks like the second example. I can save somewhere a string with the columns specifications, but how do I turn a string into a cols() object?
r readr
add a comment |
I want to use readr::read_csv() to read several data sets.
I have a CSV with the description of each file that looks like this:
# A tibble: 5 x 8
Object_Name File_Path Column1 Column2 Column3 Column4 Column5 Column6
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 Object1 data/file1.csv character character character numeric numeric numeric
2 Object2 data/file2.csv character character character numeric NA NA
3 Object3 data/file3.csv character character numeric NA NA NA
4 Object4 data/file4.csv character numeric numeric numeric NA NA
5 Object5 data/file5.csv character numeric numeric NA NA NA
What I wish if for something like this to work:
for (i in 1:nrow(list_of_files)) {
obj_name <- list_of_files[i,]$Object_Name
pth <- list_of_files[i,]$File_Path
col_types <- as.character(list_of_files[i,3:8])
col_types <- col_types[!is.na(col_types)]
# This part doesnt work
tab <- readr::read_csv(file = pth,
col_types = col_types)
assign(obj_name,tab)
}
Usually the guessing works, but I need to make sure.
EDIT
Simplifying the questions, so maybe it's clearer.
To specify the columns types on read_csv, you need a cols() call.
This works:
> cols('c', 'n')
cols(
= col_character(),
= col_number()
)
This doesn't work:
> aa <- c('c','n')
> cols(aa)
Error in switch(x, `_` = , `-` = col_skip(), `?` = col_guess(), c = col_character(), :
EXPR must be a length 1 vector
Because I'm iterating through a bunch of files, I need something that looks like the second example. I can save somewhere a string with the columns specifications, but how do I turn a string into a cols() object?
r readr
Difficult to find an answer without seeing the exact error message, please edit your question to show what exactly happens ("doesn't work"). THX :-)
– R Yoda
Nov 16 '18 at 23:42
Check the documentation forread_csv
; you needcol_types
argument to be "One of NULL, a cols() specification, or a string." Currently you have none of these options.
– Calum You
Nov 17 '18 at 0:18
I added more details to the question. Basically, if I have saved a string with columns specifications, how do I turn it into a cols() object?
– Leo Barlach
Nov 19 '18 at 18:13
add a comment |
I want to use readr::read_csv() to read several data sets.
I have a CSV with the description of each file that looks like this:
# A tibble: 5 x 8
Object_Name File_Path Column1 Column2 Column3 Column4 Column5 Column6
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 Object1 data/file1.csv character character character numeric numeric numeric
2 Object2 data/file2.csv character character character numeric NA NA
3 Object3 data/file3.csv character character numeric NA NA NA
4 Object4 data/file4.csv character numeric numeric numeric NA NA
5 Object5 data/file5.csv character numeric numeric NA NA NA
What I wish if for something like this to work:
for (i in 1:nrow(list_of_files)) {
obj_name <- list_of_files[i,]$Object_Name
pth <- list_of_files[i,]$File_Path
col_types <- as.character(list_of_files[i,3:8])
col_types <- col_types[!is.na(col_types)]
# This part doesnt work
tab <- readr::read_csv(file = pth,
col_types = col_types)
assign(obj_name,tab)
}
Usually the guessing works, but I need to make sure.
EDIT
Simplifying the questions, so maybe it's clearer.
To specify the columns types on read_csv, you need a cols() call.
This works:
> cols('c', 'n')
cols(
= col_character(),
= col_number()
)
This doesn't work:
> aa <- c('c','n')
> cols(aa)
Error in switch(x, `_` = , `-` = col_skip(), `?` = col_guess(), c = col_character(), :
EXPR must be a length 1 vector
Because I'm iterating through a bunch of files, I need something that looks like the second example. I can save somewhere a string with the columns specifications, but how do I turn a string into a cols() object?
r readr
I want to use readr::read_csv() to read several data sets.
I have a CSV with the description of each file that looks like this:
# A tibble: 5 x 8
Object_Name File_Path Column1 Column2 Column3 Column4 Column5 Column6
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 Object1 data/file1.csv character character character numeric numeric numeric
2 Object2 data/file2.csv character character character numeric NA NA
3 Object3 data/file3.csv character character numeric NA NA NA
4 Object4 data/file4.csv character numeric numeric numeric NA NA
5 Object5 data/file5.csv character numeric numeric NA NA NA
What I wish if for something like this to work:
for (i in 1:nrow(list_of_files)) {
obj_name <- list_of_files[i,]$Object_Name
pth <- list_of_files[i,]$File_Path
col_types <- as.character(list_of_files[i,3:8])
col_types <- col_types[!is.na(col_types)]
# This part doesnt work
tab <- readr::read_csv(file = pth,
col_types = col_types)
assign(obj_name,tab)
}
Usually the guessing works, but I need to make sure.
EDIT
Simplifying the questions, so maybe it's clearer.
To specify the columns types on read_csv, you need a cols() call.
This works:
> cols('c', 'n')
cols(
= col_character(),
= col_number()
)
This doesn't work:
> aa <- c('c','n')
> cols(aa)
Error in switch(x, `_` = , `-` = col_skip(), `?` = col_guess(), c = col_character(), :
EXPR must be a length 1 vector
Because I'm iterating through a bunch of files, I need something that looks like the second example. I can save somewhere a string with the columns specifications, but how do I turn a string into a cols() object?
r readr
r readr
edited Nov 19 '18 at 18:12
Leo Barlach
asked Nov 16 '18 at 23:10


Leo BarlachLeo Barlach
384
384
Difficult to find an answer without seeing the exact error message, please edit your question to show what exactly happens ("doesn't work"). THX :-)
– R Yoda
Nov 16 '18 at 23:42
Check the documentation forread_csv
; you needcol_types
argument to be "One of NULL, a cols() specification, or a string." Currently you have none of these options.
– Calum You
Nov 17 '18 at 0:18
I added more details to the question. Basically, if I have saved a string with columns specifications, how do I turn it into a cols() object?
– Leo Barlach
Nov 19 '18 at 18:13
add a comment |
Difficult to find an answer without seeing the exact error message, please edit your question to show what exactly happens ("doesn't work"). THX :-)
– R Yoda
Nov 16 '18 at 23:42
Check the documentation forread_csv
; you needcol_types
argument to be "One of NULL, a cols() specification, or a string." Currently you have none of these options.
– Calum You
Nov 17 '18 at 0:18
I added more details to the question. Basically, if I have saved a string with columns specifications, how do I turn it into a cols() object?
– Leo Barlach
Nov 19 '18 at 18:13
Difficult to find an answer without seeing the exact error message, please edit your question to show what exactly happens ("doesn't work"). THX :-)
– R Yoda
Nov 16 '18 at 23:42
Difficult to find an answer without seeing the exact error message, please edit your question to show what exactly happens ("doesn't work"). THX :-)
– R Yoda
Nov 16 '18 at 23:42
Check the documentation for
read_csv
; you need col_types
argument to be "One of NULL, a cols() specification, or a string." Currently you have none of these options.– Calum You
Nov 17 '18 at 0:18
Check the documentation for
read_csv
; you need col_types
argument to be "One of NULL, a cols() specification, or a string." Currently you have none of these options.– Calum You
Nov 17 '18 at 0:18
I added more details to the question. Basically, if I have saved a string with columns specifications, how do I turn it into a cols() object?
– Leo Barlach
Nov 19 '18 at 18:13
I added more details to the question. Basically, if I have saved a string with columns specifications, how do I turn it into a cols() object?
– Leo Barlach
Nov 19 '18 at 18:13
add a comment |
1 Answer
1
active
oldest
votes
For you to obtain the second example, you can just use do.call
:
aa <- c('c','n')
do.call(cols,as.list(aa))
cols(
= col_character(),
= col_number()
)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53346557%2freadr-passing-a-string-of-column-classes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
For you to obtain the second example, you can just use do.call
:
aa <- c('c','n')
do.call(cols,as.list(aa))
cols(
= col_character(),
= col_number()
)
add a comment |
For you to obtain the second example, you can just use do.call
:
aa <- c('c','n')
do.call(cols,as.list(aa))
cols(
= col_character(),
= col_number()
)
add a comment |
For you to obtain the second example, you can just use do.call
:
aa <- c('c','n')
do.call(cols,as.list(aa))
cols(
= col_character(),
= col_number()
)
For you to obtain the second example, you can just use do.call
:
aa <- c('c','n')
do.call(cols,as.list(aa))
cols(
= col_character(),
= col_number()
)
answered Nov 19 '18 at 18:43


OnyambuOnyambu
15.4k1519
15.4k1519
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53346557%2freadr-passing-a-string-of-column-classes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Difficult to find an answer without seeing the exact error message, please edit your question to show what exactly happens ("doesn't work"). THX :-)
– R Yoda
Nov 16 '18 at 23:42
Check the documentation for
read_csv
; you needcol_types
argument to be "One of NULL, a cols() specification, or a string." Currently you have none of these options.– Calum You
Nov 17 '18 at 0:18
I added more details to the question. Basically, if I have saved a string with columns specifications, how do I turn it into a cols() object?
– Leo Barlach
Nov 19 '18 at 18:13