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

时间:2014-01-28 07:25:49

标签: r mongodb rmongodb

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

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)

我的位置产生以下输出:

[9972,] "NA"                                        
[9973,] ""                                          
[9974,] ""                                          
[9975,] ""                                          
[9976,] ""                                          
[9977,] "Madrid"                                    
[9978,] ""                                          
[9979,] ""                                          
[9980,] "San Antonsdnndsjo\todurnv\tkckdn"                               
[9981,] ""                                          
[9982,] ""                                          
[9983,] ""                                          
[9984,] ""                                          
[9985,] ""     

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

1 个答案:

答案 0 :(得分:0)

不确定我的问题是否正确,但不会在这些方面做任何事情:

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' ) ) )

完成工作(如果你坚持使用rbind())?