代码之家  ›  专栏  ›  技术社区  ›  esterodr

如何将Excel文件读取为数据框

  •  1
  • esterodr  · 技术社区  · 8 年前

    base <- read.table("http://www.imf.org/external/pubs/ft/weo/2017/02/weodata/WEOOct2017all.xls", header=TRUE, sep="\t", fill=TRUE)
    

    但是,数据保存为列表:

    typeof(base)
    [1] "list"
    

    graph <- base[which((base[2]=="ARG")&(base[3]=='NGDP_RPCH')),40:49]
    graph
    [1] X2010 X2011 X2012 X2013 X2014 X2015 X2016 X2017 X2018 X2019
    <0 rows> (or 0-length row.names)
    

    我想知道如何将数据保存为数据帧,或者如何从列表中提取数据向量,以便运行:

    barplot(graph).
    
    1 回复  |  直到 8 年前
        1
  •  5
  •   joran    8 年前

    首先,使用适当的工具导入Excel文件。这可能包括软件包 readxl , XLConnect连接 ,

    library(readxl)
    base <- read_excel("~/Desktop/WEOOct2017all.xls")
    

    [[ 选择单个列,而不是 [ .

    graph <- base[which((base[[2]]=="ARG")&(base[[3]]=='NGDP_RPCH')),40:49]
    

    并转换为矩阵以使用 barplot .

    barplot(as.matrix(graph))