【问题标题】:Removing dataframe from list with no data RStudio从没有数据 RStudio 的列表中删除数据框
【发布时间】:2020-04-20 04:15:02
【问题描述】:

我的大部分项目都使用从网络下载信息的列表。有时,当我下载 100 组不同的数据时,网站并没有为我提供其中一些的数据。

你可以说它没有数据,因为它说。

具有 0 行和 7 列的 data.frame。

一个好的数据框是这样说的。

具有 245345 行和 7 列的 data.Frame。

我的脚本不喜欢列表中没有数据。它在那个地方停止了我的循环。

提前谢谢你。

 #Pulls all the active USGS gages for the URL's 
    GageList <- CDEC
    gage <- c(as.character(GageList$GAGE_ID))
    duration <- c(as.character(GageList$DURATION_CODE))
    number <- c(as.character(GageList$SENSOR_CODE))
    View(GageList)

    #CDEC URL
    urls <- sprintf(final=list()
    TOTALERRORS =list()

    #Pulls all the active USGS gages for the URL's 
    GageList <- CDEC
    gage <- c(as.character(GageList$GAGE_ID))
    duration <- c(as.character(GageList$DURATION_CODE))
    number <- c(as.character(GageList$SENSOR_CODE))
    View(GageList)

    #CDEC URL
    urls <- sprintf("http://cdec.water.ca.gov/cgi-progs/querySHEF? 
    station_id=%s&dur_code=%s&sensor_num=%s&start_date=10/25/2019", 
    gage,duration,number)
    View(urls)



data <- suppressWarnings(lapply(urls, fread, header=TRUE)))

【问题讨论】:

    标签: r list dataframe rstudio


    【解决方案1】:

    不看清单就很难回答。但从你的描述来看,这是一个例子:

    # first here is a list of data.frame
    l = list(data.frame(0,nrow = 2,ncol =5),
             data.frame(0,nrow = 1, ncol=5))
    # here i remove the only row of the second data.frame
    l[[2]] = l[[2]][-1,]
    
    l
    # I set a data.frame identifiying the dim of each data.frame
    d2rm = data.frame(n = rep(1:length(l),each = 2),
                      empty =unlist(lapply(l,dim)) == 0)
    
    # I remove from the list the data.frames that have dim of 0 (in col or row)
    l[[d2rm[which(d2rm$empty),1]]] = NULL
    
    l
    

    【讨论】:

      猜你喜欢
      • 2021-01-12
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      • 2019-02-24
      • 2017-10-28
      • 2020-02-14
      • 2021-09-29
      相关资源
      最近更新 更多