Home > Mobile >  Extract specific text parts from df column R
Extract specific text parts from df column R

Time:12-08

I have a question how to extract parts of the text and convert them df output.

This is an example of my df, output of one row in my one column (content of one cell)

[{"id"=>"aaaaaaaaaaaaaaaa", "effortDate"=>"2021-07-04T23:00:00.000Z", "effort"=>2, "author"=>"a:aa:a"}, {"id"=>"bbbbbbbbbbbbbb", "effortDate"=>"2021-07-11T23:00:00.000Z", "effort"=>1, "author"=>"b:bb:b"}, {"id"=>"ccccccccccccc", "effortDate"=>"2021-07-17T23:00:00.000Z", "effort"=>1, "author"=>"c:cc:c"}]

My expected output would be to have 2 columns with as many rows I get from this string:

effortDate
2021-07-04
2021-04-11

and second column
effort
2
1

Any suggestion how to achieve that?

Thanks!

CodePudding user response:

looks like json-content... but the => messes with the reading. If you replace it with :, you sould be able to read properly.

mystr <- '[{"id"=>"aaaaaaaaaaaaaaaa", "effortDate"=>"2021-07-04T23:00:00.000Z", "effort"=>2, "author"=>"a:aa:a"}, {"id"=>"bbbbbbbbbbbbbb", "effortDate"=>"2021-07-11T23:00:00.000Z", "effort"=>1, "author"=>"b:bb:b"}, {"id"=>"ccccccccccccc", "effortDate"=>"2021-07-17T23:00:00.000Z", "effort"=>1, "author"=>"c:cc:c"}]'

jsonlite::fromJSON(gsub("=>", ":", mystr))
#                 id               effortDate effort author
# 1 aaaaaaaaaaaaaaaa 2021-07-04T23:00:00.000Z      2 a:aa:a
# 2   bbbbbbbbbbbbbb 2021-07-11T23:00:00.000Z      1 b:bb:b
# 3    ccccccccccccc 2021-07-17T23:00:00.000Z      1 c:cc:c
  • Related