Reputation: 4939
My data frame looks like this
TFexp
ID Entrez_Gene_ID Symbol adj.P.Val P.Value t B logFC GSM507414
ILMN_1765574 7020 TFAP2A 0.03071446 0.001797766 -5.9919321 -1.012015 -1.02847949 9.537218
ILMN_2374115 7020 TFAP2A 0.03914067 0.002813079 -5.4197845 -1.517871 -0.75604907 6.776104
ILMN_1749838 7593 ZNF42 0.12390434 0.021504872 -3.2861751 -3.807814 -0.48844495 8.586840
ILMN_2181540 7528 YY1 0.21007443 0.054908628 2.4869982 -4.834348 0.31190866 10.404929
ILMN_1733135 7020 TFAP2A 0.22665655 0.063170290 -2.3738597 -4.984464 -0.82238355 3.827819
ILMN_1670816 7593 ZNF42 0.44843565 0.220579380 1.3975294 -6.251177 1.60730795 2.963474
ILMN_1770892 7528 YY1 0.47245442 0.243567365 1.3198784 -6.343075 0.16865034 10.903882
ILMN_2275760 7020 TFAP2A 0.72242186 0.532549376 -0.6695509 -6.978362 -0.52387868 3.392317
ILMN_1676010 6667 SP1 0.75701851 0.581434509 0.5886784 -7.034562 0.07281042 7.846117
ILMN_2282477 7020 TFAP2A 0.95496669 0.907458427 -0.1221984 -7.225401 -0.02493289 6.903279
Within each level for Symbol
I want to select the min, and construct a new data frame from these minimums.
I tried this
ddply(TFexp, .(Symbol), function(x)x[x$P.Value==min(x$P.Value)])
but this returned the following:
ID Entrez_Gene_ID Symbol adj.P.Val P.Value t B logFC GSM507414 GSM507415
1 ILMN_1676010 6667 SP1 0.7570185 0.58143451 0.5886784 -7.034562 0.07281042 7.846117 7.920055
2 ILMN_1765574 <NA> <NA> NA NA -5.9919321 NA NA NA NA
3 ILMN_2374115 <NA> <NA> NA NA -5.4197845 NA NA NA NA
4 ILMN_1733135 <NA> <NA> NA NA -2.3738597 NA NA NA NA
5 ILMN_2275760 <NA> <NA> NA NA -0.6695509 NA NA NA NA
6 ILMN_2282477 <NA> <NA> NA NA -0.1221984 NA NA NA NA
7 ILMN_2181540 <NA> YY1 NA 0.05490863 NA -4.834348 NA 10.404929 NA
8 ILMN_1770892 <NA> YY1 NA 0.24356737 NA -6.343075 NA 10.903882 NA
9 ILMN_1749838 <NA> ZNF42 NA 0.02150487 NA -3.807814 NA 8.586840 NA
10 ILMN_1670816 <NA> ZNF42 NA 0.22057938 NA -6.251177 NA 2.963474 NA
Any ideas?
Cheers,
Davy.
Upvotes: 2
Views: 1846
Reputation: 176638
That's because x[x$P.Value==min(x$P.Value)]
is selecting a column of the data.frame, not a row. Use x[x$P.Value==min(x$P.Value),]
instead. Better yet, use x[which.min(x$P.Value),]
.
Upvotes: 2
Reputation: 58825
(untested)
ddply(TFexp, .(Symbol), function(x)x[which.min(x$P.Value),])
The two changes:
,
after the row selection criteria to make it actually be
a row selection.Upvotes: 3