'import comma delimited text file with key value pairs into R as a dataframe
I was not able to find answers to this exact scenario.
I have a comma deliminated TXT file where the column names and values are separated by columns. I would like to import it into R as a dataframe. A simple read.table() does not work because of the key: value pair issue. would appreciate any insight you might have
TXT FILE
{"Hypertension":false,"lave_mains_avec4":false,"SignatureStatus":"Signature on file","sexe":1,"NomFamilleContact":"testnumbertwo","hematologique":false}
In this TXT file, the first column name is hypertension. The value for the column name hypertension is false. The next column name is lave_mains_avec4, the value for this column in false
Thanks so much
Solution 1:[1]
This format is known as JSON (short for JavaScript Object Notation). It is an extremely common and versatile format for exchanging data online. There are multiple R packages for parsing json files, including jsonlite:
library(jsonlite)
read_json("myfile.txt") |> as.data.frame()
#> Hypertension lave_mains_avec4 SignatureStatus sexe NomFamilleContact hematologique
#> 1 FALSE FALSE Signature on file 1 testnumbertwo FALSE
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Allan Cameron |
