简体   繁体   中英

R convert raw data to character

I try to use R load data from mongodb with package "mongolite", and the code like this:

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

Which I want to extract the "CurrentId" of collection, and variable "CurrentId" is ObjectId in mongodb, which may contains several ObjectId.

and the df look like this:

[[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

And df[[6]][[1]] is :

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

the type of typeof(df[[6]][[1]]) is :

 [1] "raw"

I use paste(dc3[[6]][[1]],collapse = '') convert the raw type to string, just like mongodb ObjectId format:

 [1] "56cd5f02b89b5bd026cb39c9"

And then I try to convert all the raw data in df to string like above. So I use sapply function:

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

and got this :

[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))"

But I want to get something like this:

[[1]]
list()

[[2]]
list()

[[3]]
list()

[[4]]
list()

[[5]]
list()

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

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

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

Could anyone know how to handle this? And could there be more efficient way to do the whole work?

Update:

I should give some code to reproduce my origin dataset:

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)

although this code produce this:

[[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()

It's not like original df , but I really don't know how to paste Raw data in nested list, hope this may help you!

the result of sapply(df, function(x) paste(x,collapse = '')) is just like this:

[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))"

just use paste() without calling as.character() within your sapply() call. Short example:

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"

Update actually there is another issue that results from using a nested list. Because of you dealing with a nested list your sapply call needs to be nested too. You can ie call it via lapply() . Here is a short example hope that finally resolves your issue:

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) 

the result is:

[[1]] list() 

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

if you prefer this:

[[1]] list()

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

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

simply call, lapply() nested:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM