I have got this array in a string variable (k) and would like to convert it to a dataframe in r. The as.array(k) does not convert it. What is the best way to do this. Also, considering that there will be nested arrays in the future to be dealt with.
places = [[\"Abtenau\", \"Abtenau\", 767, 52, 3.0000],[\"Achenkirch\", \"Achenkirch am Achensee\", 248, 61, 6.0000],[\"Adelharz\", \"Adelharz\", 47, 46, 2.0000],[\"Alpbach\", \"Alpbachtal\", 2629, 89, 2.0000]]
An option would be jsonlite
to convert to a matrix
, then change it to data.frame
and retype
library(jsonlite)
library(hablar)
library(dplyr)
fromJSON(places) %>%
as.data.frame %>%
retype
# A tibble: 4 x 5
# V1 V2 V3 V4 V5
# <chr> <chr> <int> <int> <int>
#1 Abtenau Abtenau 767 52 3
#2 Achenkirch Achenkirch am Achensee 248 61 6
#3 Adelharz Adelharz 47 46 2
#4 Alpbach Alpbachtal 2629 89 2
Or this can be done with base R
after removing the square brackets with regex
read.csv(text = gsub('[][]|"', "", gsub("(?<=\\]),(?=\\[)", "\n",
places, perl = TRUE)), header = FALSE, stringsAsFactors = FALSE)
# V1 V2 V3 V4 V5
#1 Abtenau Abtenau 767 52 3
#2 Achenkirch Achenkirch am Achensee 248 61 6
#3 Adelharz Adelharz 47 46 2
#4 Alpbach Alpbachtal 2629 89 2
places <- "[[\"Abtenau\", \"Abtenau\", 767, 52, 3.0000],[\"Achenkirch\", \"Achenkirch am Achensee\", 248, 61, 6.0000],[\"Adelharz\", \"Adelharz\", 47, 46, 2.0000],[\"Alpbach\", \"Alpbachtal\", 2629, 89, 2.0000]]"
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.