【问题标题】:Why is read.csv() giving me a list from a .csv file instead of a data frame? [duplicate]为什么 read.csv() 给我一个来自 .csv 文件而不是数据框的列表? [复制]
【发布时间】:2021-12-07 12:13:09
【问题描述】:

一个similar question 确实有类似类型的问题,但似乎涉及一些拼写错误,并且没有使用正确的功能,不知道它不是数据框。我的正在作为表格结构的 .csv 文件中的列表读入。我还尝试了这个 question 中建议的 do.call 中的 rbind 选项,但没有奏效,所以它被注释掉了,它仍然显示为 typeof() 下的列表。

这是数据集的公开 Google spreadsheet,这是我的可重现代码:

# Read study file
getwd()
bank <- read.csv("attemptCSV.csv")
bank
typeof(bank)
bank <- data.frame(bank)
typeof(bank)
colnames(bank)
#do.call(rbind.data.frame, bank)
#typeof(bank)

#> typeof(bank)
#[1] "list"
#> bank <- data.frame(bank)
#> typeof(bank)
#[1] "list"
#> colnames(bank)
# [1] "age"       "job"       "marital"   "education" "default"   "balance"   "housing"   "loan"     
# [9] "contact"   "day"       "month"     "duration"  "campaign"  "pdays"     "previous"  #"poutcome" 
#[17] "y"         

【问题讨论】:

    标签: r dataframe csv


    【解决方案1】:

    应该是class而不是typeof,因为根据?typeof

    typeof 确定任何对象的(R 内部)类型或存储方式

    class(bank)
    

    -检查

    > data(iris)
    > typeof(iris)
    [1] "list"
    > class(iris)
    [1] "data.frame"
    > is.list(iris)
    [1] TRUE
    > is.data.frame(iris)
    [1] TRUE
    

    原因还在于data.frame 是一个list,其中元素(columns) 的长度相同

    【讨论】:

    • 哦,哎呀哈哈,对此感到抱歉,我想因为出于某种原因,这些值都在引号中,因此这些值是以非数字方式读取的,所以这让我很失望。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 2016-04-24
    相关资源
    最近更新 更多