0

I'm searching since a couple of hours without being able to find out how to get rows by a specific value of the last data frame column.

I have this data:

          X1           X2           X3           X4          X5      X6
1    48.17695413  39.43730616   3.10972063   2.61426454   6.3099154  1
2    -8.65390666  46.85736629  30.87960405  44.27429466 -41.1696044  1
3    30.77856742  21.74027874  48.19661027 -21.80938770  36.6618114 -1
4   -24.59780029  21.06814152 -39.33865333 -42.97304431  -4.8804663  1
5     7.27249086  34.76682615 -45.85831994  13.88918648  47.3615198  1
6    38.67101648  38.26274828  14.08033108  49.82309644 -33.6286389  1
7   -25.51283904 -45.70014705 -26.42990421  28.78326026  35.1519014 -1
8    -2.39959008  -2.97487159  17.50174041  12.75989751 -45.0535343  1
9    37.15204622  31.15702239   7.78025487  42.89215957  22.0417423  1
10  -47.73408335 -15.17439773  27.97187380  35.77547702  32.3852375 -1
11   -1.02035564  26.50974274 -30.74983917 -22.68985184 -38.8585380  1
12   22.58425138 -19.56519226   1.71576410 -34.55656213   2.9134512 -1
13   -9.38323175 -19.29783276  16.63345252  40.99594080 -29.0354038  1
14  -27.58411688  48.17471695  46.65622143 -30.48450849  14.9938999 -1
15   38.80037898 -47.36718861  37.63894415 -16.63851268  40.3090175 -1
16   43.51794997  18.72671039  37.09440767 -34.12630649 -15.1023072 -1
17  -32.74301919  11.90051064   2.80043937  46.51653604 -28.9494123  1
18   35.68157670  14.81230436 -36.31750911  47.51496385 -37.1951638  1
19  -40.50665190 -34.03052327  29.38828175   1.34482600 -34.8719438 -1
20   41.07689315  43.92234944   2.65074784 -11.53928787  32.2066362  1
21   17.66030733 -14.92802922  12.11998039  15.00645671  21.2442750 -1
22  -33.64110310  10.86618125 -39.01640368  40.54307269  39.6113877  1
23   38.51638250  22.13161306 -12.81533381  33.42758568   2.4696089  1
24  -45.89782895  24.45276037  21.59339122   0.08909106 -12.7604788 -1
25  -29.12297575 -19.68773385   2.79124135  31.15364150 -18.4448177  1
26   38.31280286 -30.47305727  26.44094897  -4.80549014  25.7998981 -1
27   16.18606397 -31.02221894 -49.26810868 -25.00526297   6.1112312 -1
28   48.40843189 -21.66230679  -0.12366320   3.17854472   3.8587339  1
29   40.21567111 -48.13758477  24.93614273  28.69090671 -26.4871368  1
30    1.51516791 -26.42521115 -12.80261928  16.00497463 -29.2357545  1
31  -13.19148520  41.22625608 -19.86476800  32.65111602  35.2801044  1
32   20.27577299 -25.47924414  21.95390093  -1.85122907  29.2785393 -1
33    5.61452187  -5.35398889  10.64601119 -44.01104380  17.1113494 -1
34  -44.78640135  -3.47331611  -1.96771566 -12.24545513 -43.1549257  1
35   25.08181627  33.58752318  28.18229569 -11.75732762  19.3758997 -1
36   14.11425164   0.61068691  35.01117583  32.70457520 -39.7158644  1
37   47.13524659   5.07630450  21.24833809 -47.87322641 -16.3222777 -1
38  -30.68369164  44.48609874  17.87844354  38.41511346 -15.1628380  1
39   -9.82401448  47.94880943 -27.94965848  13.46387044   4.4655846  1
40  -16.53469554  13.49646898  27.13568562  48.88138534  13.3870898  1
41    5.66302277  -5.13428820  28.43233454 -38.00809828  34.1122602 -1
42    0.26656424 -30.29644771  -9.31532411   4.33435896  -6.6514673 -1
43  -23.25400757  43.53289178  31.26810214  26.17365187  10.1966180  1
44  -10.83291732  19.52698252 -46.98810957 -26.03460227   3.7786703  1
45   40.12858390  38.52749658  47.63697251 -43.28443427  -2.9563767 -1
46  -21.25995888  11.08579396  17.64257259  49.27916457  14.6470747  1
47  -14.52012877   4.73049677  29.67983205  11.74713625   0.9333332 -1
48   32.01571801  26.66981509  -4.45819930 -21.25811025  13.7524510  1
49  -44.85126981 -28.82791162 -40.01033327  -7.61150781  33.2228162 -1
50   37.97772161 -20.30672238 -43.07964963  30.13702892 -15.0164150  1
51   29.08064781  15.27835699 -37.16766285  15.21206819  -2.2740492  1
52  -40.48949617 -10.51356671  41.26806469 -29.10868146 -34.6600571 -1
53   30.29430094 -46.88648379 -18.59240613 -22.08509911  -5.0840363 -1
54    2.14905366 -12.34249272  15.41978475 -23.87309705  49.7638849 -1
55  -10.86263228  37.23807719 -38.22960216  24.74888896  39.6630126  1
56   40.46251560  -7.42908975   5.45871421  32.46898202  26.3232591  1
57  -49.35276681  48.47594881  -6.54060557  13.13420576 -30.6234515  1
58  -32.55195227 -24.04979744  -0.90411811 -28.68508857 -14.3195416 -1
59  -41.40694165 -32.71186997 -49.91184238 -47.44685916 -30.7997680 -1
60  -40.25409685 -38.63885684  28.98155658  17.04148985 -29.1016112 -1
61    6.55163422  47.65563791   6.50914682 -43.26901732  12.7081363 -1
62  -34.60404077   0.70960103  40.22226434  22.20810365   4.2410096 -1
63  -34.34207600  19.25329904  41.07820110   5.01101012 -11.3040819 -1
64  -26.09685140  26.89760709   0.13924714   6.23782547  30.3378282 -1
65    2.77869620   3.46297752 -16.93740098 -49.87237649  10.3518355 -1
66  -34.43377707 -11.54497443  -0.03308267  18.72712739 -44.7833285  1
67   15.03299868  -7.64621769  49.49413652 -30.31192285  33.8391314 -1
68  -18.59508867 -25.63164262 -22.74283541  34.89008134  21.4274962  1
69   36.03547243  -1.33956145   8.09449207 -15.73835583 -42.1537299  1
70  -12.76628897 -40.29422191   5.18926801   4.31100223  -2.7201807 -1
71   29.70183801 -47.01430625  -0.10015091 -16.11912933  29.4462418 -1
72   19.68328992 -43.62236075  39.62997815 -47.02085708  46.5351474 -1
73   13.60548099 -14.21226230 -16.40881621  -7.07651307  30.7896358 -1
74   48.97985228  28.08398050 -21.35308585  -7.14836787  22.5584449  1
75   40.56481351  44.48666607  39.73892389 -28.47008093 -37.2993243  1
76   38.65095733 -25.91175342 -42.96530839  28.70831673  30.8613458  1
77  -29.94483940  -8.39585150 -28.69421113 -26.08410181 -33.0075214  1
78   -8.54073463  28.14237990  20.12624827   3.81252319 -38.4828388  1
79   48.69534343 -32.77915958   8.73959693  -4.19653771  37.0420157 -1
80  -11.14250922 -41.17423620 -49.40661241 -20.27474060  25.6862722 -1
81  -31.04337221  11.66699962   5.31555978 -49.68183776  16.6878605 -1
82  -46.18124976 -40.82516073 -38.52873622 -43.34530456 -30.9348033 -1
83   29.78327069 -16.52878316  19.70788015  22.03853386  -6.8582143  1
84  -34.10644981 -24.50293470   3.68809036 -24.28773544  38.5350600 -1
85   -0.10980293  33.79236367  30.77957788 -44.64550817 -15.9467584 -1
86   42.79020275 -49.23286503 -27.00992972 -34.40842158  31.3848205 -1
87   41.49881038 -24.64161024 -22.96349106  20.57055852  14.4795177  1
88  -37.18016476  -6.11188293  -9.10155319  -1.89142874 -45.2628603  1
89   -2.62336845   3.08700614 -15.49178469  26.82823380 -32.2975439  1
90   -4.11448206  30.86272427 -46.10658828  44.78534460  46.8005464  1
91  -45.66761896 -32.70136176  24.77845098  34.32040859  39.1300678 -1
92   31.28242453  40.31769738 -20.26872914 -10.96235495 -22.1892530  1
93    3.80620165  46.78701940   4.35569431  45.98498959 -40.7786838  1
94    0.81615241  24.83391126   9.15402786 -19.10655624 -35.2444648  1
95  -41.83242584 -21.35343621  41.83379223  41.67253454  13.9536310 -1
96   47.49085810  -3.14880335 -37.31183154   9.95015414  -0.4881163  1
97   35.54580712  32.30499793  31.31646449 -11.14545153   8.4006399 -1
98   41.21926124  40.27996780  39.20314582  30.17564565 -32.0637980  1
99   35.71568001  -9.81678010  38.08503181 -39.24719978 -25.6364958 -1
100 -26.34892268  48.57874219 -46.59255797  14.88185874 -25.8851135  1
101 -10.52907093 -18.00235135   5.27036686  31.21748420 -34.4811461  1
102 -29.36565380  30.24585389  40.90974440  26.51184569 -17.7345452  1
103   7.87903350  -1.09457127 -15.66868282  -9.03650313   9.9299264 -1
104  42.60331560  23.64356695  25.10587261 -20.22202369  32.7391773 -1
105  34.14003735 -12.71724263  44.14021997 -25.71712218 -31.9221933 -1
106 -12.44853460 -47.72728265  48.57131951   7.67443904  33.7671637 -1
107 -14.09033376  14.33033464 -32.60425429 -38.24150169   6.7293922 -1
108  34.07870485 -46.51382137  49.84296632 -40.89916174 -43.6066909 -1
109  33.01124896 -49.23768658  14.13645993 -44.37299115   4.8207535 -1
110  10.36037307 -33.63043843  21.71480323 -28.38937459  17.0479339 -1
111  -5.79975462 -31.78398826 -11.77024557  -1.45170258 -34.1461023  1
112 -17.11579286 -36.18001689 -20.23241029   5.67694043  42.8745132 -1
113 -38.41568830   3.69573398 -40.29819805  33.50354005  45.8279202  1
114 -29.81968126  -1.95940533 -20.59776224 -36.95698555  29.9862751 -1
115   2.68202946  18.95755879 -24.05982686 -25.90978143   9.7582864  1
116  42.61640511  27.26011581 -46.40585079   4.26192286  29.0983045  1
117   3.67996979  49.15965132  27.85841639 -26.41195238 -43.4398819  1
118  48.34243569   2.41912773   7.61683735 -29.50917231 -19.5587220  1
119  49.62098389  35.17561462   7.30517844  12.31159559  45.7202338  1
120  -0.02593545  19.94858689  17.98142134 -39.22495318   5.3603052 -1
121 -22.92342477  40.02773573 -23.85643139  12.96254296  20.0602848  1
122 -21.57715647 -17.06917104 -29.41318592  36.47456393   2.7248362  1
123  29.80646805 -27.48751035  31.44713799  11.61041714 -44.3767835  1
124 -17.70907494   0.09368618 -49.75451597 -37.48356637  -6.3979823  1
125  28.88035215 -14.24206311  -0.16729170 -42.50496686 -26.9064285 -1
126  35.68704489  -5.52500989  38.28872927  13.51765934 -23.1683312  1
127  -2.63194633   0.26762190 -12.58921986 -28.77510269  31.0379969 -1
128  30.89340176  29.63612082 -19.92898253 -46.91745718  13.4511064 -1
129 -35.64908991  -0.62351443  -1.92955756 -11.38650151 -26.3827307  1
130  15.95162002  12.78902963  34.69599115  21.07257789  -0.7049875  1
131   4.76377490 -44.94423338   1.10263422  46.68685843 -21.0213374  1
132  -7.37550976  14.72977384  -6.50357553  29.58541936  12.0437405  1
133 -35.41488789  15.30446517 -34.03278021   6.36947788   3.5355144  1
134  35.85945931  32.76696808  46.98049189  16.64510823   2.3072411  1
135  16.23364908 -36.40773527  35.59254617  21.82013777  -8.8757714 -1
136  26.42379180   9.73426250  20.27802824 -36.71915454 -14.1613290 -1
137 -36.27642491 -31.46302165 -36.49763539  -9.67619771  15.6194962 -1
138 -47.50820657 -39.16183484 -38.03864804   5.07058196  15.7445797 -1
139 -21.84303377  10.98107803  -0.40645790  41.45426797 -28.7873543  1
140  -0.95827512  -1.69135577  32.42284888 -19.45907820  25.6286883 -1
141 -25.38794791 -18.17971012 -35.69542712 -14.09463407  28.4907040 -1
142  26.73252139  25.03299059 -12.28499415  30.17292826 -10.5327087  1
143 -43.16460560 -29.86155320 -38.54692092 -45.51337271   2.9275517 -1
144   8.24473461  21.64336434 -27.36689942 -27.98296309  21.6617776 -1
145   4.97203793  33.32681707 -38.52586835 -10.99964115 -36.9242265  1
146 -31.53733450  -1.02430454  40.17001574  18.02365675 -48.1134572  1
147 -12.77233372  24.44318922  -2.01369112 -29.32467558 -36.7925263  1
148  -2.59054168   9.29519264 -43.52171838  -4.17696589  17.9192101  1
149  44.38525802  16.66222662  14.42328799  42.99000488  36.5686033  1
150  24.20129897  -6.10695339   3.54532897   5.21023085   8.3394838 -1
151 -25.18552805  32.60349229   2.03504197   6.99553278 -38.9368815  1
152  11.56381064 -21.92995299  15.23446254 -35.43917607 -41.3730330 -1
153 -13.90532325  20.56872472 -42.20650226 -38.18344893  15.1799403 -1
154  -4.53937219 -15.48650358  30.36769438 -48.65573135  -4.2165786 -1
155 -19.33582416  37.94649800  12.92351044  12.55487665 -23.2021390  1
156 -27.08828198 -41.94863634  19.97831776 -31.80869636 -23.1660966 -1
157 -19.96743444  39.69741114 -13.42580225 -48.12533744 -46.1491481  1
158   9.16460447  12.19791272   3.40255061  46.28932201 -13.4358461  1
159  21.10104361 -36.03372062  19.19042345  36.96155972  38.7752557 -1
160 -13.60133037  33.48209793  25.34574629 -39.59111779  26.5550812 -1
161 -13.08967145   9.11082048   2.15857571  -6.17023725  21.4346305 -1
162  11.66161026 -45.03933019 -16.59182839  18.57721794  41.4250354 -1
163  41.77852059  45.44966395  22.19019849 -35.77017984 -16.8267572  1
164 -21.51823819 -45.14922628  14.55832121   0.94149860 -11.4418946 -1
165   7.77473154 -22.52768062  -6.71764023  -2.76005429  21.3051374 -1
166  24.17912751  11.42646645  41.03107578  38.10855299  -4.4167988  1

