1

I'm looking for the cleanest way to implement the following.

vars <- c("f1", "f2", "f3")
list(data$f1, data$f2, data$f3)

Where instead I make the list by using each string in vars instead of hardcoding the data$f1, data$f2, ...

It can be solved easily using a for loop to create the list but is there a one-liner/lapply way to solve it?

1 Answer 1

2

If data is a dataframe, then it's already a list. You can do this:

data[,vars]

Or you can make it a simple list:

c(data[,vars])
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, didn't know that c(df) yields a list, that's very useful to know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.