2

I have a data set like,

En     Mn    Hours  var1       var2 
1      1     1      0.1023488  0.6534707 
1      1     2      0.1254325  0.5423215 
1      1     3      0.1523245  0.2542354 
1      2     1      0.1225425  0.2154533 
1      2     2      0.1452354  0.4521255 
1      2     3      0.1853324  0.2545545 
2      1     1      0.1452369  0.2321542 
2      1     2      0.1241241  0.2525212 
2      1     3      0.0542232  0.2626214 
2      2     1      0.8542154  0.2154522 
2      2     2      0.0215420  0.5245125 
2      2     3      0.2541254  0.2542512 

var <- as.character(readline('Enter the variable of your choice')) 

the var stores the user input Ex: var1

and if I use it in some R commands say cbind or any for that matter, it doesn't work. simple one if a is a dataset a$var doesn't work. Ex: aggregate(cbind(var)~Mn+hours,a, FUN=mean)

And I have a var1, var2, var3 like some 30 columns and I want to read multiple inputs from the user

like when user is prompted to enter the variable names he enters like var1 var2 var3 (space or comma separated it doesn't matter for me) then I need to read them and use them in the R command line to get some result.

rcs
  • 67,191
  • 22
  • 172
  • 153
cppiscute
  • 707
  • 2
  • 10
  • 33

1 Answers1

0

Lets say your dataframe is named df...And user entered column name as you said....

var <- as.character(readline('Enter the variable of your choice')) 

df[var] = 0 #assign actual values here... As var one named column is added

You can run it in loop the same code and create n number of columns...

Edit

Now if you want to use user input for various purpose then following are the case,

  1. When you want to refer column directly with data.frame that will be easy

    df[var]

  2. When you want to refer column in some function or formula then you have to do it like this

    aggregate(cbind(get(var1),get(var2))~Mn+hours,a, FUN=mean)

Concept behind get() is, it will put actual value (column name) in formula for you...

Updated

Refer this for specially aggregate function : Name columns within aggregate in R

I hope this will work for you...

Community
  • 1
  • 1
vrajs5
  • 4,066
  • 1
  • 27
  • 44
  • Thanks. I don't want to assign the value to any column. I want to select the column based on the user input. like user enters two variable names that will be stored inside var. Ex: var1 and var2 stored inside var. Now I want to use them inside say aggregate(cbind(var1,var2)~Mn+hours,a, FUN=mean) – cppiscute Jun 02 '14 at 11:04
  • So you are trying to say you won't alter your table once created.. You will just use user input in various functions? – vrajs5 Jun 02 '14 at 11:06
  • Yeah, I wont be altering the main dataset, and I will be using the user input to select the columns. – cppiscute Jun 02 '14 at 11:08
  • get command replaces the original column names and names it with V1,V2...etc in the resulting dataset. But How to hold on to the original column names? This you need if you are reaggranging the dataset and then using it to plot the (in ggplot example) – cppiscute Jun 02 '14 at 11:21
  • Each function you take, you have to use it differently. So just list out functions in which you require this kind of methodology. in ggplot you can always change the x-y-labels... – vrajs5 Jun 02 '14 at 11:23
  • after I get the data into gg1. (using get) next I will be doing like ggplot(gg1, aes(x = hours, y = get(var), group = Mn, fill = Mn, color = Mn)) + geom_point() This is not working, I can always use the column name directly if the dataset is fixed, like y = gg1[,3] but is there any method to call y = variable_name – cppiscute Jun 02 '14 at 11:30
  • `ggplot(gg1, aes(x = hours, y = get(var), group = Mn, fill = Mn, color = Mn)) + geom_point()+ labs(list(y=var))` - is working perfectly for me... var has column name.. – vrajs5 Jun 02 '14 at 11:56
  • Sorry, I mean ggx <- aggregate(cbind(get(var))~Mn+hours,a, FUN=mean) This ggx is not having column name for var. dataset a has column name and I want to retain it in ggx also. But if I apply get(var) here then this variable column name in ggx will be like V1 and not the same as a's column name. – cppiscute Jun 02 '14 at 12:10
  • I already told you, each function has its own way of handling column.. aggregate by default give V1,V2 names to columns produced after calculation. You have to change it manually... – vrajs5 Jun 02 '14 at 12:16
  • Ok, I was hoping there is some other way to handle this. Anyway thanks for the clarification. Its really helpful. – cppiscute Jun 02 '14 at 12:19