And what I want to do is to get all of the rows when X6 == -1 (by example). Also, the name and the number of column can change, so I need to find the name of the last column. I did it like that:

simple_learner <- function(df)
{
    lastColName <- colnames(df)[ncol(df)]
    #....
}

So now, I tried to get the value like that => out <- df[df$lastColName == -1, ], however, I get this input...

> simple_learner <- function(df)
+ {
+   lastColName <- colnames(df)[ncol(df)]
+   as.numeric(df$lastColName)
+   out <- df[df$lastColName == -1, ]
+   out
+ }
> 
> simple_learner(read.csv("Exercise-4.csv"))
[1] X1 X2 X3 X4 X5 X6
<0 rows> (or 0-length row.names)

Why?

4
  • 1
    try df[df[, lastColName] == -1, ] Commented Mar 1, 2017 at 2:33
  • 1
    Maybe you can use filter{dplyr}, filter(df, X6==-1) Commented Mar 1, 2017 at 2:44
  • 1
    @VidaWang - the point is that X6 will be different each time, so in your case it will be something like filter_(df, lastColumn == -1) Commented Mar 1, 2017 at 2:51
  • @SymbolixAU I got it, try eval(parse(paste0("filter(df,",lastcolname,"==-1)"))) Commented Mar 1, 2017 at 2:55

3 Answers 3

1

Try

out <- df[df[,ncol(df)] == -1, ]

or

out <- df[df[,lastColName] == -1, ]

in your function.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, I lost so much of time for such a small mistake... thank you a lot, our time can move forward now !
0

Kindly go through the following solution:

Get the last column index using following R code (I am dealing with iris dataset):

 lastcolumn<-max(ncol(iris))
 lastcolumn
  [1] 5

Now, retrieve the rows by the specific value of the last data frame column using following code (I am retrieving the rows with last column having value virginica):

iris[iris[,lastcolumn]=="virginica",]

Hope it works for you.

Comments

0

I had a silimar problem, finally solved it by looking at the help page of dplyr::filter. At the end it gives a nice example:

# To refer to column names that are stored as strings, use the `.data` pronoun:
vars <- c("mass", "height")
cond <- c(80, 150)
starwars %>%
filter(
  .data[[vars[[1]]]] > cond[[1]],
  .data[[vars[[2]]]] > cond[[2]]
)

In your example above, this could lead to:

df %>%
  filter(.data[[colnames(df)[[ncol(df)]]]]== -1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.