[R] how to create a new data given a vector of variable names

christy saunders.christy at ymail.com
Tue May 15 07:12:16 CEST 2012


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. 

>x

          h 1        h 2        h 3         h 4        h 5        h 6       
h 7        h 8        h 9
1  0.38971928 0.62884802 0.32708216 0.093909834 0.57773251 0.41258918
0.37360577 0.65259411 0.88204799
2  0.51890830 0.15949863 0.75715149 0.871781822 0.06321826 0.91844114
0.05692871 0.84588084 0.77173376
3  0.94057256 0.16100731 0.80961141 0.239716639 0.55804412 0.42854829
0.54987115 0.68416629 0.24353692
4  0.19895720 0.52955693 0.98471869 0.378197899 0.16774788 0.68029534
0.42039730 0.82217244 0.74397124
5  0.27899679 0.29145024 0.07198476 0.732466508 0.14887818 0.90658800
0.64186885 0.66542828 0.98182923
6  0.69375077 0.05840897 0.77325437 0.866099979 0.75063858 0.94230759
0.72182389 0.65574673 0.27406027
7  0.35033643 0.22525597 0.81657974 0.000762193 0.88383211 0.98120966
0.29471244 0.32119662 0.10313222
8  0.40616362 0.37962815 0.80085463 0.919385580 0.47183711 0.15078169
0.93693666 0.24638847 0.12288727
9  0.07939773 0.39030956 0.50235863 0.516507293 0.49247563 0.30633870
0.45665595 0.25479969 0.34689089
10 0.68677267 0.32089352 0.61330153 0.444584299 0.15588483 0.30584289
0.78482250 0.55628942 0.81763581
11 0.47406350 0.75586693 0.19546691 0.698137899 0.47609057 0.56439955
0.33120842 0.54064656 0.36384570
12 0.73796417 0.32741375 0.60800036 0.249716033 0.21919825 0.14749886
0.53495852 0.74101013 0.69063797
13 0.87890769 0.77631054 0.76307442 0.561350947 0.73865259 0.58031305
0.06972116 0.53286669 0.09135791
14 0.91022993 0.52290742 0.21219953 0.209784849 0.90892801 0.03580675
0.19870342 0.79300520 0.85703181
15 0.11331488 0.67744821 0.96226396 0.350925439 0.32038355 0.39465379
0.38653925 0.09538576 0.04436648
16 0.71950535 0.77548893 0.60316799 0.123102348 0.10200008 0.05392754
0.17026972 0.17092818 0.35550621
17 0.29593089 0.75526797 0.52088596 0.629731365 0.13592383 0.20219434
0.63906356 0.55297375 0.30580842
18 0.02915505 0.56244353 0.62397566 0.770202648 0.07929744 0.08574671
0.36506494 0.47563923 0.84796898
19 0.27369892 0.95739919 0.63443013 0.810165262 0.10230919 0.52165672
0.84467928 0.60684813 0.02245486
20 0.31494866 0.26169713 0.84314426 0.239598362 0.59996122 0.46954979
0.99728261 0.28905422 0.91817317
          h 10
1  0.552413907
2  0.130387427
3  0.523121318
4  0.610000351
5  0.005378552
6  0.275925081
7  0.939273614
8  0.152024143
9  0.216325412
10 0.577869906
11 0.484999656
12 0.686217251
13 0.920351777
14 0.924500707
15 0.577019180
16 0.824386203
17 0.130089829
18 0.539668426
19 0.776488706
20 0.992742685


and i have a vector of strings which I called vars.

> vars
[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.



More information about the R-help mailing list