user1398057
user1398057

Reputation: 1159

Is there a way to parse data within R if the data has each row separated out?

I currently have data where each row is separated out. My goal is to be able to combine all the rows together into one neat data table. The data currently looks like so in a text file:

{    "Name":"Person_1_Name"
     "Age":"15"
     }
{    "Name":"Person_2_Name"
     "Age":"25"
     } 
........

I would like to get it so it looks like:

Name                Age
Person_1_Name       15
Person_2_Name       25
.....

Is there a way to accomplish this within R? The crudest method I know of is writing up a search function, but was wondering if there way another way people would know of. Thank you!

Upvotes: 2

Views: 84

Answers (2)

arvi1000
arvi1000

Reputation: 9582

Sure. If this is actually JSON format, use a library for that. But you can also just parse the raw text.

yr_data <-
'{    "Name":"Person_1_Name"
      "Age":"15"
      }
 {    "Name":"Person_2_Name"
      "Age":"25"
      }'

# split by line
yr_data <- unlist(strsplit(yr_data, '\n'))

# subset to only the lines with : in them
yr_data <- grep(':', yr_data, value=T)

# split on the colon
yr_data <- unlist(strsplit(yr_data, ':'))

# name is in every 2nd place, age in every 4th
data.frame(Name=yr_data[seq(2, length(yr_data), by=4)],
           Age=yr_data[seq(4, length(yr_data), by=4)])

Output:

  Name           Age
1 Person_1_Name  15
2 Person_2_Name  25

Upvotes: 3

G. Grothendieck
G. Grothendieck

Reputation: 269501

If the data actually looks like this:

txt <- '[{    "Name":"Person_1_Name",
     "Age":"15"
     },
{    "Name":"Person_2_Name",
     "Age":"25"
     }]'

then the following:

library(rjson)
do.call(rbind, fromJSON(txt))

gives:

     Name            Age 
[1,] "Person_1_Name" "15"
[2,] "Person_2_Name" "25"

If not, then massage it into that form first.

Upvotes: 4

Related Questions