Remove attributes from data read in readr::read_csv
readr::read_csv
adds attributes that don't get updated when the data is edited. For example,
library('tidyverse')
df <- read_csv("A,B,Cna,1,xnb,1,ync,1,z")
# Remove columns with only one distinct entry
no_info <- df %>% sapply(n_distinct)
no_info <- names(no_info[no_info==1])
df2 <- df %>%
select(-no_info)
Inspecting the structure, we see that column B is still present in the attributes of df2
:
> str(df)
Classes ‘spec_tbl_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of 3 variables:
$ A: chr "a" "b" "c"
$ B: num 1 1 1
$ C: chr "x" "y" "z"
- attr(*, "spec")=
.. cols(
.. A = col_character(),
.. B = col_double(),
.. C = col_character()
.. )
> str(df2)
Classes ‘spec_tbl_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of 2 variables:
$ A: chr "a" "b" "c"
$ C: chr "x" "y" "z"
- attr(*, "spec")=
.. cols(
.. A = col_character(),
.. B = col_double(),
.. C = col_character()
.. )
> attributes(df2)
$class
[1] "spec_tbl_df" "tbl_df" "tbl" "data.frame"
$row.names
[1] 1 2 3
$spec
cols(
A = col_character(),
B = col_double(),
C = col_character()
)
$names
[1] "A" "C"
>
How can I remove columns (or any other updates to the data) and have the changes accurately reflected in the new data structure and attributes?
r readr
add a comment |
readr::read_csv
adds attributes that don't get updated when the data is edited. For example,
library('tidyverse')
df <- read_csv("A,B,Cna,1,xnb,1,ync,1,z")
# Remove columns with only one distinct entry
no_info <- df %>% sapply(n_distinct)
no_info <- names(no_info[no_info==1])
df2 <- df %>%
select(-no_info)
Inspecting the structure, we see that column B is still present in the attributes of df2
:
> str(df)
Classes ‘spec_tbl_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of 3 variables:
$ A: chr "a" "b" "c"
$ B: num 1 1 1
$ C: chr "x" "y" "z"
- attr(*, "spec")=
.. cols(
.. A = col_character(),
.. B = col_double(),
.. C = col_character()
.. )
> str(df2)
Classes ‘spec_tbl_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of 2 variables:
$ A: chr "a" "b" "c"
$ C: chr "x" "y" "z"
- attr(*, "spec")=
.. cols(
.. A = col_character(),
.. B = col_double(),
.. C = col_character()
.. )
> attributes(df2)
$class
[1] "spec_tbl_df" "tbl_df" "tbl" "data.frame"
$row.names
[1] 1 2 3
$spec
cols(
A = col_character(),
B = col_double(),
C = col_character()
)
$names
[1] "A" "C"
>
How can I remove columns (or any other updates to the data) and have the changes accurately reflected in the new data structure and attributes?
r readr
1
out of curiosity, why do you want to do this? I understand you want attributes to be indicative of the actual tibble, but why do you care? Cheers
– Khaynes
Jan 2 at 3:16
One is that it's annoying to scroll through information on non-existing columns, particularly when there's a large number of columns and you've removed them programmatically. Another is a concern of unintended consequences, such as when you don't drop factors after removing some of them, and future calculations, plots, etc, will behave differently, as if they were still there. I'm not sure of the consequences of having information included on features that no longer exist.
– conor
Jan 2 at 3:29
I mean, you could just dodata.frame(df2)
.
– joran
Jan 2 at 3:37
add a comment |
readr::read_csv
adds attributes that don't get updated when the data is edited. For example,
library('tidyverse')
df <- read_csv("A,B,Cna,1,xnb,1,ync,1,z")
# Remove columns with only one distinct entry
no_info <- df %>% sapply(n_distinct)
no_info <- names(no_info[no_info==1])
df2 <- df %>%
select(-no_info)
Inspecting the structure, we see that column B is still present in the attributes of df2
:
> str(df)
Classes ‘spec_tbl_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of 3 variables:
$ A: chr "a" "b" "c"
$ B: num 1 1 1
$ C: chr "x" "y" "z"
- attr(*, "spec")=
.. cols(
.. A = col_character(),
.. B = col_double(),
.. C = col_character()
.. )
> str(df2)
Classes ‘spec_tbl_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of 2 variables:
$ A: chr "a" "b" "c"
$ C: chr "x" "y" "z"
- attr(*, "spec")=
.. cols(
.. A = col_character(),
.. B = col_double(),
.. C = col_character()
.. )
> attributes(df2)
$class
[1] "spec_tbl_df" "tbl_df" "tbl" "data.frame"
$row.names
[1] 1 2 3
$spec
cols(
A = col_character(),
B = col_double(),
C = col_character()
)
$names
[1] "A" "C"
>
How can I remove columns (or any other updates to the data) and have the changes accurately reflected in the new data structure and attributes?
r readr
readr::read_csv
adds attributes that don't get updated when the data is edited. For example,
library('tidyverse')
df <- read_csv("A,B,Cna,1,xnb,1,ync,1,z")
# Remove columns with only one distinct entry
no_info <- df %>% sapply(n_distinct)
no_info <- names(no_info[no_info==1])
df2 <- df %>%
select(-no_info)
Inspecting the structure, we see that column B is still present in the attributes of df2
:
> str(df)
Classes ‘spec_tbl_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of 3 variables:
$ A: chr "a" "b" "c"
$ B: num 1 1 1
$ C: chr "x" "y" "z"
- attr(*, "spec")=
.. cols(
.. A = col_character(),
.. B = col_double(),
.. C = col_character()
.. )
> str(df2)
Classes ‘spec_tbl_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of 2 variables:
$ A: chr "a" "b" "c"
$ C: chr "x" "y" "z"
- attr(*, "spec")=
.. cols(
.. A = col_character(),
.. B = col_double(),
.. C = col_character()
.. )
> attributes(df2)
$class
[1] "spec_tbl_df" "tbl_df" "tbl" "data.frame"
$row.names
[1] 1 2 3
$spec
cols(
A = col_character(),
B = col_double(),
C = col_character()
)
$names
[1] "A" "C"
>
How can I remove columns (or any other updates to the data) and have the changes accurately reflected in the new data structure and attributes?
r readr
r readr
asked Jan 2 at 2:59
conorconor
320414
320414
1
out of curiosity, why do you want to do this? I understand you want attributes to be indicative of the actual tibble, but why do you care? Cheers
– Khaynes
Jan 2 at 3:16
One is that it's annoying to scroll through information on non-existing columns, particularly when there's a large number of columns and you've removed them programmatically. Another is a concern of unintended consequences, such as when you don't drop factors after removing some of them, and future calculations, plots, etc, will behave differently, as if they were still there. I'm not sure of the consequences of having information included on features that no longer exist.
– conor
Jan 2 at 3:29
I mean, you could just dodata.frame(df2)
.
– joran
Jan 2 at 3:37
add a comment |
1
out of curiosity, why do you want to do this? I understand you want attributes to be indicative of the actual tibble, but why do you care? Cheers
– Khaynes
Jan 2 at 3:16
One is that it's annoying to scroll through information on non-existing columns, particularly when there's a large number of columns and you've removed them programmatically. Another is a concern of unintended consequences, such as when you don't drop factors after removing some of them, and future calculations, plots, etc, will behave differently, as if they were still there. I'm not sure of the consequences of having information included on features that no longer exist.
– conor
Jan 2 at 3:29
I mean, you could just dodata.frame(df2)
.
– joran
Jan 2 at 3:37
1
1
out of curiosity, why do you want to do this? I understand you want attributes to be indicative of the actual tibble, but why do you care? Cheers
– Khaynes
Jan 2 at 3:16
out of curiosity, why do you want to do this? I understand you want attributes to be indicative of the actual tibble, but why do you care? Cheers
– Khaynes
Jan 2 at 3:16
One is that it's annoying to scroll through information on non-existing columns, particularly when there's a large number of columns and you've removed them programmatically. Another is a concern of unintended consequences, such as when you don't drop factors after removing some of them, and future calculations, plots, etc, will behave differently, as if they were still there. I'm not sure of the consequences of having information included on features that no longer exist.
– conor
Jan 2 at 3:29
One is that it's annoying to scroll through information on non-existing columns, particularly when there's a large number of columns and you've removed them programmatically. Another is a concern of unintended consequences, such as when you don't drop factors after removing some of them, and future calculations, plots, etc, will behave differently, as if they were still there. I'm not sure of the consequences of having information included on features that no longer exist.
– conor
Jan 2 at 3:29
I mean, you could just do
data.frame(df2)
.– joran
Jan 2 at 3:37
I mean, you could just do
data.frame(df2)
.– joran
Jan 2 at 3:37
add a comment |
1 Answer
1
active
oldest
votes
You can remove column specifiction by setting it to NULL
:
> attr(df, 'spec') <- NULL
> str(df)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of 3 variables:
$ A: chr "a" "b" "c"
$ B: int 1 1 1
$ C: chr "x" "y" "z"
> df
# A tibble: 3 x 3
A B C
<chr> <int> <chr>
1 a 1 x
2 b 1 y
3 c 1 z
This removes the attribute completely, rather than just adjusting it to reflect the data after a manipulation.
– conor
Jan 2 at 23:57
1
@conor Yeah. I did some search and didn't found any function that update it. However, the column specifications are not used elsewhere. It tells you howread_csv
parsed each column during reading. AFAIK, it is safe to drop them and is unlikely to have any undesired consequences.
– mt1022
Jan 3 at 1:25
Ah great, that's good to know. Thanks @mt1022
– conor
Jan 3 at 1:37
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%2f54000753%2fremove-attributes-from-data-read-in-readrread-csv%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
You can remove column specifiction by setting it to NULL
:
> attr(df, 'spec') <- NULL
> str(df)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of 3 variables:
$ A: chr "a" "b" "c"
$ B: int 1 1 1
$ C: chr "x" "y" "z"
> df
# A tibble: 3 x 3
A B C
<chr> <int> <chr>
1 a 1 x
2 b 1 y
3 c 1 z
This removes the attribute completely, rather than just adjusting it to reflect the data after a manipulation.
– conor
Jan 2 at 23:57
1
@conor Yeah. I did some search and didn't found any function that update it. However, the column specifications are not used elsewhere. It tells you howread_csv
parsed each column during reading. AFAIK, it is safe to drop them and is unlikely to have any undesired consequences.
– mt1022
Jan 3 at 1:25
Ah great, that's good to know. Thanks @mt1022
– conor
Jan 3 at 1:37
add a comment |
You can remove column specifiction by setting it to NULL
:
> attr(df, 'spec') <- NULL
> str(df)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of 3 variables:
$ A: chr "a" "b" "c"
$ B: int 1 1 1
$ C: chr "x" "y" "z"
> df
# A tibble: 3 x 3
A B C
<chr> <int> <chr>
1 a 1 x
2 b 1 y
3 c 1 z
This removes the attribute completely, rather than just adjusting it to reflect the data after a manipulation.
– conor
Jan 2 at 23:57
1
@conor Yeah. I did some search and didn't found any function that update it. However, the column specifications are not used elsewhere. It tells you howread_csv
parsed each column during reading. AFAIK, it is safe to drop them and is unlikely to have any undesired consequences.
– mt1022
Jan 3 at 1:25
Ah great, that's good to know. Thanks @mt1022
– conor
Jan 3 at 1:37
add a comment |
You can remove column specifiction by setting it to NULL
:
> attr(df, 'spec') <- NULL
> str(df)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of 3 variables:
$ A: chr "a" "b" "c"
$ B: int 1 1 1
$ C: chr "x" "y" "z"
> df
# A tibble: 3 x 3
A B C
<chr> <int> <chr>
1 a 1 x
2 b 1 y
3 c 1 z
You can remove column specifiction by setting it to NULL
:
> attr(df, 'spec') <- NULL
> str(df)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of 3 variables:
$ A: chr "a" "b" "c"
$ B: int 1 1 1
$ C: chr "x" "y" "z"
> df
# A tibble: 3 x 3
A B C
<chr> <int> <chr>
1 a 1 x
2 b 1 y
3 c 1 z
answered Jan 2 at 4:59
mt1022mt1022
10.2k22147
10.2k22147
This removes the attribute completely, rather than just adjusting it to reflect the data after a manipulation.
– conor
Jan 2 at 23:57
1
@conor Yeah. I did some search and didn't found any function that update it. However, the column specifications are not used elsewhere. It tells you howread_csv
parsed each column during reading. AFAIK, it is safe to drop them and is unlikely to have any undesired consequences.
– mt1022
Jan 3 at 1:25
Ah great, that's good to know. Thanks @mt1022
– conor
Jan 3 at 1:37
add a comment |
This removes the attribute completely, rather than just adjusting it to reflect the data after a manipulation.
– conor
Jan 2 at 23:57
1
@conor Yeah. I did some search and didn't found any function that update it. However, the column specifications are not used elsewhere. It tells you howread_csv
parsed each column during reading. AFAIK, it is safe to drop them and is unlikely to have any undesired consequences.
– mt1022
Jan 3 at 1:25
Ah great, that's good to know. Thanks @mt1022
– conor
Jan 3 at 1:37
This removes the attribute completely, rather than just adjusting it to reflect the data after a manipulation.
– conor
Jan 2 at 23:57
This removes the attribute completely, rather than just adjusting it to reflect the data after a manipulation.
– conor
Jan 2 at 23:57
1
1
@conor Yeah. I did some search and didn't found any function that update it. However, the column specifications are not used elsewhere. It tells you how
read_csv
parsed each column during reading. AFAIK, it is safe to drop them and is unlikely to have any undesired consequences.– mt1022
Jan 3 at 1:25
@conor Yeah. I did some search and didn't found any function that update it. However, the column specifications are not used elsewhere. It tells you how
read_csv
parsed each column during reading. AFAIK, it is safe to drop them and is unlikely to have any undesired consequences.– mt1022
Jan 3 at 1:25
Ah great, that's good to know. Thanks @mt1022
– conor
Jan 3 at 1:37
Ah great, that's good to know. Thanks @mt1022
– conor
Jan 3 at 1:37
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.
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%2f54000753%2fremove-attributes-from-data-read-in-readrread-csv%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
1
out of curiosity, why do you want to do this? I understand you want attributes to be indicative of the actual tibble, but why do you care? Cheers
– Khaynes
Jan 2 at 3:16
One is that it's annoying to scroll through information on non-existing columns, particularly when there's a large number of columns and you've removed them programmatically. Another is a concern of unintended consequences, such as when you don't drop factors after removing some of them, and future calculations, plots, etc, will behave differently, as if they were still there. I'm not sure of the consequences of having information included on features that no longer exist.
– conor
Jan 2 at 3:29
I mean, you could just do
data.frame(df2)
.– joran
Jan 2 at 3:37