-2

i'm new in R, and i try to operate with data frame: screen how to get numeric array from row 10 ar <-df[10,1] did't work

  • 1
    Welcome to Stackoverflow! This site could help improving your post: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. – Tlatwork Jul 16 '18 at 18:55
  • Another good resource for improving your question: [ask] – Jaap Jul 16 '18 at 19:17
  • Is that JSON? You might do something like `lapply(df$ym.s.goalsID, jsonlite::fromJSON)` to have everything broken out. If you need it to remain in the frame, you might switch to a "tidy" way of dealing with frames such as in the `tidyverse` using packages `tidyr` and `purrr`. Good reference for them: http://dplyr.tidyverse.org/ – r2evans Jul 16 '18 at 19:23
  • no, its factor, here is output: http://prntscr.com/k774ua – Alexey Yurov Jul 16 '18 at 19:34

1 Answers1

0

You can use gsub to remove brackets. Please see the code below:

# Simulation
x <- factor(c("[1]", "[2,3]", "[4]", "[]"))
str(x)
# Factor w/ 4 levels "[]","[1]","[2,3]",..: 2 3 4 1


foobar <- lapply(x, function(x) {
  # remove brackets
 s <- gsub("\\[||\\]", "", as.character(x)) 
 as.numeric(unlist(strsplit(s, split = ",")))
})

str(foobar)

Output:

List of 4
 $ : num 1
 $ : num [1:2] 2 3
 $ : num 4
 $ : num(0) 
Artem
  • 3,304
  • 3
  • 18
  • 41