examdata <- RCurl::getURL("https://raw.githubusercontent.com/jrwolf/IT497/master/spendingdata.txt")
examdata2 <- read.table(textConnection(examdata), sep = ",", header = T)
扫描错误(文件,什么,nmax,sep,dec,quote,skip,nlines,na.strings,:第1行没有2个元素
examdata <- RCurl::getURL("https://raw.githubusercontent.com/jrwolf/IT497/master/spendingdata.txt")
examdata2 <- read.table(textConnection(examdata), sep = ",", header = T)
扫描错误(文件,什么,nmax,sep,dec,quote,skip,nlines,na.strings,:第1行没有2个元素
2 回答
看起来你只需要跳过几行 . 我使用
readLines(textConnection(examdata))
来确定实际数据表的开始位置 . 原来它从第32行开始 . 因此,我们可以使用read.csv
中的skip
参数跳过前31行 . 我使用了strip.white
参数,因为表中似乎有一些错误的空格 .由于'll probably want those numbers to be numeric, you'll需要删除
$
符号并将列转换为数字,因此您可以将它们用于以后可能执行的任何计算 .现在除了第一列之外的所有列都是数字 .
read.table
和read.csv
会将URL作为路径并为您处理连接,因此您不需要RCurl
:此外,如果您使用
readr::read_csv
,您可以告诉它将列解析为数字,在读取时删除$
个字符: