Reputation: 2280
I don't understand why when I read into a csv.
file, when the first column name is empty, is filed in with X1
as well there is an index column at beginning of my data. How can I avoid this?
Data structure:
sample data:
structure(list(X1 = c(110, 210, 310, 1110, 3110, 5310, 8210,
9120, 9390), `110` = c(0.970588235, 0, 0, 0, 0, 0, 0.125, 0,
0), `210` = c(0, 0.5, 0, 0, 0.666666667, 0, 0, 0, 0), `310` = c(0.029411765,
0.25, 0.6, 0, 0, 0, 0, 0, 0), `1110` = c(0, 0, 0, 0.981481481,
0, 0, 0, 0.25, 0), `3110` = c(0, 0, 0, 0, 0.333333333, 0.25,
0, 0, 0.037037037), `5310` = c(0, 0, 0, 0, 0, 0.75, 0, 0, 0.037037037
), `8210` = c(0, 0, 0.2, 0, 0, 0, 0.875, 0, 0), `9120` = c(0,
0.25, 0, 0, 0, 0, 0, 0.75, 0), `9390` = c(0, 0, 0.2, 0.018518519,
0, 0, 0, 0, 0.925925926)), class = c("spec_tbl_df", "tbl_df",
"tbl", "data.frame"), row.names = c(NA, -9L), spec = structure(list(
cols = list(X1 = structure(list(), class = c("collector_double",
"collector")), `110` = structure(list(), class = c("collector_double",
"collector")), `210` = structure(list(), class = c("collector_double",
"collector")), `310` = structure(list(), class = c("collector_double",
"collector")), `1110` = structure(list(), class = c("collector_double",
"collector")), `3110` = structure(list(), class = c("collector_double",
"collector")), `5310` = structure(list(), class = c("collector_double",
"collector")), `8210` = structure(list(), class = c("collector_double",
"collector")), `9120` = structure(list(), class = c("collector_double",
"collector")), `9390` = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))
Upvotes: 2
Views: 72
Reputation: 56
Second option is to specify the colnames through col.names or other package equivalent
library(data.table)
df <- fread("df.csv",
header = TRUE,
col.names = c("","110","210","310","1110","3110","5310","8210","9120","9390"))
Upvotes: 1
Reputation: 886938
We can use row.names = 1
while reading so that it will return with the first column as row names
df1 <- read.csv('file.csv', row.names = 1, check.names = FALSE)
Upvotes: 2