0

How to eval parse a string with spaces in it

I created a example:

# Change the name of, the default in r available dataset, ChickWeight to get 
# a variable with a space in it
        `Chick Weight` <- ChickWeight

        datasetnames <- c("iris", "cars", "Chick Weight")

        head(eval(parse(text = datasetnames[1])))

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

        head(eval(parse(text = datasetnames[2])))

  speed dist
1     4    2
2     4   10
3     7    4
4     7   22
5     8   16
6     9   10

        head(eval(parse(text = datasetnames[3])))

Error in parse(text = datasetnames[3]) : <text>:1:7: unexpected symbol
1: Chick Weight

So it doen't work when i put a string with a space within the eval(parse(text=)) functionality. How could i do this?

Or is their another method to approach a vriable with a space in it from a string?

Berecht
  • 1,085
  • 9
  • 23

0 Answers0