gpt4 book ai didi

r - 如何在R中用fread分配列名?

转载 作者:行者123 更新时间:2023-12-04 18:05:31 25 4
gpt4 key购买 nike

我有以下代码 -

zz3 <- 'data,key
"VA1,VA2,20140524,,0,0,5969,20140523134902,S7,S1147,140,20140523134902,m/t",4503632376496128
"VA2,VA3,20140711,,0,0,8824,20140601095714,S1,S6402,175,20140601095839,m/t",4503643113914368
"VA1,VA3,20140710,,0,0,11678,20140604085203,S1,S1430,250,20140604085329,m/t",4503666467799040
"VA2,VA1,20140724,,0,0,7109,20140523133835,S7,S793,130,20140523133835,m/t",4503679218483200
"VA3,VA1,20140925,,0,0,10592,20140604092548,S7,S109,395,20140604092714,m/t",4503694653521920'

columnClasses <- c("or"="factor", "d"="factor", "ddate"="factor", "rdate"="factor", "changes"="integer", "class"="factor", "price"="integer", "fdate"="factor", "company"="factor", "number"="factor", "dur"="integer", "added"="factor", "source"="factor", "key"="NULL") # skip last column "key"
data <- fread(zz3, header = FALSE, sep = ",", skip = 1, na.strings = c(""), colClasses = columnClasses)

但它返回一个错误 -
Error in fread(zz3, header = FALSE, sep = ",", skip = 1, na.strings = c(""),  : 
Column name 'or' in colClasses[[1]] not found

我预计 colClasses指定列名,当 header = FALSE ,但看起来并非如此。

我该如何解决?类似 read.csv代码运行良好。

最佳答案

事实并非如此。
colClasses允许您使用 fread 定义列类型.假设您已将文件拆分为 |使用名为“key”的列并且您希望它是一个字符,您将运行以下命令:fread(filePath, sep='|', colClasses=c(key='character')) .

如果文件中没有名称,可以使用 setnames将列名分配给您的 data.table一旦被读取。

关于r - 如何在R中用fread分配列名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29395223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com