Grant, I think,
Your NO SUBJECT message confused me as it seems a continuation of an earlier
discussion of a new and likely irrelevant metric of the worthiness of R
programs.
Did you make a mistake here? I tried your code as well and the results did
not look like what the OP asked for.
It took me a bit to figure out why we needed factors to do what was asked,
and I have concluded we did not but that a particular approach made use of
it as a way to get unsplit to gather the right values together as in:
x <- list(`1` = c(7, 13, 1, 4, 10),
`2` = c(2, 5, 14, 8, 11),
`3` = c(6, 9, 15, 12, 3))
f <- factor(rep(1:3,5))
result <- unsplit(x, f)
By inspection, what the code is doing is treating the three list elements
like rows stacked and then reading down the columns from left to right:
> x
$`1`
[1] 7 13 1 4 10
$`2`
[1] 2 5 14 8 11
$`3`
[1] 6 9 15 12 3
> unsplit(x, f)
[1] 7 2 6 13 5 9 1 14 15 4 8 12 10 11 3
Your code offers, instead:
unlist(x[f])
I have no idea why that would work as it acts by taking indices (not really
factors even if in factor form as it end up being integers from 1 to 3
repeated 5 times) and thus simply repeats all of x five times and then
gathers all the results in a pile of numbers that does not resemble what is
wanted:
> f
[1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
Levels: 1 2 3> x[f]
$`1`
[1] 7 13 1 4 10
$`2`
[1] 2 5 14 8 11
$`3`
[1] 6 9 15 12 3
$`1`
[1] 7 13 1 4 10
<<<LOTS OMITTED
$`3`
[1] 6 9 15 12 3
> unlist(x[f])
11 12 13 14 15 21 22 23 24 25 31 32 33 34 35 11 12 13 14 15 21 22 23 24 25
31 32 33 34 35 11 12 13 14 15
7 13 1 4 10 2 5 14 8 11 6 9 15 12 3 7 13 1 4 10 2 5 14 8 11
6 9 15 12 3 7 13 1 4 10
21 22 23 24 25 31 32 33 34 35 11 12 13 14 15 21 22 23 24 25 31 32 33 34 35
11 12 13 14 15 21 22 23 24 25
2 5 14 8 11 6 9 15 12 3 7 13 1 4 10 2 5 14 8 11 6 9 15 12 3
7 13 1 4 10 2 5 14 8 11
31 32 33 34 35
6 9 15 12 3
I would like to hear if you meant something else or am I doing anything
wrong.
I was thinking of how to expand the request using the technique we now
consider the Rexiest.
If you declare x to be a list of Nrows vectors, all the same length, Mcols,
then a generalized version might be working on something like this, where,
names do not seem needed or even relevant as the various methods use have
tossed them.
x <- list(c(7, 13, 1, 4, 10, 12),
c(2, 5, 14, 8, 11, 13),
c(6, 9, 15, 12, 3, 14),
c( 101, 102, 103, 104, 105, 15),
c(-105, -104, -103, -102, -101, 16))
I added one number at the end of each from above and added two rows.
The number of items in the list is:
Nrows <- length(x)
In the above example, that happens to be five and in the original, 3.
Assuming all the parts are the same length, you can just check one of them
for a length as a vector:
Mcols <- length(x[[1]])
And you get 6 for the new version and 5 for the original.
So, using the same technique, generalized, should work as:
f <- factor(rep(1:Nrows,Mcols))
And the result is:
result <- unsplit(x, f)
Comparing the new test of x versus results shows
> x
[[1]]
[1] 7 13 1 4 10 12
[[2]]
[1] 2 5 14 8 11 13
[[3]]
[1] 6 9 15 12 3 14
[[4]]
[1] 101 102 103 104 105 15
[[5]]
[1] -105 -104 -103 -102 -101 16
> result
[1] 7 2 6 101 -105 13 5 9 102 -104 1 14 15 103
-103 4 8 12 104 -102
[21] 10 11 3 105 -101 12 13 14 15 16
Of course, many of the other techniques we discussed here also scale up to
any size easily, if not as compactly.
-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Izmirlian,
Grant
(NIH/NCI) [E] via R-help
Sent: Tuesday, October 1, 2024 7:54 AM
To: R-help at r-project.org
Subject: [R] (no subject)
I would go with unlist on x,single bracket subsetted on f
x <- list(`1` = c(7, 13, 1, 4, 10),
`2` = c(2, 5, 14, 8, 11),
`3` = c(6, 9, 15, 12, 3))
f <- factor(rep(1:3,5))
unlist(x[f])
Yes, unsplit() it is. I was messing around with ave() (which can be hammered
into submission, sort of).
My overlooking unsplit() is somewhat impressive in view of "svn diff -c
18591"....
-pd
> On 27 Sep 2024, at 17:08 , Martin Maechler <maechler at
stat.math.ethz.ch>
wrote:
>
>>>>>> Chris Evans via R-help
>>>>>> on Fri, 27 Sep 2024 12:20:47 +0200 writes:
>
>> Oh glorious! Thanks Duncan.
>> Fortune cookie nomination!
>
> I don't disagree with the nomination -- thank you, Duncan!
>
> However, please note that I'm sure Rolf's was challenged /
> question was ment to work correctly for all factors `f` with
> levels "1", "2", "3".
>
> Almost all solution were simply assuming that the toy example
> `f` was the real `f`, but that's not realistic.
>
> Consequently, in my view, the only valid proposition and a very
> nice one, indeed, was Deepayan's (well, he's "R core",
...)
>
> unsplit(x, f)
>
> Martin
>
>> On 27/09/2024 11:13, Duncan Murdoch wrote:
>>> On 2024-09-26 11:55 p.m., Rolf Turner wrote:
>>>>
>>>> I have (toy example):
>>>>
>>>> x <- list(`1` = c(7, 13, 1, 4, 10),
>>>> `2` = c(2, 5, 14, 8, 11),
>>>> `3` = c(6, 9, 15, 12, 3))
>>>> and
>>>>
>>>> f <- factor(rep(1:3,5))
>>>>
>>>> I want to create a vector v of length 15 such that the entries
of v,
>>>> corresponding to level l of f are the entries of x[[l]]. I.e.
I want
>>>> v to equal
>>>>
>>>> c(7, 2, 6, 13, 5, 9, 1, 14, 15, 4, 8, 12, 10, 11, 3)
>>>>
>>>> I can create v "easily enough", using say, a
for-loop. It seems to me,
>>>> though, that there should be sexier (single command) way of
achieving
>>>> the desired result. However I cannot devise one.
>>>>
>>>
>>> Don't you find a for loop's naked display of intention to
be sexy?
>>>
>>> Duncan Murdoch
>>>
>> --
>> Chris Evans (he/him)
>> Visiting Professor, UDLA, Quito, Ecuador & Honorary Professor,
>> University of Roehampton, London, UK.
>> CORE site:
http://www.coresystemtrust.org.uk<http://www.coresystemtrust.org.uk/>>> Other work web site: https://www.psyctc.org/psyctc/
>> Personal site: https://www.psyctc.org/pelerinage2016/
>
>> ______________________________________________
>> 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
https://www.R-project.org/posting-guide.html<https://www.r-project.org/posti
ng-guide.html>>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> 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
https://www.R-project.org/posting-guide.html<https://www.r-project.org/posti
ng-guide.html>> 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<http://pd.mes/>@cbs.dk Priv: PDalgd at gmail.com
[[alternative HTML version deleted]]
______________________________________________
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
https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.