Using dataframe's first data column for row names
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm learning R and currently looking at ISLR's College.csv dataset (found here). I'm trying to set the first data column as the row names but none of the solutions I've found have worked:
college <- fread("College.csv")
rownames(college) <- college$V1
college <- college[, -1]
college
college <- fread("College.csv")
rownames(college) <- college[[1]]
college <- college[, -1]
college
college <- fread("College.csv")
rownames(college) <- college[,1]
college[,1] <- NULL
college
college <- fread("College.csv")
rownames(college) <- college[,1]
college <- college(, -1)
college
I've found a ton of advice on this issue on StackExchange, on other sites, and in the book I'm using and am confused about why none of it is working for me. I'd welcome any advice.
edit for more detail: I'd like to do this using fread or at least read_csv, and I'd like to do it without reassigning. If that can't be done without reassigning, I'd like to be explicitly told so because I don't trust myself in this matter.
r
add a comment |
I'm learning R and currently looking at ISLR's College.csv dataset (found here). I'm trying to set the first data column as the row names but none of the solutions I've found have worked:
college <- fread("College.csv")
rownames(college) <- college$V1
college <- college[, -1]
college
college <- fread("College.csv")
rownames(college) <- college[[1]]
college <- college[, -1]
college
college <- fread("College.csv")
rownames(college) <- college[,1]
college[,1] <- NULL
college
college <- fread("College.csv")
rownames(college) <- college[,1]
college <- college(, -1)
college
I've found a ton of advice on this issue on StackExchange, on other sites, and in the book I'm using and am confused about why none of it is working for me. I'd welcome any advice.
edit for more detail: I'd like to do this using fread or at least read_csv, and I'd like to do it without reassigning. If that can't be done without reassigning, I'd like to be explicitly told so because I don't trust myself in this matter.
r
you can see duplicate: stackoverflow.com/questions/5555408/…
– YOLO
Jan 3 at 22:34
I saw that before posting. I'd like to do it without reassigning and his non-reassigning method is the third one of the four attempts I listed.
– JohnDoeVsJoeSchmoe
Jan 3 at 22:37
I just went and tried his reassigning method for good measure, and got ainvalid row.names lengtherror.
– JohnDoeVsJoeSchmoe
Jan 3 at 22:42
add a comment |
I'm learning R and currently looking at ISLR's College.csv dataset (found here). I'm trying to set the first data column as the row names but none of the solutions I've found have worked:
college <- fread("College.csv")
rownames(college) <- college$V1
college <- college[, -1]
college
college <- fread("College.csv")
rownames(college) <- college[[1]]
college <- college[, -1]
college
college <- fread("College.csv")
rownames(college) <- college[,1]
college[,1] <- NULL
college
college <- fread("College.csv")
rownames(college) <- college[,1]
college <- college(, -1)
college
I've found a ton of advice on this issue on StackExchange, on other sites, and in the book I'm using and am confused about why none of it is working for me. I'd welcome any advice.
edit for more detail: I'd like to do this using fread or at least read_csv, and I'd like to do it without reassigning. If that can't be done without reassigning, I'd like to be explicitly told so because I don't trust myself in this matter.
r
I'm learning R and currently looking at ISLR's College.csv dataset (found here). I'm trying to set the first data column as the row names but none of the solutions I've found have worked:
college <- fread("College.csv")
rownames(college) <- college$V1
college <- college[, -1]
college
college <- fread("College.csv")
rownames(college) <- college[[1]]
college <- college[, -1]
college
college <- fread("College.csv")
rownames(college) <- college[,1]
college[,1] <- NULL
college
college <- fread("College.csv")
rownames(college) <- college[,1]
college <- college(, -1)
college
I've found a ton of advice on this issue on StackExchange, on other sites, and in the book I'm using and am confused about why none of it is working for me. I'd welcome any advice.
edit for more detail: I'd like to do this using fread or at least read_csv, and I'd like to do it without reassigning. If that can't be done without reassigning, I'd like to be explicitly told so because I don't trust myself in this matter.
r
r
edited Jan 3 at 22:38
JohnDoeVsJoeSchmoe
asked Jan 3 at 21:35
JohnDoeVsJoeSchmoeJohnDoeVsJoeSchmoe
36211
36211
you can see duplicate: stackoverflow.com/questions/5555408/…
– YOLO
Jan 3 at 22:34
I saw that before posting. I'd like to do it without reassigning and his non-reassigning method is the third one of the four attempts I listed.
– JohnDoeVsJoeSchmoe
Jan 3 at 22:37
I just went and tried his reassigning method for good measure, and got ainvalid row.names lengtherror.
– JohnDoeVsJoeSchmoe
Jan 3 at 22:42
add a comment |
you can see duplicate: stackoverflow.com/questions/5555408/…
– YOLO
Jan 3 at 22:34
I saw that before posting. I'd like to do it without reassigning and his non-reassigning method is the third one of the four attempts I listed.
– JohnDoeVsJoeSchmoe
Jan 3 at 22:37
I just went and tried his reassigning method for good measure, and got ainvalid row.names lengtherror.
– JohnDoeVsJoeSchmoe
Jan 3 at 22:42
you can see duplicate: stackoverflow.com/questions/5555408/…
– YOLO
Jan 3 at 22:34
you can see duplicate: stackoverflow.com/questions/5555408/…
– YOLO
Jan 3 at 22:34
I saw that before posting. I'd like to do it without reassigning and his non-reassigning method is the third one of the four attempts I listed.
– JohnDoeVsJoeSchmoe
Jan 3 at 22:37
I saw that before posting. I'd like to do it without reassigning and his non-reassigning method is the third one of the four attempts I listed.
– JohnDoeVsJoeSchmoe
Jan 3 at 22:37
I just went and tried his reassigning method for good measure, and got a
invalid row.names length error.– JohnDoeVsJoeSchmoe
Jan 3 at 22:42
I just went and tried his reassigning method for good measure, and got a
invalid row.names length error.– JohnDoeVsJoeSchmoe
Jan 3 at 22:42
add a comment |
3 Answers
3
active
oldest
votes
fread is part of the data.table package. When you import, it does so as a data.table. The reason you can't assign row names is that data.tables cannot have row names. It's an attribute of the package. See https://cran.r-project.org/web/packages/data.table/data.table.pdf.
Try using base or dplyr and you shouldn't have any trouble.
Also, see Display row names in a data.table object.
Oh this is very interesting, thank you. So aredata.frames in general a bad design choice? Should I be usingdata.tables for most use cases?
– JohnDoeVsJoeSchmoe
Jan 3 at 22:58
1
@JohnDoeVsJoeSchmoe I prefer tidyverse but data.table is not at all a bad choice. In fact, it is the fastest implementation of R, beating both base and tidyverse under most conditions. It's just a matter of preference.
– hmhensen
Jan 3 at 23:07
add a comment |
Import the csv file using:
college <- read.csv("path/to/file/College.csv", header = TRUE, row.names = 1)
Thanks. Can you do it using fread or at least read_csv? I've heard that read.csv is much slower than the other two.
– JohnDoeVsJoeSchmoe
Jan 3 at 22:31
1
Apparently not:readr(the parent ofread_csv) produces atibble, which doesn't support row names, but are faster and more memory efficient than data frames.freadalso does not support row names as it produces adata.table. You could perhaps read in usingdata.frame(fread("college.csv), row.names=1), but I have no idea if that would be efficient or not.
– bob1
Jan 3 at 22:53
Thank you so much for the detailed follow-up! So should I in general be usingtibbles ordata.tables instead ofdata.frames? Are they best practice?
– JohnDoeVsJoeSchmoe
Jan 3 at 22:59
add a comment |
You can use this
college<- read.csv("C:/Users/USER/Downloads/College.csv")
rownames(college) <- college[,1]
Or this upon import
college<- read.csv("C:/Users/USER/Downloads/College.csv", header = TRUE, row.names = 1)
college<- read.csv("C:/Users/USER/Downloads/College.csv")
– mokdur
Jan 3 at 22:50
rownames(college) <- college[,1]
– mokdur
Jan 3 at 22:51
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%2f54030122%2fusing-dataframes-first-data-column-for-row-names%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
fread is part of the data.table package. When you import, it does so as a data.table. The reason you can't assign row names is that data.tables cannot have row names. It's an attribute of the package. See https://cran.r-project.org/web/packages/data.table/data.table.pdf.
Try using base or dplyr and you shouldn't have any trouble.
Also, see Display row names in a data.table object.
Oh this is very interesting, thank you. So aredata.frames in general a bad design choice? Should I be usingdata.tables for most use cases?
– JohnDoeVsJoeSchmoe
Jan 3 at 22:58
1
@JohnDoeVsJoeSchmoe I prefer tidyverse but data.table is not at all a bad choice. In fact, it is the fastest implementation of R, beating both base and tidyverse under most conditions. It's just a matter of preference.
– hmhensen
Jan 3 at 23:07
add a comment |
fread is part of the data.table package. When you import, it does so as a data.table. The reason you can't assign row names is that data.tables cannot have row names. It's an attribute of the package. See https://cran.r-project.org/web/packages/data.table/data.table.pdf.
Try using base or dplyr and you shouldn't have any trouble.
Also, see Display row names in a data.table object.
Oh this is very interesting, thank you. So aredata.frames in general a bad design choice? Should I be usingdata.tables for most use cases?
– JohnDoeVsJoeSchmoe
Jan 3 at 22:58
1
@JohnDoeVsJoeSchmoe I prefer tidyverse but data.table is not at all a bad choice. In fact, it is the fastest implementation of R, beating both base and tidyverse under most conditions. It's just a matter of preference.
– hmhensen
Jan 3 at 23:07
add a comment |
fread is part of the data.table package. When you import, it does so as a data.table. The reason you can't assign row names is that data.tables cannot have row names. It's an attribute of the package. See https://cran.r-project.org/web/packages/data.table/data.table.pdf.
Try using base or dplyr and you shouldn't have any trouble.
Also, see Display row names in a data.table object.
fread is part of the data.table package. When you import, it does so as a data.table. The reason you can't assign row names is that data.tables cannot have row names. It's an attribute of the package. See https://cran.r-project.org/web/packages/data.table/data.table.pdf.
Try using base or dplyr and you shouldn't have any trouble.
Also, see Display row names in a data.table object.
answered Jan 3 at 22:47
hmhensenhmhensen
9021718
9021718
Oh this is very interesting, thank you. So aredata.frames in general a bad design choice? Should I be usingdata.tables for most use cases?
– JohnDoeVsJoeSchmoe
Jan 3 at 22:58
1
@JohnDoeVsJoeSchmoe I prefer tidyverse but data.table is not at all a bad choice. In fact, it is the fastest implementation of R, beating both base and tidyverse under most conditions. It's just a matter of preference.
– hmhensen
Jan 3 at 23:07
add a comment |
Oh this is very interesting, thank you. So aredata.frames in general a bad design choice? Should I be usingdata.tables for most use cases?
– JohnDoeVsJoeSchmoe
Jan 3 at 22:58
1
@JohnDoeVsJoeSchmoe I prefer tidyverse but data.table is not at all a bad choice. In fact, it is the fastest implementation of R, beating both base and tidyverse under most conditions. It's just a matter of preference.
– hmhensen
Jan 3 at 23:07
Oh this is very interesting, thank you. So are
data.frames in general a bad design choice? Should I be using data.tables for most use cases?– JohnDoeVsJoeSchmoe
Jan 3 at 22:58
Oh this is very interesting, thank you. So are
data.frames in general a bad design choice? Should I be using data.tables for most use cases?– JohnDoeVsJoeSchmoe
Jan 3 at 22:58
1
1
@JohnDoeVsJoeSchmoe I prefer tidyverse but data.table is not at all a bad choice. In fact, it is the fastest implementation of R, beating both base and tidyverse under most conditions. It's just a matter of preference.
– hmhensen
Jan 3 at 23:07
@JohnDoeVsJoeSchmoe I prefer tidyverse but data.table is not at all a bad choice. In fact, it is the fastest implementation of R, beating both base and tidyverse under most conditions. It's just a matter of preference.
– hmhensen
Jan 3 at 23:07
add a comment |
Import the csv file using:
college <- read.csv("path/to/file/College.csv", header = TRUE, row.names = 1)
Thanks. Can you do it using fread or at least read_csv? I've heard that read.csv is much slower than the other two.
– JohnDoeVsJoeSchmoe
Jan 3 at 22:31
1
Apparently not:readr(the parent ofread_csv) produces atibble, which doesn't support row names, but are faster and more memory efficient than data frames.freadalso does not support row names as it produces adata.table. You could perhaps read in usingdata.frame(fread("college.csv), row.names=1), but I have no idea if that would be efficient or not.
– bob1
Jan 3 at 22:53
Thank you so much for the detailed follow-up! So should I in general be usingtibbles ordata.tables instead ofdata.frames? Are they best practice?
– JohnDoeVsJoeSchmoe
Jan 3 at 22:59
add a comment |
Import the csv file using:
college <- read.csv("path/to/file/College.csv", header = TRUE, row.names = 1)
Thanks. Can you do it using fread or at least read_csv? I've heard that read.csv is much slower than the other two.
– JohnDoeVsJoeSchmoe
Jan 3 at 22:31
1
Apparently not:readr(the parent ofread_csv) produces atibble, which doesn't support row names, but are faster and more memory efficient than data frames.freadalso does not support row names as it produces adata.table. You could perhaps read in usingdata.frame(fread("college.csv), row.names=1), but I have no idea if that would be efficient or not.
– bob1
Jan 3 at 22:53
Thank you so much for the detailed follow-up! So should I in general be usingtibbles ordata.tables instead ofdata.frames? Are they best practice?
– JohnDoeVsJoeSchmoe
Jan 3 at 22:59
add a comment |
Import the csv file using:
college <- read.csv("path/to/file/College.csv", header = TRUE, row.names = 1)
Import the csv file using:
college <- read.csv("path/to/file/College.csv", header = TRUE, row.names = 1)
edited Jan 4 at 1:23
hmhensen
9021718
9021718
answered Jan 3 at 21:40
bob1bob1
264110
264110
Thanks. Can you do it using fread or at least read_csv? I've heard that read.csv is much slower than the other two.
– JohnDoeVsJoeSchmoe
Jan 3 at 22:31
1
Apparently not:readr(the parent ofread_csv) produces atibble, which doesn't support row names, but are faster and more memory efficient than data frames.freadalso does not support row names as it produces adata.table. You could perhaps read in usingdata.frame(fread("college.csv), row.names=1), but I have no idea if that would be efficient or not.
– bob1
Jan 3 at 22:53
Thank you so much for the detailed follow-up! So should I in general be usingtibbles ordata.tables instead ofdata.frames? Are they best practice?
– JohnDoeVsJoeSchmoe
Jan 3 at 22:59
add a comment |
Thanks. Can you do it using fread or at least read_csv? I've heard that read.csv is much slower than the other two.
– JohnDoeVsJoeSchmoe
Jan 3 at 22:31
1
Apparently not:readr(the parent ofread_csv) produces atibble, which doesn't support row names, but are faster and more memory efficient than data frames.freadalso does not support row names as it produces adata.table. You could perhaps read in usingdata.frame(fread("college.csv), row.names=1), but I have no idea if that would be efficient or not.
– bob1
Jan 3 at 22:53
Thank you so much for the detailed follow-up! So should I in general be usingtibbles ordata.tables instead ofdata.frames? Are they best practice?
– JohnDoeVsJoeSchmoe
Jan 3 at 22:59
Thanks. Can you do it using fread or at least read_csv? I've heard that read.csv is much slower than the other two.
– JohnDoeVsJoeSchmoe
Jan 3 at 22:31
Thanks. Can you do it using fread or at least read_csv? I've heard that read.csv is much slower than the other two.
– JohnDoeVsJoeSchmoe
Jan 3 at 22:31
1
1
Apparently not:
readr (the parent of read_csv) produces a tibble, which doesn't support row names, but are faster and more memory efficient than data frames. fread also does not support row names as it produces a data.table. You could perhaps read in using data.frame(fread("college.csv), row.names=1), but I have no idea if that would be efficient or not.– bob1
Jan 3 at 22:53
Apparently not:
readr (the parent of read_csv) produces a tibble, which doesn't support row names, but are faster and more memory efficient than data frames. fread also does not support row names as it produces a data.table. You could perhaps read in using data.frame(fread("college.csv), row.names=1), but I have no idea if that would be efficient or not.– bob1
Jan 3 at 22:53
Thank you so much for the detailed follow-up! So should I in general be using
tibbles or data.tables instead of data.frames? Are they best practice?– JohnDoeVsJoeSchmoe
Jan 3 at 22:59
Thank you so much for the detailed follow-up! So should I in general be using
tibbles or data.tables instead of data.frames? Are they best practice?– JohnDoeVsJoeSchmoe
Jan 3 at 22:59
add a comment |
You can use this
college<- read.csv("C:/Users/USER/Downloads/College.csv")
rownames(college) <- college[,1]
Or this upon import
college<- read.csv("C:/Users/USER/Downloads/College.csv", header = TRUE, row.names = 1)
college<- read.csv("C:/Users/USER/Downloads/College.csv")
– mokdur
Jan 3 at 22:50
rownames(college) <- college[,1]
– mokdur
Jan 3 at 22:51
add a comment |
You can use this
college<- read.csv("C:/Users/USER/Downloads/College.csv")
rownames(college) <- college[,1]
Or this upon import
college<- read.csv("C:/Users/USER/Downloads/College.csv", header = TRUE, row.names = 1)
college<- read.csv("C:/Users/USER/Downloads/College.csv")
– mokdur
Jan 3 at 22:50
rownames(college) <- college[,1]
– mokdur
Jan 3 at 22:51
add a comment |
You can use this
college<- read.csv("C:/Users/USER/Downloads/College.csv")
rownames(college) <- college[,1]
Or this upon import
college<- read.csv("C:/Users/USER/Downloads/College.csv", header = TRUE, row.names = 1)
You can use this
college<- read.csv("C:/Users/USER/Downloads/College.csv")
rownames(college) <- college[,1]
Or this upon import
college<- read.csv("C:/Users/USER/Downloads/College.csv", header = TRUE, row.names = 1)
edited Jan 4 at 2:29
hmhensen
9021718
9021718
answered Jan 3 at 22:49
mokdurmokdur
11
11
college<- read.csv("C:/Users/USER/Downloads/College.csv")
– mokdur
Jan 3 at 22:50
rownames(college) <- college[,1]
– mokdur
Jan 3 at 22:51
add a comment |
college<- read.csv("C:/Users/USER/Downloads/College.csv")
– mokdur
Jan 3 at 22:50
rownames(college) <- college[,1]
– mokdur
Jan 3 at 22:51
college<- read.csv("C:/Users/USER/Downloads/College.csv")
– mokdur
Jan 3 at 22:50
college<- read.csv("C:/Users/USER/Downloads/College.csv")
– mokdur
Jan 3 at 22:50
rownames(college) <- college[,1]
– mokdur
Jan 3 at 22:51
rownames(college) <- college[,1]
– mokdur
Jan 3 at 22:51
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%2f54030122%2fusing-dataframes-first-data-column-for-row-names%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
you can see duplicate: stackoverflow.com/questions/5555408/…
– YOLO
Jan 3 at 22:34
I saw that before posting. I'd like to do it without reassigning and his non-reassigning method is the third one of the four attempts I listed.
– JohnDoeVsJoeSchmoe
Jan 3 at 22:37
I just went and tried his reassigning method for good measure, and got a
invalid row.names lengtherror.– JohnDoeVsJoeSchmoe
Jan 3 at 22:42