hi, please help me on this. I'm very new to R. I've been figuring out how to
do this the whole day, and I could not get the correct R code.
Suppose I have a dataframe called x and it consists of 10variables.
[1] "h 8" "h 4" "h 10" "h 1"
the variables inside vars are subset of the column names of x. The order is
important. In my dataframe, I want to obtain the following:
newdata
h 8 h 4 h 10 h 1
[1,] 0.65259411 0.093909834 0.552413907 0.38971928
[2,] 0.84588084 0.871781822 0.130387427 0.51890830
[3,] 0.68416629 0.239716639 0.523121318 0.94057256
[4,] 0.82217244 0.378197899 0.610000351 0.19895720
[5,] 0.66542828 0.732466508 0.005378552 0.27899679
[6,] 0.65574673 0.866099979 0.275925081 0.69375077
[7,] 0.32119662 0.000762193 0.939273614 0.35033643
[8,] 0.24638847 0.919385580 0.152024143 0.40616362
[9,] 0.25479969 0.516507293 0.216325412 0.07939773
[10,] 0.55628942 0.444584299 0.577869906 0.68677267
[11,] 0.54064656 0.698137899 0.484999656 0.47406350
[12,] 0.74101013 0.249716033 0.686217251 0.73796417
[13,] 0.53286669 0.561350947 0.920351777 0.87890769
[14,] 0.79300520 0.209784849 0.924500707 0.91022993
[15,] 0.09538576 0.350925439 0.577019180 0.11331488
[16,] 0.17092818 0.123102348 0.824386203 0.71950535
[17,] 0.55297375 0.629731365 0.130089829 0.29593089
[18,] 0.47563923 0.770202648 0.539668426 0.02915505
[19,] 0.60684813 0.810165262 0.776488706 0.27369892
[20,] 0.28905422 0.239598362 0.992742685 0.31494866
I tried to do the following but it does not give me what I want:
x[names(x)==names(x[names(x)%in%vars])]
Thank you very much.
~Christy
--
View this message in context: http://r.789695.n4.nabble.com/how-to-create-a-new-data-given-a-vector-of-variable-names-tp4630024.html
Sent from the R help mailing list archive at Nabble.com.