as(x,class(k))中的错误:无法将“NULL”强制转换为“data.frame” [英] Error in as(x, class(k)) : no method or default for coercing “NULL” to “data.frame”

查看:538
本文介绍了as(x,class(k))中的错误:无法将“NULL”强制转换为“data.frame”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正面临下面提到的错误,它与被强制转换为数据帧的NULL值相关。数据集确实包含null,但是我已尝试使用is.na()和is.null()函数来替换其他值的空值。数据存储在hdfs中,并以pig.hive格式存储。我也附上了下面的代码。如果我从密钥中删除v [,25],该代码工作正常。

I am currently facing an error mentioned below which is related to NULL values being coerced to a data frame. The data set does contain nulls, however I have tried both is.na() and is.null() functions to replace the null values with something else. The data is stored on hdfs and is stored in a pig.hive format. I have also attached the code below. The code works fine if I remove v[,25] from the key.

代码:

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 

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

UPDATE I have added the sample data and edited the code above. Hope this helps!

样本数据:

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"


推荐答案

有一些问题,而作为已被最近确定。我不明白为什么 as 在默认情况下无法处理,但您可以修改处理转换的 cerce 使用S4方法调用 as.data.frame

There are some issues with as which have been identified recently. I don't see why as can't handle this by default, but you can modify coerce which handles the conversion with an S4 method to call 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

这篇关于as(x,class(k))中的错误:无法将“NULL”强制转换为“data.frame”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