2016-03-08 92 views
0

我尝试从包“mongolite”的MongoDB使用R称重传感器的数据,这样的代码:[R原始数据转换为字符

df <- db$find('{}', '{"CurrentId":1,"_id":0}') 

,我想提取收集"CurrentId"和可变"CurrentId"是mongodb中的ObjectId,它可能包含多个ObjectId。

df是这样的:

[[1]] 
list() 

[[2]] 
list() 

[[3]] 
list() 

[[4]] 
list() 

[[5]] 
list() 

[[6]] 
[[6]][[1]] 
[1] 56 cd 5f 02 b8 9b 5b d0 26 cb 39 c9 

[[6]][[2]] 
[1] 56 cd 6c 13 b8 9b 5b d0 26 cb 39 d5 

[[6]][[3]] 
[1] 56 cd 6f c6 b8 9b 5b d0 26 cb 39 de 

而且df[[6]][[1]]是:

[1] 56 cd 5f 02 b8 9b 5b d0 26 cb 39 c9 

typeof(df[[6]][[1]])类型是:

[1] "raw" 

我用paste(dc3[[6]][[1]],collapse = '')转换原始类型字符串,就像mongodb ObjectId的形式T:

[1] "56cd5f02b89b5bd026cb39c9" 

然后我尝试将所有的原始数据中df转换为string像上面。所以我用sapply功能:

sapply(df, function(x) paste(as.character(x),collapse = ''))

,并得到这个:

[1] ""                                                             
[2] ""                                                             
[3] ""                                                             
[4] ""                                                             
[5] ""                                                             
[6] "as.raw(c(0x56, 0xcd, 0x5f, 0x02, 0xb8, 0x9b, 0x5b, 0xd0, 0x26, 0xcb, 0x39, 0xc9))as.raw(c(0x56, 0xcd, 0x6c, 0x13, 0xb8, 0x9b, 0x5b, 0xd0, 0x26, 0xcb, 0x39, 0xd5))as.raw(c(0x56, 0xcd, 0x6f, 0xc6, 0xb8, 0x9b, 0x5b, 0xd0, 0x26, 0xcb, 0x39, 0xde))" 

但我想是这样的:

[[1]] 
list() 

[[2]] 
list() 

[[3]] 
list() 

[[4]] 
list() 

[[5]] 
list() 

[[6]] 
[[6]][[1]] 
[1] "56cd5f02b89b5bd026cb39c9" 

[[6]][[2]] 
[1] "56cd6c13b89b5bd026cb39d5" 

[[6]][[3]] 
[1] "56cd6fc6b89b5bd026cb39de" 

谁能知道如何来处理呢?是否有更有效的方法来完成整个工作?

更新:

我应该给一些代码来重现我的原点数据集:

test = as.raw(as.hexmode(x = c("56","cd","5f","02","b8","9b","5b","d0","26","cb","39","c9"))) 
df = lapply(1:10,function(x) test) 

虽然这段代码产生这样的:

[[1]] 
list() 

[[2]] 
[[2]][[1]] 
[1] 5f 

[[2]][[2]] 
[1] d0 


[[3]] 
[[3]][[1]] 
[1] 26 

[[3]][[2]] 
[1] 56 


[[4]] 
list() 

[[5]] 
[[5]][[1]] 
[1] cb 


[[6]] 
list() 

它不象原来df,但我真的不知道如何将原始数据粘贴到嵌套列表中,希望这可以帮助你!

sapply(df, function(x) paste(x,collapse = ''))结果却是这样的:

[1] ""                                                             
[2] ""                                                             
[3] ""                                                             
[4] ""                                                             
[5] ""                                                             
[6] "as.raw(c(0x56, 0xcd, 0x5f, 0x02, 0xb8, 0x9b, 0x5b, 0xd0, 0x26, 0xcb, 0x39, 0xc9))as.raw(c(0x56, 0xcd, 0x6c, 0x13, 0xb8, 0x9b, 0x5b, 0xd0, 0x26, 0xcb, 0x39, 0xd5))as.raw(c(0x56, 0xcd, 0x6f, 0xc6, 0xb8, 0x9b, 0x5b, 0xd0, 0x26, 0xcb, 0x39, 0xde))" 

回答

0

只需使用paste()没有你的电话sapply()中调用as.character()。 简单的例子:

convertRaw = function(x) paste(x,collapse = '') # works identical in sapply 
test = as.raw(as.hexmode(x = c("56","cd","5f","02","b8","9b","5b","d0","26","cb","39","c9"))) # line copied from your sample 
convertRaw(test) 
[1] "56cd5f02b89b5bd026cb39c9" 

更新 居然还有从使用嵌套表导致的另一个问题。由于你处理了一个嵌套列表,所以你的sapply调用也需要嵌套。您可以通过lapply()调用它。下面是一个简单的例子,希望最终解决您的问题:

test = as.raw(as.hexmode(x = c("56","cd","5f","02","b8","9b","5b","d0","26","cb","39","c9"))) 
testList = list(list(),list(test,test)) # here I create a short nested list 
res = lapply(testList,function(y) sapply(y,function(x) paste(x,collapse = ''))) 
print(res) 

结果是:

[[1]] list() 

[[2]] [1] "56cd5f02b89b5bd026cb39c9" "56cd5f02b89b5bd026cb39c9" 

如果你喜欢这样的:

[[1]] list() 

[[2]] [[2]][[1]] 
[1] "56cd5f02b89b5bd026cb39c9" 

[[2]][[2]] 
[1] "56cd5f02b89b5bd026cb39c9" 

只需拨打,lapply()嵌套:

lapply(testList,function(y) lapply(y,function(x) paste(x,collapse = ''))) 
+0

嗨,大卫。我试过你的解决方案,但我仍然得到相同的结果:“as.raw(c(0x56,0xcd,0x5f,0x02,0xb8,0x9b,0x5b,0xd0,0x26,0xcb,0x39,0xc9))as.raw(c 0x56,0xcd,0x6c,0x13,0xb8,0x9b,0x5b,0xd0,0x26,0xcb,0x39,0xd5))as.raw(c(0x56,0xcd,0x6f,0xc6,0xb8,0x9b,0x5b,0xd0,0x26,0xcb) ,0x39,0xde))“ – jjdblast

+0

你误读我的建议。我建议,不要这样做:你会输入 –

+0

下一个评论,因为我不能编辑了... 你误解了我的解决方案。我建议不要这样: 'sapply(df,function(x)paste(as.character(x),collapse =''))' 你会输入: 'sapply(df,function x)paste(x,collapse =''))' 转换'as.character()'会返回你不想要的奇怪结果。不要称之为转换是必要的! 在我的答案的第二行我只是读了一个样本,在第三行我称之为实际函数。 –

相关问题