Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
rmongodb:将mongo bson值转换为数据帧_R_Mongodb_Rmongodb - Fatal编程技术网

rmongodb:将mongo bson值转换为数据帧

rmongodb:将mongo bson值转换为数据帧,r,mongodb,rmongodb,R,Mongodb,Rmongodb,我正在R中使用rmongodb运行mongodb查询。我已经获得mongo.cursor对象,需要将这些游标值转换为R数据帧。但是,我的值包含一些空字符串和不需要的长字符,因此我需要将这些空字符串和长字符串转换为NA,以便将它们转换为dataframe。下面是我的代码 library(rmongodb) mongo <- mongo.create(host="localhost") dbns <- mongo.get.database.collections(mongo, db="n

我正在R中使用rmongodb运行mongodb查询。我已经获得mongo.cursor对象,需要将这些游标值转换为R数据帧。但是,我的值包含一些空字符串和不需要的长字符,因此我需要将这些空字符串和长字符串转换为NA,以便将它们转换为dataframe。下面是我的代码

library(rmongodb)
mongo <- mongo.create(host="localhost")
dbns <- mongo.get.database.collections(mongo, db="namedisambiguation")
query <- '{ "name": { "$exists": true }, "username": { "$exists": true } }'
fields <-  '{ "username": 1, "name": 1, "location": 1}'
cur <- mongo.find(mongo, dbns, query=query, fields=fields)
username <- name <- location <- NULL
while (mongo.cursor.next(cur)) {
        value <- mongo.cursor.value(cur)
        username <- rbind(username, mongo.bson.value(value, 'username'))
        name <- rbind(name, mongo.bson.value(value, 'name'))
        location <- rbind(location, mongo.bson.value(value, 'location'))
        }

data2 <- data.frame(username=username, name=name, location=location)

如何将这些空值和长字符串(如“San Antonsdnndsjo\todurnv\tkckdn”)转换为NA?

我不确定是否正确回答了您的问题,但沿着这些思路做的事情不会起作用:

maxlength <- 16

location <- ifelse( length( mongo.bson.value(value, 'location' ) ) == 0 | 
                    length( mongo.bson.value(value, 'location' ) ) > maxlength,
                rbind( location, NA ),
                rbind( location, mongo.bson.value( value, 'location' ) ) )

maxlength为什么不使用
mongo.cursor.to.data.frame(cursor,nullToNA=TRUE,…)
而不是循环和
rbind()
到三个独立的单列data.frames?
maxlength <- 16

location <- ifelse( length( mongo.bson.value(value, 'location' ) ) == 0 | 
                    length( mongo.bson.value(value, 'location' ) ) > maxlength,
                rbind( location, NA ),
                rbind( location, mongo.bson.value( value, 'location' ) ) )