Assigning to an object the values that span from a column to another

As per the title, I am trying to make a list of columns from the anscombe dataset, but I want to avoid having to repeat columns like

var_esplicative <- list(anscombe$x1, anscombe$x2, anscombe$x3, anscombe$x4)

So I tried doing something like this var_esplicative <- (anscombe$x1:$x4) to take all the columns ranging from x1 to x4. Nonetheless to say, it didn’t work.

>Solution :

You can use as.list with an indexed dataframe.

as.list(anscombe[, 1:4])

$x1
 [1] 10  8 13  9 11 14  6  4 12  7  5

$x2
 [1] 10  8 13  9 11 14  6  4 12  7  5

$x3
 [1] 10  8 13  9 11 14  6  4 12  7  5

$x4
 [1]  8  8  8  8  8  8  8 19  8  8  8

Leave a Reply