【问题标题】:Read ".dat" to R (import dat file into R)将“.dat”读入 R(将 dat 文件导入 R)
【发布时间】:2015-11-27 17:16:39
【问题描述】:

我正在尝试读取这些数据:http://www.biostat.umn.edu/~brad/data/smoking.datR。 我在http://stackoverflow.com/questions/11664075/import-dat-file-into-r中使用了答案

read.table("http://www.biostat.umn.edu/~brad/data/smoking.dat", 
           header=TRUE,, sep="\n", skip=2)

它有效,但给出了错误的数据。

head(x)
                  list.regions.81..num...c.8..5..3..8..5..1..6.
1                 7, 3, 5, 7, 7, 2, 2, 5, 6, 6, 7, 4, 8, 7, 6, 
2 6, 2, 8, 4, 4, 10, 4, 3, 7, 6, 5, 7, 7, 7, 5, 6, 4, 9, 4, 7, 
3  4, 5, 9, 3, 7, 5, 5, 4, 5, 6, 6, 5, 2, 6, 2, 8, 7, 6, 5, 6, 
4       3, 6, 6, 6, 6, 4, 10, 8, 3, 4, 2, 6, 5, 7, 7, 4, 7, 6, 
5                                                2),sumnum=441,
6                            adj=c(2, 5, 6, 8, 11, 45, 75, 80, 

其实,在这个数据中有一些列表。

【问题讨论】:

  • x <- dget("http://www.biostat.umn.edu/~brad/data/smoking.dat") 也许?
  • 似乎只导入了第二个列表——这就是你想要的吗?无论哪种方式,您都可以通过 res <- do.call(cbind.data.frame, x[-1L]) 转换为数据框
  • @DavidArenburg 你是对的,它只获得第二个列表。让我们看看是否可以拆分dget-ing之前的文本。我想不出更聪明的方法;我从来没有尝试过像那样读取多个对象。
  • @DavidArenburg 更新,如果有更简单的方法请告诉我

标签: r


【解决方案1】:

您无法使用read.table() 读取此文件,因为它不是表格。相反,它是由dput() 生成的R 对象(在本例中为两个 列表)的文本表示。正如 David Arenburg 上面建议的那样,您应该使用 dget()。我是httr 包的忠实粉丝。

编辑: 用于单个页面上任意数量的 list 对象:

put_multiple_objs_from_url <- function(url){
  require(httr)  
  request <- GET(url)
  stop_for_status(request)
  text_lines <- readLines(textConnection(content(request, as = 'text')))

  # look for lines that start with "list(" to determine file parts
  start_lines <- grep('^list\\(',  text_lines)
  end_lines <- integer(length(start_lines))
  for (i in 1:(length(start_lines)-1) ){
    end_lines[i] <- start_lines[i+1] - 1
  }
  end_lines[length(start_lines)] <- length(text_lines)

  # dget each of these file parts as an element of obj_list 
  obj_list <- vector("list",length(start_lines))
  for( i in 1:length(start_lines) ){
    obj_txt <- paste0(text_lines[start_lines[i]:end_lines[i]],
                      collapse=" ")
    obj_list[[i]] <- dget(textConnection(obj_txt))
  }
  obj_list
}  

x <- put_multiple_objs_from_url("http://www.biostat.umn.edu/~brad/data/smoking.dat")

str(x)
# List of 2
# $ :List of 4
# ..$ regions: num 81
# ..$ num    : num [1:81] 8 5 3 8 5 1 6 7 3 5 ...
# ..$ sumnum : num 441
# ..$ adj    : num [1:441] 2 5 6 8 11 45 75 80 1 8 ...
# $ :List of 9
# ..$ N             : num 223
# ..$ Age           : num [1:223] 49 47 50 55 59 41 55 42 51 49 ...
# ..$ SexF          : num [1:223] 0 0 1 0 0 0 1 1 1 0 ...
# ..$ AgeStart      : num [1:223] 18 14 19 15 18 16 15 18 18 18 ...
# ..$ SIUC          : num [1:223] 1 0 1 1 1 1 1 1 1 1 ...
# ..$ F10Cigs       : num [1:223] 30 20 12 40 20 40 18 40 20 18 ...
# ..$ censored.time1: num [1:223] 1.01 5 4.99 5.04 5 ...
# ..$ censored.time2: num [1:223] 1.97 100 100 100 100 ...
# ..$ County        : num [1:223] 17 21 77 30 25 58 13 16 13 77 ...

【讨论】:

    猜你喜欢
    • 2012-07-24
    • 2020-08-20
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 2018-10-22
    • 2017-06-30
    相关资源
    最近更新 更多