The Last Word
The Last Word

Reputation: 203

Range of columns from table based on user input

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.

Upvotes: 0

Views: 61

Answers (1)

Matt Tyers
Matt Tyers

Reputation: 2215

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.

Upvotes: 1

Related Questions