[R] How to select one value per row (different columns) from array
waser at frankenfoerder-fg.de
Wed Mar 1 21:23:01 CET 2017
Thank you very much indeed. PD's code is exactly what I was looking for, and unfortunately it is so obvious, that I could kick myself for not thinking of it in the first place, as I feared would happen.
WM: thanks for the additional suggestion. I'll check it in detail to see how my codes can benefit from it.
As a general note, I would like to express my gratitude for all contributors for their very fast and most helpful replies. For me it always made a huge difference to a) spending frustrating hours looking for the solution, knowing it is there but not finding it, and therefore b) having to work around it in a most inelegant manner.
On 1 March 2017 15:38:15 CET, William Michels <wjm1 at caa.columbia.edu> wrote:
>Building on Peter Dalgaard's code, are you just trying to take a sample
>a random column from each row? You don't need to use apply:
>> array[cbind(1:nrow(array), sample.int(ncol(array), nrow(array),
>Just a general note, since you're sampling one-column-per-row from an
>with more rows than columns, you'll have to set replace=TRUE. However,
>there may be other datasets where you have more columns than rows and
>want to sample each column more than once, in which case you would set
>On Wed, Mar 1, 2017 at 5:38 AM, peter dalgaard <pdalgd at gmail.com>
>> On 01 Mar 2017, at 14:28 , Wolfgang Waser
><waser at frankenfoerder-fg.de>
>> > Dear all,
>> > I have to pick one value per row from an array, but from row to row
>> > a different column. The column positions of the values for each row
>> > stored in a vector.
>> > array: 999 rows, 48 columns
>> > vector: 999 values (each between 1 and 48) indicating for each row
>> > value to pick from that row.
>> > Is there a non-loop way to pick the 999 values from the array,
>> > using some form of ?apply?
>> > Thank you very much for help and suggestions!
>> > Wolfgang
>> > ______________________________________________
>> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide http://www.R-project.org/
>> > and provide commented, minimal, self-contained, reproducible code.
>> Peter Dalgaard, Professor,
>> Center for Statistics, Copenhagen Business School
>> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
>> Phone: (+45)38153501
>> Office: A 4.23
>> Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> PLEASE do read the posting guide http://www.R-project.org/
>> and provide commented, minimal, self-contained, reproducible code.
Sent from my touchy-wipy-thing. Please excuse the typos.
[[alternative HTML version deleted]]
More information about the R-help