as(x,class(k))中的错误:没有将“null”强制为“data.frame”的方法或默认值

nukf8bse  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(392)

我目前面临下面提到的一个错误,它与将空值强制到Dataframe有关。数据集确实包含空值,但是我尝试了is.na()和is.null()函数来用其他函数替换空值。数据存储在hdfs上,并以pig.hive格式存储。我还附上了下面的代码。如果我从键中删除v[,25],代码工作正常。
代码:

AM = c("AN");
UK = c("PP");
sample.map <- function(k,v){
 key <- data.frame(acc = v[!which(is.na(v[,1],1], 
                   year = substr(v[!which(is.na(v[,1]),2],1,4), 
                   month = substr(v[!which(is.na(v[,1]),2],5,6))
 value <- data.frame(v[,3],count=1)
 keyval(key,value)
}

sample.reduce <- function(key,v){
  AT <- sum(v[which(v[,1] %in% AM=="TRUE"),2])
  UnknownT <- sum(v[which(v[,1] %in% UK=="TRUE"),2])
  Total <- AT + UnknownT
  d <- data.frame(AT,UnknownT,Total)
  keyval(key,d)
 }
 out <- mapreduce(input ="/user/hduser/input",
             output = "/user/hduser/output",
             input.format = make.input.format("pig.hive", sep = "\u0001")                            
             output.format = make.output.format("csv", sep = ","),
             map= sample.map)
             reduce = sample.reduce)

错误:

Warning in asMethod(object) : NAs introduced by coercion 
Warning in split.default(1:rmr.length(y), unique(ind), drop = TRUE) :   data length is not a multiple of split variable 
Warning in rmr.split(x, x, FALSE, keep.rownames = FALSE) :   number of items to replace is not a multiple of replacement length Warning in        split.default(1:rmr.length(y), unique(ind), drop = TRUE) :   
data length is not a multiple of split variable 
Warning in rmr.split(v, ind, lossy = lossy, keep.rownames = TRUE) :   number of items to replace is not a multiple of replacement length 
Error in as(x, class(k)) :    
no method or default for coercing “NULL” to “data.frame” 
Calls: <Anonymous> ... apply.reduce -> c.keyval -> reduce.keyval -> lapply -> FUN -> as No traceback available

更新我已经添加了样本数据并编辑了上面的代码。希望这有帮助!
样本数据:

NULL,"2014-03-14","PP"
345689202,"2014-03-14","AN"
234539390,"2014-03-14","PP"
123125444,"2014-03-14","AN"
NULL,"2014-03-14","AN"
901828393,"2014-03-14","AN"
7vhp5slm

7vhp5slm1#

有一些问题 as 最近发现的。我不明白为什么 as 默认情况下无法处理此问题,但您可以修改 coerce 它用一个s4方法来调用 as.data.frame .

setMethod("coerce",c("NULL","data.frame"), function(from, to, strict=TRUE) as.data.frame(from))
[1] "coerce"
as(NULL,"data.frame")
data frame with 0 columns and 0 rows

相关问题