简体   繁体   English

CSV 文件读入问题

[英]Csv file read into issues

I don't understand why when I read into a csv.我不明白为什么当我读入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.文件,当第一列名称为空时,也与X1一起归档,并且在我的数据开头有一个索引列。 How can I avoid this?我怎样才能避免这种情况?

Data structure:数据结构:

在此处输入图片说明

Desired output:期望的输出: 在此处输入图片说明

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"))

我们可以在读取时使用row.names = 1 ,以便它将以第一列作为行名称返回

df1 <- read.csv('file.csv', row.names = 1, check.names = FALSE)

Second option is to specify the colnames through col.names or other package equivalent第二个选项是通过 col.names 或其他等效包指定 colnames

library(data.table) 

df <- fread("df.csv",
        header = TRUE,
        col.names = c("","110","210","310","1110","3110","5310","8210","9120","9390"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM