0

I have a table with columns ranging from foo1...... foo999 . Could you give me a code in R to extract a set number of columns as needed by the user. The user should be able to decide about what range of columns he needs i.e from 1 to 60 in a separate table or maybe its 1 to 565 that the user needs. I tried a couple of methods. This is what I have currently. The solution seems to be quite basic but I can't find it anywhere.

number <- readline(prompt="Enter the number of columns: ")
subset(data, select=foo1:foo(number))

The expected output is the contents of the columns from the range that the user needs preferably stored in another variable so that I could use the data for further analysis.

The Last Word
  • 203
  • 1
  • 7
  • 24
  • Welcome to SO. Please have a look at [how to ask a good question](https://stackoverflow.com/help/how-to-ask) as well as [how to give reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Sotos Jun 13 '17 at 17:56

1 Answers1

1

Here's a construct that could do it:

number <- readline(prompt="Enter the number of columns: ")
columns<- eval(parse(text=number))

df_selected <- df[,columns]

This will handle the user entering something like 3:8 or c(1,4,9), etc.

Matt Tyers
  • 2,125
  • 1
  • 14
  • 23