我有一个名为
data.table
的 dat
对象,如果我尝试 write.table(dat,'test.txt')
,我会收到这个奇怪的错误:
'Error in sc[[length(sc) - 7L]][[1L]] == "example": comparison (1) is possible only for atomic and list types'
跟随堆栈跟踪将我带到 cedta.R
cedta.R 超出了我的能力范围。这是怎么回事?我该如何解决这个问题?
在上下文中,我使用
fread()
从文本文件中读取一些数据,然后使用条件从表中选择一些行,然后我想将过滤后的表写入新的文本文件。很简单,我很困惑这里出了什么问题。
dat <- fread(ptx, skip = 10, header = FALSE)
names(dat) <- c("X", "Y", "Z", "Intensity", "R", "G", "B")
ptx_header = readLines(ptx,n=2)
ncols = as.integer(ptx_header[1])
nrows = as.integer(ptx_header[2])
if (reducePtx==T){
# Remove every other point from PTX file
npoints = dim(dat)[1]
cols = 0:(npoints-1) %/% ncols
rows = rep.int(0:(nrows-1),ncols)
dat1 = dat[cols%%2==0 & rows%%2==0,]
remove(cols)
remove(rows)
# Write output ptx file
ptx_out = paste0('reduced_',ptx)
write.table(dat1,ptx_out,append = T,sep=' ',quote=F,row.names = F,col.names = F)}
我遇到了同样的问题,我能想到的最好办法就是在写入之前将对象转换为 data.frame 。换句话说,不要尝试使用
write.table(dat,'test.txt')
,而尝试使用 write.table(as.data.frame(dat),'test.txt')
。虽然不是很满意,但确实有效。