Re: [R] problem for strsplit function

2021-07-09 Thread Rolf Turner


This discussion has developed in such a way that it seems a better
subject line would be "problem for the hairsplit function". :-)

cheers,

Rolf Turner

-- 
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

__
R-help@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/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] problem for strsplit function

2021-07-09 Thread Jeff Newmiller
A bit too fast there, Duncan... x[[c(1,2)]] is illegal.

On July 9, 2021 5:16:13 PM PDT, Duncan Murdoch  wrote:
>On 09/07/2021 6:44 p.m., Bert Gunter wrote:
>> OK, I stand somewhat chastised.
>> 
>> But my point still is that what you get when you "extract" depends on
>> how you define "extract." Do note that ?"[" yields a help file titled
>> "Extract or Replace Parts of an object"; and afaics, the term
>"subset"
>> is not explicitly used as Duncan prefers.
>
>?"[[" gives you the same page, but I agree:  this part of the 
>documentation isn't written very clearly. The "Introduction to R"
>manual 
>uses the terms I used (see section 2.7, "Index vectors; selecting and 
>modifying subsets of a data set"), as does the source code (and the R 
>Language Definition manual, though it's not as clear as the Intro).
>
>But the point isn't to chastise you, it's to educate you (and the OP). 
>Thinking of [] as subsetting is more helpful than thinking of it as 
>extraction.  That way the result of x[c(1,2)] makes sense.  It's a 
>little bit more of a stretch, but the result of x[[c(1,2)]] also makes 
>sense when you think of it as extraction.
>
>Duncan Murdoch
>
>  The relevant part of the
>> Help file says for "[" for recursive objects says: "Indexing by [ is
>> similar to atomic vectors and selects a list of the specified
>> element(s)."  That a data.frame is a list is explicitly stated, as I
>> noted; that lists are in fact vectors is also explicitly stated
>(?list
>> says: "Almost all lists in R internally are Generic Vectors") but
>then
>> one is stuck with: a data.frame is a list and therefore a vector, but
>> is.vector(d3) is FALSE. The explanation is explicit again in
>> ?is.vector ("is.vector returns TRUE if x is a vector of the specified
>> mode having no attributes other than names. It returns FALSE
>> otherwise."). But I would say these issues are sufficiently murky
>that
>> my warning to be precise is not entirely inappropriate;
>unfortunately,
>> I may have made them more so. Sigh
>> 
>> Cheers,
>> Bert
>> 
>> 
>> 
>> Bert Gunter
>> 
>> "The trouble with having an open mind is that people keep coming
>along
>> and sticking things into it."
>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>> 
>> On Fri, Jul 9, 2021 at 3:05 PM Duncan Murdoch
> wrote:
>>>
>>> On 09/07/2021 5:51 p.m., Jeff Newmiller wrote:
 "Strictly speaking", Greg is correct, Bert.


>https://cran.r-project.org/doc/manuals/r-release/R-lang.html#List-objects

 Lists in R are vectors. What we colloquially refer to as "vectors"
>are more precisely referred to as "atomic vectors". And without a
>doubt, this "vector" nature of lists is a key underlying concept that
>explains why adding a dim attribute creates a matrix that can hold data
>frames. It is also a stumbling block for programmers from other
>languages that have things like linked lists.
>>>
>>> I would also object to v3 (below) as "extracting" a column from d.
>>> "d[2]" doesn't extract anything, it "subsets" the data frame, so the
>>> result is a data frame, not what you get when you extract something
>from
>>> a data frame.
>>>
>>> People don't realize that "x <- 1:10; y <- x[[3]]" is perfectly
>legal.
>>> That extracts the 3rd element (the number 3).  The problem is that R
>has
>>> no way to represent a scalar number, only a vector of numbers, so
>x[[3]]
>>> gets promoted to a vector containing that number when it is returned
>and
>>> assigned to y.
>>>
>>> Lists are vectors of R objects, so if x is a list, x[[3]] is
>something
>>> that can be returned, and it is different from x[3].
>>>
>>> Duncan Murdoch
>>>

 On July 9, 2021 2:36:19 PM PDT, Bert Gunter
> wrote:
> "1.  a column, when extracted from a data frame, *is* a vector."
> Strictly speaking, this is false; it depends on exactly what is
>meant
> by "extracted." e.g.:
>
>> d <- data.frame(col1 = 1:3, col2 = letters[1:3])
>> v1 <- d[,2] ## a vector
>> v2 <- d[[2]] ## the same, i.e
>> identical(v1,v2)
> [1] TRUE
>> v3 <- d[2] ## a data.frame
>> v1
> [1] "a" "b" "c"  ## a character vector
>> v3
>col2
> 1a
> 2b
> 3c
>> is.vector(v1)
> [1] TRUE
>> is.vector(v3)
> [1] FALSE
>> class(v3)  ## data.frame
> [1] "data.frame"
> ## but
>> is.list(v3)
> [1] TRUE
>
> which is simply explained in ?data.frame (where else?!) by:
> "A data frame is a **list** [emphasis added] of variables of the
>same
> number of rows with unique row names, given class "data.frame". If
>no
> variables are included, the row names determine the number of
>rows."
>
> "2.  maybe your question is "is a given function for a vector, or
>for a
>  data frame/matrix/array?".  if so, i think the only way is
>reading
>  the help information (?foo)."
>
> Indeed! Is this not what the Help system is for?! But note also
>that
> the S3 class system may 

Re: [R] problem for strsplit function

2021-07-09 Thread Jeff Newmiller
My mental model for the `[` vs `[[` behavior is that `[` indexes multiple 
results while `[[` indexes only one item. If returning multiple items from a 
list the result must be a list. For consistency, `[` always returns a list when 
applied to a list. The double bracket drops the containing list.

The is.vector() behavior is not intuitive to me... I avoid that function, as I 
think it is more useful to think of lists as vectors than as something "other".

On July 9, 2021 3:44:29 PM PDT, Bert Gunter  wrote:
>OK, I stand somewhat chastised.
>
>But my point still is that what you get when you "extract" depends on
>how you define "extract." Do note that ?"[" yields a help file titled
>"Extract or Replace Parts of an object"; and afaics, the term "subset"
>is not explicitly used as Duncan prefers. The relevant part of the
>Help file says for "[" for recursive objects says: "Indexing by [ is
>similar to atomic vectors and selects a list of the specified
>element(s)."  That a data.frame is a list is explicitly stated, as I
>noted; that lists are in fact vectors is also explicitly stated (?list
>says: "Almost all lists in R internally are Generic Vectors") but then
>one is stuck with: a data.frame is a list and therefore a vector, but
>is.vector(d3) is FALSE. The explanation is explicit again in
>?is.vector ("is.vector returns TRUE if x is a vector of the specified
>mode having no attributes other than names. It returns FALSE
>otherwise."). But I would say these issues are sufficiently murky that
>my warning to be precise is not entirely inappropriate; unfortunately,
>I may have made them more so. Sigh
>
>Cheers,
>Bert
>
>
>
>Bert Gunter
>
>"The trouble with having an open mind is that people keep coming along
>and sticking things into it."
>-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>On Fri, Jul 9, 2021 at 3:05 PM Duncan Murdoch
> wrote:
>>
>> On 09/07/2021 5:51 p.m., Jeff Newmiller wrote:
>> > "Strictly speaking", Greg is correct, Bert.
>> >
>> >
>https://cran.r-project.org/doc/manuals/r-release/R-lang.html#List-objects
>> >
>> > Lists in R are vectors. What we colloquially refer to as "vectors"
>are more precisely referred to as "atomic vectors". And without a
>doubt, this "vector" nature of lists is a key underlying concept that
>explains why adding a dim attribute creates a matrix that can hold data
>frames. It is also a stumbling block for programmers from other
>languages that have things like linked lists.
>>
>> I would also object to v3 (below) as "extracting" a column from d.
>> "d[2]" doesn't extract anything, it "subsets" the data frame, so the
>> result is a data frame, not what you get when you extract something
>from
>> a data frame.
>>
>> People don't realize that "x <- 1:10; y <- x[[3]]" is perfectly
>legal.
>> That extracts the 3rd element (the number 3).  The problem is that R
>has
>> no way to represent a scalar number, only a vector of numbers, so
>x[[3]]
>> gets promoted to a vector containing that number when it is returned
>and
>> assigned to y.
>>
>> Lists are vectors of R objects, so if x is a list, x[[3]] is
>something
>> that can be returned, and it is different from x[3].
>>
>> Duncan Murdoch
>>
>> >
>> > On July 9, 2021 2:36:19 PM PDT, Bert Gunter
> wrote:
>> >> "1.  a column, when extracted from a data frame, *is* a vector."
>> >> Strictly speaking, this is false; it depends on exactly what is
>meant
>> >> by "extracted." e.g.:
>> >>
>> >>> d <- data.frame(col1 = 1:3, col2 = letters[1:3])
>> >>> v1 <- d[,2] ## a vector
>> >>> v2 <- d[[2]] ## the same, i.e
>> >>> identical(v1,v2)
>> >> [1] TRUE
>> >>> v3 <- d[2] ## a data.frame
>> >>> v1
>> >> [1] "a" "b" "c"  ## a character vector
>> >>> v3
>> >>   col2
>> >> 1a
>> >> 2b
>> >> 3c
>> >>> is.vector(v1)
>> >> [1] TRUE
>> >>> is.vector(v3)
>> >> [1] FALSE
>> >>> class(v3)  ## data.frame
>> >> [1] "data.frame"
>> >> ## but
>> >>> is.list(v3)
>> >> [1] TRUE
>> >>
>> >> which is simply explained in ?data.frame (where else?!) by:
>> >> "A data frame is a **list** [emphasis added] of variables of the
>same
>> >> number of rows with unique row names, given class "data.frame". If
>no
>> >> variables are included, the row names determine the number of
>rows."
>> >>
>> >> "2.  maybe your question is "is a given function for a vector, or
>for a
>> >> data frame/matrix/array?".  if so, i think the only way is
>reading
>> >> the help information (?foo)."
>> >>
>> >> Indeed! Is this not what the Help system is for?! But note also
>that
>> >> the S3 class system may somewhat blur the issue: foo() may work
>> >> appropriately and differently for different (S3) classes of
>objects. A
>> >> detailed explanation of this behavior can be found in appropriate
>> >> resources or (more tersely) via ?UseMethod .
>> >>
>> >> "you might find reading ?"[" and  ?"[.data.frame" useful"
>> >>
>> >> Not just 'useful" -- **essential** if you want to work in R,
>unless
>> >> one gets this information via any of the 

Re: [R] problem for strsplit function

2021-07-09 Thread Duncan Murdoch

On 09/07/2021 6:44 p.m., Bert Gunter wrote:

OK, I stand somewhat chastised.

But my point still is that what you get when you "extract" depends on
how you define "extract." Do note that ?"[" yields a help file titled
"Extract or Replace Parts of an object"; and afaics, the term "subset"
is not explicitly used as Duncan prefers.


?"[[" gives you the same page, but I agree:  this part of the 
documentation isn't written very clearly. The "Introduction to R" manual 
uses the terms I used (see section 2.7, "Index vectors; selecting and 
modifying subsets of a data set"), as does the source code (and the R 
Language Definition manual, though it's not as clear as the Intro).


But the point isn't to chastise you, it's to educate you (and the OP). 
Thinking of [] as subsetting is more helpful than thinking of it as 
extraction.  That way the result of x[c(1,2)] makes sense.  It's a 
little bit more of a stretch, but the result of x[[c(1,2)]] also makes 
sense when you think of it as extraction.


Duncan Murdoch

 The relevant part of the

Help file says for "[" for recursive objects says: "Indexing by [ is
similar to atomic vectors and selects a list of the specified
element(s)."  That a data.frame is a list is explicitly stated, as I
noted; that lists are in fact vectors is also explicitly stated (?list
says: "Almost all lists in R internally are Generic Vectors") but then
one is stuck with: a data.frame is a list and therefore a vector, but
is.vector(d3) is FALSE. The explanation is explicit again in
?is.vector ("is.vector returns TRUE if x is a vector of the specified
mode having no attributes other than names. It returns FALSE
otherwise."). But I would say these issues are sufficiently murky that
my warning to be precise is not entirely inappropriate; unfortunately,
I may have made them more so. Sigh

Cheers,
Bert



Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Fri, Jul 9, 2021 at 3:05 PM Duncan Murdoch  wrote:


On 09/07/2021 5:51 p.m., Jeff Newmiller wrote:

"Strictly speaking", Greg is correct, Bert.

https://cran.r-project.org/doc/manuals/r-release/R-lang.html#List-objects

Lists in R are vectors. What we colloquially refer to as "vectors" are more precisely referred to 
as "atomic vectors". And without a doubt, this "vector" nature of lists is a key 
underlying concept that explains why adding a dim attribute creates a matrix that can hold data frames. It is 
also a stumbling block for programmers from other languages that have things like linked lists.


I would also object to v3 (below) as "extracting" a column from d.
"d[2]" doesn't extract anything, it "subsets" the data frame, so the
result is a data frame, not what you get when you extract something from
a data frame.

People don't realize that "x <- 1:10; y <- x[[3]]" is perfectly legal.
That extracts the 3rd element (the number 3).  The problem is that R has
no way to represent a scalar number, only a vector of numbers, so x[[3]]
gets promoted to a vector containing that number when it is returned and
assigned to y.

Lists are vectors of R objects, so if x is a list, x[[3]] is something
that can be returned, and it is different from x[3].

Duncan Murdoch



On July 9, 2021 2:36:19 PM PDT, Bert Gunter  wrote:

"1.  a column, when extracted from a data frame, *is* a vector."
Strictly speaking, this is false; it depends on exactly what is meant
by "extracted." e.g.:


d <- data.frame(col1 = 1:3, col2 = letters[1:3])
v1 <- d[,2] ## a vector
v2 <- d[[2]] ## the same, i.e
identical(v1,v2)

[1] TRUE

v3 <- d[2] ## a data.frame
v1

[1] "a" "b" "c"  ## a character vector

v3

   col2
1a
2b
3c

is.vector(v1)

[1] TRUE

is.vector(v3)

[1] FALSE

class(v3)  ## data.frame

[1] "data.frame"
## but

is.list(v3)

[1] TRUE

which is simply explained in ?data.frame (where else?!) by:
"A data frame is a **list** [emphasis added] of variables of the same
number of rows with unique row names, given class "data.frame". If no
variables are included, the row names determine the number of rows."

"2.  maybe your question is "is a given function for a vector, or for a
 data frame/matrix/array?".  if so, i think the only way is reading
 the help information (?foo)."

Indeed! Is this not what the Help system is for?! But note also that
the S3 class system may somewhat blur the issue: foo() may work
appropriately and differently for different (S3) classes of objects. A
detailed explanation of this behavior can be found in appropriate
resources or (more tersely) via ?UseMethod .

"you might find reading ?"[" and  ?"[.data.frame" useful"

Not just 'useful" -- **essential** if you want to work in R, unless
one gets this information via any of the numerous online tutorials,
courses, or books that are available. The Help system is accurate and
authoritative, but terse. I happen to like this mode of documentation,

Re: [R] problem for strsplit function

2021-07-09 Thread Bert Gunter
"But it takes me a while to get familiar R."

Of course. That is true for all of us. Just keep on plugging away and
you'll get it. Probably far better than I before too long.

Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Fri, Jul 9, 2021 at 3:45 PM Kai Yang  wrote:
>
> Thanks Bert,
>
> I'm reading some books now. But it takes me a while to get familiar R.
>
>
> Best,
>
> Kai
> On Friday, July 9, 2021, 03:06:11 PM PDT, Duncan Murdoch 
>  wrote:
>
>
> On 09/07/2021 5:51 p.m., Jeff Newmiller wrote:
> > "Strictly speaking", Greg is correct, Bert.
> >
> > https://cran.r-project.org/doc/manuals/r-release/R-lang.html#List-objects
> >
> > Lists in R are vectors. What we colloquially refer to as "vectors" are more 
> > precisely referred to as "atomic vectors". And without a doubt, this 
> > "vector" nature of lists is a key underlying concept that explains why 
> > adding a dim attribute creates a matrix that can hold data frames. It is 
> > also a stumbling block for programmers from other languages that have 
> > things like linked lists.
>
> I would also object to v3 (below) as "extracting" a column from d.
> "d[2]" doesn't extract anything, it "subsets" the data frame, so the
> result is a data frame, not what you get when you extract something from
> a data frame.
>
> People don't realize that "x <- 1:10; y <- x[[3]]" is perfectly legal.
> That extracts the 3rd element (the number 3).  The problem is that R has
> no way to represent a scalar number, only a vector of numbers, so x[[3]]
> gets promoted to a vector containing that number when it is returned and
> assigned to y.
>
> Lists are vectors of R objects, so if x is a list, x[[3]] is something
> that can be returned, and it is different from x[3].
>
> Duncan Murdoch
>
>
> >
> > On July 9, 2021 2:36:19 PM PDT, Bert Gunter  wrote:
> >> "1.  a column, when extracted from a data frame, *is* a vector."
> >> Strictly speaking, this is false; it depends on exactly what is meant
> >> by "extracted." e.g.:
> >>
> >>> d <- data.frame(col1 = 1:3, col2 = letters[1:3])
> >>> v1 <- d[,2] ## a vector
> >>> v2 <- d[[2]] ## the same, i.e
> >>> identical(v1,v2)
> >> [1] TRUE
> >>> v3 <- d[2] ## a data.frame
> >>> v1
> >> [1] "a" "b" "c"  ## a character vector
> >>> v3
> >>  col2
> >> 1a
> >> 2b
> >> 3c
> >>> is.vector(v1)
> >> [1] TRUE
> >>> is.vector(v3)
> >> [1] FALSE
> >>> class(v3)  ## data.frame
> >> [1] "data.frame"
> >> ## but
> >>> is.list(v3)
> >> [1] TRUE
> >>
> >> which is simply explained in ?data.frame (where else?!) by:
> >> "A data frame is a **list** [emphasis added] of variables of the same
> >> number of rows with unique row names, given class "data.frame". If no
> >> variables are included, the row names determine the number of rows."
> >>
> >> "2.  maybe your question is "is a given function for a vector, or for a
> >>data frame/matrix/array?".  if so, i think the only way is reading
> >>the help information (?foo)."
> >>
> >> Indeed! Is this not what the Help system is for?! But note also that
> >> the S3 class system may somewhat blur the issue: foo() may work
> >> appropriately and differently for different (S3) classes of objects. A
> >> detailed explanation of this behavior can be found in appropriate
> >> resources or (more tersely) via ?UseMethod .
> >>
> >> "you might find reading ?"[" and  ?"[.data.frame" useful"
> >>
> >> Not just 'useful" -- **essential** if you want to work in R, unless
> >> one gets this information via any of the numerous online tutorials,
> >> courses, or books that are available. The Help system is accurate and
> >> authoritative, but terse. I happen to like this mode of documentation,
> >> but others may prefer more extended expositions. I stand by this claim
> >> even if one chooses to use the "Tidyverse", data.table package, or
> >> other alternative frameworks for handling data. Again, others may
> >> disagree, but R is structured around these basics, and imo one remains
> >> ignorant of them at their peril.
> >>
> >> Cheers,
> >> Bert
> >>
> >>
> >> Bert Gunter
> >>
> >> "The trouble with having an open mind is that people keep coming along
> >> and sticking things into it."
> >> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> >>
> >> On Fri, Jul 9, 2021 at 11:57 AM Greg Minshall 
> >> wrote:
> >>>
> >>> Kai,
> >>>
>  one more question, how can I know if the function is for column
>  manipulations or for vector?
> >>>
> >>> i still stumble around R code.  but, i'd say the following (and look
> >>> forward to being corrected! :):
> >>>
> >>> 1.  a column, when extracted from a data frame, *is* a vector.
> >>>
> >>> 2.  maybe your question is "is a given function for a vector, or for
> >> a
> >>>  data frame/matrix/array?".  if so, i think the only way is
> >> reading
> >>>  the help information (?foo).
> >>>
> >>> 3.  

Re: [R] problem for strsplit function

2021-07-09 Thread Kai Yang via R-help
 Thanks Bert,
I'm reading some books now. But it takes me a while to get familiar R.

Best,
KaiOn Friday, July 9, 2021, 03:06:11 PM PDT, Duncan Murdoch 
 wrote:  
 
 On 09/07/2021 5:51 p.m., Jeff Newmiller wrote:
> "Strictly speaking", Greg is correct, Bert.
> 
> https://cran.r-project.org/doc/manuals/r-release/R-lang.html#List-objects
> 
> Lists in R are vectors. What we colloquially refer to as "vectors" are more 
> precisely referred to as "atomic vectors". And without a doubt, this "vector" 
> nature of lists is a key underlying concept that explains why adding a dim 
> attribute creates a matrix that can hold data frames. It is also a stumbling 
> block for programmers from other languages that have things like linked lists.

I would also object to v3 (below) as "extracting" a column from d. 
"d[2]" doesn't extract anything, it "subsets" the data frame, so the 
result is a data frame, not what you get when you extract something from 
a data frame.

People don't realize that "x <- 1:10; y <- x[[3]]" is perfectly legal. 
That extracts the 3rd element (the number 3).  The problem is that R has 
no way to represent a scalar number, only a vector of numbers, so x[[3]] 
gets promoted to a vector containing that number when it is returned and 
assigned to y.

Lists are vectors of R objects, so if x is a list, x[[3]] is something 
that can be returned, and it is different from x[3].

Duncan Murdoch

> 
> On July 9, 2021 2:36:19 PM PDT, Bert Gunter  wrote:
>> "1.  a column, when extracted from a data frame, *is* a vector."
>> Strictly speaking, this is false; it depends on exactly what is meant
>> by "extracted." e.g.:
>>
>>> d <- data.frame(col1 = 1:3, col2 = letters[1:3])
>>> v1 <- d[,2] ## a vector
>>> v2 <- d[[2]] ## the same, i.e
>>> identical(v1,v2)
>> [1] TRUE
>>> v3 <- d[2] ## a data.frame
>>> v1
>> [1] "a" "b" "c"  ## a character vector
>>> v3
>>  col2
>> 1    a
>> 2    b
>> 3    c
>>> is.vector(v1)
>> [1] TRUE
>>> is.vector(v3)
>> [1] FALSE
>>> class(v3)  ## data.frame
>> [1] "data.frame"
>> ## but
>>> is.list(v3)
>> [1] TRUE
>>
>> which is simply explained in ?data.frame (where else?!) by:
>> "A data frame is a **list** [emphasis added] of variables of the same
>> number of rows with unique row names, given class "data.frame". If no
>> variables are included, the row names determine the number of rows."
>>
>> "2.  maybe your question is "is a given function for a vector, or for a
>>    data frame/matrix/array?".  if so, i think the only way is reading
>>    the help information (?foo)."
>>
>> Indeed! Is this not what the Help system is for?! But note also that
>> the S3 class system may somewhat blur the issue: foo() may work
>> appropriately and differently for different (S3) classes of objects. A
>> detailed explanation of this behavior can be found in appropriate
>> resources or (more tersely) via ?UseMethod .
>>
>> "you might find reading ?"[" and  ?"[.data.frame" useful"
>>
>> Not just 'useful" -- **essential** if you want to work in R, unless
>> one gets this information via any of the numerous online tutorials,
>> courses, or books that are available. The Help system is accurate and
>> authoritative, but terse. I happen to like this mode of documentation,
>> but others may prefer more extended expositions. I stand by this claim
>> even if one chooses to use the "Tidyverse", data.table package, or
>> other alternative frameworks for handling data. Again, others may
>> disagree, but R is structured around these basics, and imo one remains
>> ignorant of them at their peril.
>>
>> Cheers,
>> Bert
>>
>>
>> Bert Gunter
>>
>> "The trouble with having an open mind is that people keep coming along
>> and sticking things into it."
>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>>
>> On Fri, Jul 9, 2021 at 11:57 AM Greg Minshall 
>> wrote:
>>>
>>> Kai,
>>>
 one more question, how can I know if the function is for column
 manipulations or for vector?
>>>
>>> i still stumble around R code.  but, i'd say the following (and look
>>> forward to being corrected! :):
>>>
>>> 1.  a column, when extracted from a data frame, *is* a vector.
>>>
>>> 2.  maybe your question is "is a given function for a vector, or for
>> a
>>>      data frame/matrix/array?".  if so, i think the only way is
>> reading
>>>      the help information (?foo).
>>>
>>> 3.  sometimes, extracting the column as a vector from a data
>> frame-like
>>>      object might be non-intuitive.  you might find reading ?"[" and
>>>      ?"[.data.frame" useful (as well as ?"[.data.table" if you use
>> that
>>>      package).  also, the str() command can be helpful in
>> understanding
>>>      what is happening.  (the lobstr:: package's sxp() function, as
>> well
>>>      as more verbose .Internal(inspect()) can also give you insight.)
>>>
>>>      with the data.table:: package, for example, if "DT" is a
>> data.table
>>>      object, with "x2" as a column, adding or leaving off quotation
>> marks
>>>      for the 

Re: [R] problem for strsplit function

2021-07-09 Thread Bert Gunter
OK, I stand somewhat chastised.

But my point still is that what you get when you "extract" depends on
how you define "extract." Do note that ?"[" yields a help file titled
"Extract or Replace Parts of an object"; and afaics, the term "subset"
is not explicitly used as Duncan prefers. The relevant part of the
Help file says for "[" for recursive objects says: "Indexing by [ is
similar to atomic vectors and selects a list of the specified
element(s)."  That a data.frame is a list is explicitly stated, as I
noted; that lists are in fact vectors is also explicitly stated (?list
says: "Almost all lists in R internally are Generic Vectors") but then
one is stuck with: a data.frame is a list and therefore a vector, but
is.vector(d3) is FALSE. The explanation is explicit again in
?is.vector ("is.vector returns TRUE if x is a vector of the specified
mode having no attributes other than names. It returns FALSE
otherwise."). But I would say these issues are sufficiently murky that
my warning to be precise is not entirely inappropriate; unfortunately,
I may have made them more so. Sigh

Cheers,
Bert



Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Fri, Jul 9, 2021 at 3:05 PM Duncan Murdoch  wrote:
>
> On 09/07/2021 5:51 p.m., Jeff Newmiller wrote:
> > "Strictly speaking", Greg is correct, Bert.
> >
> > https://cran.r-project.org/doc/manuals/r-release/R-lang.html#List-objects
> >
> > Lists in R are vectors. What we colloquially refer to as "vectors" are more 
> > precisely referred to as "atomic vectors". And without a doubt, this 
> > "vector" nature of lists is a key underlying concept that explains why 
> > adding a dim attribute creates a matrix that can hold data frames. It is 
> > also a stumbling block for programmers from other languages that have 
> > things like linked lists.
>
> I would also object to v3 (below) as "extracting" a column from d.
> "d[2]" doesn't extract anything, it "subsets" the data frame, so the
> result is a data frame, not what you get when you extract something from
> a data frame.
>
> People don't realize that "x <- 1:10; y <- x[[3]]" is perfectly legal.
> That extracts the 3rd element (the number 3).  The problem is that R has
> no way to represent a scalar number, only a vector of numbers, so x[[3]]
> gets promoted to a vector containing that number when it is returned and
> assigned to y.
>
> Lists are vectors of R objects, so if x is a list, x[[3]] is something
> that can be returned, and it is different from x[3].
>
> Duncan Murdoch
>
> >
> > On July 9, 2021 2:36:19 PM PDT, Bert Gunter  wrote:
> >> "1.  a column, when extracted from a data frame, *is* a vector."
> >> Strictly speaking, this is false; it depends on exactly what is meant
> >> by "extracted." e.g.:
> >>
> >>> d <- data.frame(col1 = 1:3, col2 = letters[1:3])
> >>> v1 <- d[,2] ## a vector
> >>> v2 <- d[[2]] ## the same, i.e
> >>> identical(v1,v2)
> >> [1] TRUE
> >>> v3 <- d[2] ## a data.frame
> >>> v1
> >> [1] "a" "b" "c"  ## a character vector
> >>> v3
> >>   col2
> >> 1a
> >> 2b
> >> 3c
> >>> is.vector(v1)
> >> [1] TRUE
> >>> is.vector(v3)
> >> [1] FALSE
> >>> class(v3)  ## data.frame
> >> [1] "data.frame"
> >> ## but
> >>> is.list(v3)
> >> [1] TRUE
> >>
> >> which is simply explained in ?data.frame (where else?!) by:
> >> "A data frame is a **list** [emphasis added] of variables of the same
> >> number of rows with unique row names, given class "data.frame". If no
> >> variables are included, the row names determine the number of rows."
> >>
> >> "2.  maybe your question is "is a given function for a vector, or for a
> >> data frame/matrix/array?".  if so, i think the only way is reading
> >> the help information (?foo)."
> >>
> >> Indeed! Is this not what the Help system is for?! But note also that
> >> the S3 class system may somewhat blur the issue: foo() may work
> >> appropriately and differently for different (S3) classes of objects. A
> >> detailed explanation of this behavior can be found in appropriate
> >> resources or (more tersely) via ?UseMethod .
> >>
> >> "you might find reading ?"[" and  ?"[.data.frame" useful"
> >>
> >> Not just 'useful" -- **essential** if you want to work in R, unless
> >> one gets this information via any of the numerous online tutorials,
> >> courses, or books that are available. The Help system is accurate and
> >> authoritative, but terse. I happen to like this mode of documentation,
> >> but others may prefer more extended expositions. I stand by this claim
> >> even if one chooses to use the "Tidyverse", data.table package, or
> >> other alternative frameworks for handling data. Again, others may
> >> disagree, but R is structured around these basics, and imo one remains
> >> ignorant of them at their peril.
> >>
> >> Cheers,
> >> Bert
> >>
> >>
> >> Bert Gunter
> >>
> >> "The trouble with having an open mind is 

Re: [R] problem for strsplit function

2021-07-09 Thread Duncan Murdoch

On 09/07/2021 5:51 p.m., Jeff Newmiller wrote:

"Strictly speaking", Greg is correct, Bert.

https://cran.r-project.org/doc/manuals/r-release/R-lang.html#List-objects

Lists in R are vectors. What we colloquially refer to as "vectors" are more precisely referred to 
as "atomic vectors". And without a doubt, this "vector" nature of lists is a key 
underlying concept that explains why adding a dim attribute creates a matrix that can hold data frames. It is 
also a stumbling block for programmers from other languages that have things like linked lists.


I would also object to v3 (below) as "extracting" a column from d. 
"d[2]" doesn't extract anything, it "subsets" the data frame, so the 
result is a data frame, not what you get when you extract something from 
a data frame.


People don't realize that "x <- 1:10; y <- x[[3]]" is perfectly legal. 
That extracts the 3rd element (the number 3).  The problem is that R has 
no way to represent a scalar number, only a vector of numbers, so x[[3]] 
gets promoted to a vector containing that number when it is returned and 
assigned to y.


Lists are vectors of R objects, so if x is a list, x[[3]] is something 
that can be returned, and it is different from x[3].


Duncan Murdoch



On July 9, 2021 2:36:19 PM PDT, Bert Gunter  wrote:

"1.  a column, when extracted from a data frame, *is* a vector."
Strictly speaking, this is false; it depends on exactly what is meant
by "extracted." e.g.:


d <- data.frame(col1 = 1:3, col2 = letters[1:3])
v1 <- d[,2] ## a vector
v2 <- d[[2]] ## the same, i.e
identical(v1,v2)

[1] TRUE

v3 <- d[2] ## a data.frame
v1

[1] "a" "b" "c"  ## a character vector

v3

  col2
1a
2b
3c

is.vector(v1)

[1] TRUE

is.vector(v3)

[1] FALSE

class(v3)  ## data.frame

[1] "data.frame"
## but

is.list(v3)

[1] TRUE

which is simply explained in ?data.frame (where else?!) by:
"A data frame is a **list** [emphasis added] of variables of the same
number of rows with unique row names, given class "data.frame". If no
variables are included, the row names determine the number of rows."

"2.  maybe your question is "is a given function for a vector, or for a
data frame/matrix/array?".  if so, i think the only way is reading
the help information (?foo)."

Indeed! Is this not what the Help system is for?! But note also that
the S3 class system may somewhat blur the issue: foo() may work
appropriately and differently for different (S3) classes of objects. A
detailed explanation of this behavior can be found in appropriate
resources or (more tersely) via ?UseMethod .

"you might find reading ?"[" and  ?"[.data.frame" useful"

Not just 'useful" -- **essential** if you want to work in R, unless
one gets this information via any of the numerous online tutorials,
courses, or books that are available. The Help system is accurate and
authoritative, but terse. I happen to like this mode of documentation,
but others may prefer more extended expositions. I stand by this claim
even if one chooses to use the "Tidyverse", data.table package, or
other alternative frameworks for handling data. Again, others may
disagree, but R is structured around these basics, and imo one remains
ignorant of them at their peril.

Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Fri, Jul 9, 2021 at 11:57 AM Greg Minshall 
wrote:


Kai,


one more question, how can I know if the function is for column
manipulations or for vector?


i still stumble around R code.  but, i'd say the following (and look
forward to being corrected! :):

1.  a column, when extracted from a data frame, *is* a vector.

2.  maybe your question is "is a given function for a vector, or for

a

 data frame/matrix/array?".  if so, i think the only way is

reading

 the help information (?foo).

3.  sometimes, extracting the column as a vector from a data

frame-like

 object might be non-intuitive.  you might find reading ?"[" and
 ?"[.data.frame" useful (as well as ?"[.data.table" if you use

that

 package).  also, the str() command can be helpful in

understanding

 what is happening.  (the lobstr:: package's sxp() function, as

well

 as more verbose .Internal(inspect()) can also give you insight.)

 with the data.table:: package, for example, if "DT" is a

data.table

 object, with "x2" as a column, adding or leaving off quotation

marks

 for the column name can make all the difference between ending up
 with a vector, or with a (much reduced) data table:


is.vector(DT[, x2])

[1] TRUE

str(DT[, x2])

  num [1:9] 32 32 32 32 32 32 32 32 32


is.vector(DT[, "x2"])

[1] FALSE

str(DT[, "x2"])

Classes ‘data.table’ and 'data.frame':  9 obs. of  1 variable:
  $ x2: num  32 32 32 32 32 32 32 32 32
  - attr(*, ".internal.selfref")=


 a second level of indexing may or may not help, mostly 

Re: [R] problem for strsplit function

2021-07-09 Thread Jeff Newmiller
"Strictly speaking", Greg is correct, Bert.

https://cran.r-project.org/doc/manuals/r-release/R-lang.html#List-objects

Lists in R are vectors. What we colloquially refer to as "vectors" are more 
precisely referred to as "atomic vectors". And without a doubt, this "vector" 
nature of lists is a key underlying concept that explains why adding a dim 
attribute creates a matrix that can hold data frames. It is also a stumbling 
block for programmers from other languages that have things like linked lists.

On July 9, 2021 2:36:19 PM PDT, Bert Gunter  wrote:
>"1.  a column, when extracted from a data frame, *is* a vector."
>Strictly speaking, this is false; it depends on exactly what is meant
>by "extracted." e.g.:
>
>> d <- data.frame(col1 = 1:3, col2 = letters[1:3])
>> v1 <- d[,2] ## a vector
>> v2 <- d[[2]] ## the same, i.e
>> identical(v1,v2)
>[1] TRUE
>> v3 <- d[2] ## a data.frame
>> v1
>[1] "a" "b" "c"  ## a character vector
>> v3
>  col2
>1a
>2b
>3c
>> is.vector(v1)
>[1] TRUE
>> is.vector(v3)
>[1] FALSE
>> class(v3)  ## data.frame
>[1] "data.frame"
>## but
>> is.list(v3)
>[1] TRUE
>
>which is simply explained in ?data.frame (where else?!) by:
>"A data frame is a **list** [emphasis added] of variables of the same
>number of rows with unique row names, given class "data.frame". If no
>variables are included, the row names determine the number of rows."
>
>"2.  maybe your question is "is a given function for a vector, or for a
>data frame/matrix/array?".  if so, i think the only way is reading
>the help information (?foo)."
>
>Indeed! Is this not what the Help system is for?! But note also that
>the S3 class system may somewhat blur the issue: foo() may work
>appropriately and differently for different (S3) classes of objects. A
>detailed explanation of this behavior can be found in appropriate
>resources or (more tersely) via ?UseMethod .
>
>"you might find reading ?"[" and  ?"[.data.frame" useful"
>
>Not just 'useful" -- **essential** if you want to work in R, unless
>one gets this information via any of the numerous online tutorials,
>courses, or books that are available. The Help system is accurate and
>authoritative, but terse. I happen to like this mode of documentation,
>but others may prefer more extended expositions. I stand by this claim
>even if one chooses to use the "Tidyverse", data.table package, or
>other alternative frameworks for handling data. Again, others may
>disagree, but R is structured around these basics, and imo one remains
>ignorant of them at their peril.
>
>Cheers,
>Bert
>
>
>Bert Gunter
>
>"The trouble with having an open mind is that people keep coming along
>and sticking things into it."
>-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>On Fri, Jul 9, 2021 at 11:57 AM Greg Minshall 
>wrote:
>>
>> Kai,
>>
>> > one more question, how can I know if the function is for column
>> > manipulations or for vector?
>>
>> i still stumble around R code.  but, i'd say the following (and look
>> forward to being corrected! :):
>>
>> 1.  a column, when extracted from a data frame, *is* a vector.
>>
>> 2.  maybe your question is "is a given function for a vector, or for
>a
>> data frame/matrix/array?".  if so, i think the only way is
>reading
>> the help information (?foo).
>>
>> 3.  sometimes, extracting the column as a vector from a data
>frame-like
>> object might be non-intuitive.  you might find reading ?"[" and
>> ?"[.data.frame" useful (as well as ?"[.data.table" if you use
>that
>> package).  also, the str() command can be helpful in
>understanding
>> what is happening.  (the lobstr:: package's sxp() function, as
>well
>> as more verbose .Internal(inspect()) can also give you insight.)
>>
>> with the data.table:: package, for example, if "DT" is a
>data.table
>> object, with "x2" as a column, adding or leaving off quotation
>marks
>> for the column name can make all the difference between ending up
>> with a vector, or with a (much reduced) data table:
>> 
>> > is.vector(DT[, x2])
>> [1] TRUE
>> > str(DT[, x2])
>>  num [1:9] 32 32 32 32 32 32 32 32 32
>> >
>> > is.vector(DT[, "x2"])
>> [1] FALSE
>> > str(DT[, "x2"])
>> Classes ‘data.table’ and 'data.frame':  9 obs. of  1 variable:
>>  $ x2: num  32 32 32 32 32 32 32 32 32
>>  - attr(*, ".internal.selfref")=
>> 
>>
>> a second level of indexing may or may not help, mostly depending
>on
>> the use of '[' versus of '[['.  this can sometimes cause
>confusion
>> when you are learning the language.
>> 
>> > str(DT[, "x2"][1])
>> Classes ‘data.table’ and 'data.frame':  1 obs. of  1 variable:
>>  $ x2: num 32
>>  - attr(*, ".internal.selfref")=
>> > str(DT[, "x2"][[1]])
>>  num [1:9] 32 32 32 32 32 32 32 32 32
>> 
>>
>> the tibble:: package (used in, e.g., the dplyr:: package) also
>> (always?) returns a single column as a non-vector.  again, a
>> second indexing with double '[[]]' can produce a vector.
>> 
>> 

Re: [R] problem for strsplit function

2021-07-09 Thread Bert Gunter
"1.  a column, when extracted from a data frame, *is* a vector."
Strictly speaking, this is false; it depends on exactly what is meant
by "extracted." e.g.:

> d <- data.frame(col1 = 1:3, col2 = letters[1:3])
> v1 <- d[,2] ## a vector
> v2 <- d[[2]] ## the same, i.e
> identical(v1,v2)
[1] TRUE
> v3 <- d[2] ## a data.frame
> v1
[1] "a" "b" "c"  ## a character vector
> v3
  col2
1a
2b
3c
> is.vector(v1)
[1] TRUE
> is.vector(v3)
[1] FALSE
> class(v3)  ## data.frame
[1] "data.frame"
## but
> is.list(v3)
[1] TRUE

which is simply explained in ?data.frame (where else?!) by:
"A data frame is a **list** [emphasis added] of variables of the same
number of rows with unique row names, given class "data.frame". If no
variables are included, the row names determine the number of rows."

"2.  maybe your question is "is a given function for a vector, or for a
data frame/matrix/array?".  if so, i think the only way is reading
the help information (?foo)."

Indeed! Is this not what the Help system is for?! But note also that
the S3 class system may somewhat blur the issue: foo() may work
appropriately and differently for different (S3) classes of objects. A
detailed explanation of this behavior can be found in appropriate
resources or (more tersely) via ?UseMethod .

"you might find reading ?"[" and  ?"[.data.frame" useful"

Not just 'useful" -- **essential** if you want to work in R, unless
one gets this information via any of the numerous online tutorials,
courses, or books that are available. The Help system is accurate and
authoritative, but terse. I happen to like this mode of documentation,
but others may prefer more extended expositions. I stand by this claim
even if one chooses to use the "Tidyverse", data.table package, or
other alternative frameworks for handling data. Again, others may
disagree, but R is structured around these basics, and imo one remains
ignorant of them at their peril.

Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Fri, Jul 9, 2021 at 11:57 AM Greg Minshall  wrote:
>
> Kai,
>
> > one more question, how can I know if the function is for column
> > manipulations or for vector?
>
> i still stumble around R code.  but, i'd say the following (and look
> forward to being corrected! :):
>
> 1.  a column, when extracted from a data frame, *is* a vector.
>
> 2.  maybe your question is "is a given function for a vector, or for a
> data frame/matrix/array?".  if so, i think the only way is reading
> the help information (?foo).
>
> 3.  sometimes, extracting the column as a vector from a data frame-like
> object might be non-intuitive.  you might find reading ?"[" and
> ?"[.data.frame" useful (as well as ?"[.data.table" if you use that
> package).  also, the str() command can be helpful in understanding
> what is happening.  (the lobstr:: package's sxp() function, as well
> as more verbose .Internal(inspect()) can also give you insight.)
>
> with the data.table:: package, for example, if "DT" is a data.table
> object, with "x2" as a column, adding or leaving off quotation marks
> for the column name can make all the difference between ending up
> with a vector, or with a (much reduced) data table:
> 
> > is.vector(DT[, x2])
> [1] TRUE
> > str(DT[, x2])
>  num [1:9] 32 32 32 32 32 32 32 32 32
> >
> > is.vector(DT[, "x2"])
> [1] FALSE
> > str(DT[, "x2"])
> Classes ‘data.table’ and 'data.frame':  9 obs. of  1 variable:
>  $ x2: num  32 32 32 32 32 32 32 32 32
>  - attr(*, ".internal.selfref")=
> 
>
> a second level of indexing may or may not help, mostly depending on
> the use of '[' versus of '[['.  this can sometimes cause confusion
> when you are learning the language.
> 
> > str(DT[, "x2"][1])
> Classes ‘data.table’ and 'data.frame':  1 obs. of  1 variable:
>  $ x2: num 32
>  - attr(*, ".internal.selfref")=
> > str(DT[, "x2"][[1]])
>  num [1:9] 32 32 32 32 32 32 32 32 32
> 
>
> the tibble:: package (used in, e.g., the dplyr:: package) also
> (always?) returns a single column as a non-vector.  again, a
> second indexing with double '[[]]' can produce a vector.
> 
> > DP <- tibble(DT)
> > is.vector(DP[, "x2"])
> [1] FALSE
> > is.vector(DP[, "x2"][[1]])
> [1] TRUE
> 
>
> but, note that a list of lists is also a vector:
> > is.vector(list(list(1), list(1,2,3)))
> [1] TRUE
> > str(list(list(1), list(1,2,3)))
> List of 2
>  $ :List of 1
>   ..$ : num 1
>  $ :List of 3
>   ..$ : num 1
>   ..$ : num 2
>   ..$ : num 3
>
> etc.
>
> hth.  good luck learning!
>
> cheers, Greg
>
> __
> R-help@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/posting-guide.html
> and provide commented, 

Re: [R] How to create a matrix from a list without a for loop

2021-07-09 Thread Duncan Murdoch

On 09/07/2021 3:37 p.m., Laurent Rhelp wrote:


Very effective solution, I hope I remember that for the nex time.


The key things to remember are that in R a "list" object is a vector 
whose elements are R objects, and that matrices are just vectors with 
dimension attributes.


Those two ideas are each useful for other things, too!

Duncan Murdoch


Thank you


Le 09/07/2021 à 19:50, David Winsemius a écrit :


On 7/9/21 10:40 AM, Laurent Rhelp wrote:

Dear R-Help-list,

   I have a list init_l containing 16 dataframes and I want to create
a matrix 4 x 4 from this list with a dataframe in every cell of the
matrix. I succeeded to do that but my loop is very uggly (cf. below).
Could somebody help me to write nice R code to do this loop ?

Thank you very much

Laurent


##
## mock data, 16 dataframes in a list
##
init_l <- lapply( seq(1,16) , function(x) {
   data.frame( V1 = rnorm(3),
   V2 = rnorm(3),
   V3 = rnorm(3)
     )
})

##
## lists matrix creation with n = 4 columns and n = 4 rows
##
n <- 4




Just assign a dimension attribute and you will have your two
dimensional structure



dim(init_l) <- c(n,n)
init_l[ 2,2]

[[1]]
   V1 V2 V3
1 -1.4103259  1.9214184 -0.1590919
2  0.1899490  0.3842191  2.4502078
3 -0.4282764 -0.9992190  1.5384344


is.matrix(init_l)

[1] TRUE






__
R-help@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/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to create a matrix from a list without a for loop

2021-07-09 Thread Laurent Rhelp



Very effective solution, I hope I remember that for the nex time.
Thank you


Le 09/07/2021 à 19:50, David Winsemius a écrit :


On 7/9/21 10:40 AM, Laurent Rhelp wrote:

Dear R-Help-list,

  I have a list init_l containing 16 dataframes and I want to create 
a matrix 4 x 4 from this list with a dataframe in every cell of the 
matrix. I succeeded to do that but my loop is very uggly (cf. below). 
Could somebody help me to write nice R code to do this loop ?


Thank you very much

Laurent


##
## mock data, 16 dataframes in a list
##
init_l <- lapply( seq(1,16) , function(x) {
  data.frame( V1 = rnorm(3),
  V2 = rnorm(3),
  V3 = rnorm(3)
    )
})

##
## lists matrix creation with n = 4 columns and n = 4 rows
##
n <- 4




Just assign a dimension attribute and you will have your two 
dimensional structure



> dim(init_l) <- c(n,n)
> init_l[ 2,2]
[[1]]
  V1 V2 V3
1 -1.4103259  1.9214184 -0.1590919
2  0.1899490  0.3842191  2.4502078
3 -0.4282764 -0.9992190  1.5384344

> is.matrix(init_l)
[1] TRUE




--
L'absence de virus dans ce courrier électronique a été vérifiée par le logiciel 
antivirus Avast.
https://www.avast.com/antivirus

__
R-help@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/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to create a matrix from a list without a for loop

2021-07-09 Thread Laurent Rhelp


You are right, this extra level disturbed me.
Very impressive this solution, thank you very much.


Le 09/07/2021 à 19:50, Bill Dunlap a écrit :
> Try
>    matrix(init_l, nrow=4, ncol=4, 
> dimnames=list(c("X1","X2","X3","X4"),c("X1","X2","X3","X4")))
> It doesn't give exactly what your code does, but your code introduces 
> an extra level of "list", which you may not want.
>
> -Bill
>
> On Fri, Jul 9, 2021 at 10:40 AM Laurent Rhelp  > wrote:
>
> Dear R-Help-list,
>
>    I have a list init_l containing 16 dataframes and I want to
> create a
> matrix 4 x 4 from this list with a dataframe in every cell of the
> matrix. I succeeded to do that but my loop is very uggly (cf. below).
> Could somebody help me to write nice R code to do this loop ?
>
> Thank you very much
>
> Laurent
>
>
> ##
> ## mock data, 16 dataframes in a list
> ##
> init_l <- lapply( seq(1,16) , function(x) {
>    data.frame( V1 = rnorm(3),
>    V2 = rnorm(3),
>    V3 = rnorm(3)
>  )
> })
>
> ##
> ## lists matrix creation with n = 4 columns and n = 4 rows
> ##
> n <- 4
> ## an example of row to create the matrix with lists in the cells
> one_row <- rbind( rep( list(rep(list(1),3)) , n) )
> mymat <- do.call( "rbind" , rep( list(one_row) , n) )
>
> ##
> ## The UGGLY loop I would like to improve:
> ##
>
> ## populate the matrix
> k <- 1
> for( i in 1:n){
>    for( j in 1:n){
>  mymat[i,j][[1]] <- list( init_l[[ k ]] )
>  k <- k+1
>    }
> }
>
> colnames(mymat) <- c("X1", "X2", "X3", "X3")
> rownames(mymat) <- c("X1", "X2", "X3", "X4")
>
>
> mymat
>
> # X1 X2 X3 X3
> # X1 List,1 List,1 List,1 List,1
> # X2 List,1 List,1 List,1 List,1
> # X3 List,1 List,1 List,1 List,1
> # X4 List,1 List,1 List,1 List,1
>
>
> #
> # verification, it works
> #
> mymat[2,2]
> init_l[[6]]
>
> ##
> init_l[[6]]
>
> library(tidyverse)
> mymat.t <- as.tibble(mymat)
> mymat.t
> unnest(mymat.t[2,2],cols="X2")[[1]][[1]]
>
> mymat.df <- as.data.frame(mymat)
> mymat.df[2,2][[1]][[1]]
>
>
> thx
>
>
>
> -- 
> L'absence de virus dans ce courrier électronique a été vérifiée
> par le logiciel antivirus Avast.
> https://www.avast.com/antivirus 
>
> __
> R-help@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/posting-guide.html
> 
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
L'absence de virus dans ce courrier électronique a été vérifiée par le logiciel 
antivirus Avast.
https://www.avast.com/antivirus

[[alternative HTML version deleted]]

__
R-help@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/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] error message from read.csv in loop

2021-07-09 Thread Kai Yang via R-help
 Hi Migdonio,
I did try your code:
# Initialize the rr variable as a list.

rr <- as.list(rep(NA, nrow(ora)))


# Run the for-loop to store all the CSVs in rr.

for (j in 1:nrow(ora))

{

        mycol  <- ora[j,"fname"]

        mycsv  <- paste0(mycol,".csv")

        rdcsv  <- noquote(paste0("w:/project/_Joe.B/Oracle/data/", mycsv))

        rr[[j]]     <- read.csv(rdcsv)

}

this code is working, but rr is not a data frame, R said: Large list ( 20 
elements .). how can I use it as a data frame one by one?
Thank you for your help
Kai
On Friday, July 9, 2021, 11:39:59 AM PDT, Migdonio González 
 wrote:  
 
 It seems that your problem is that you are using single quotes inside of the 
double quotes. This is not necessary. Here is the corrected for-loop:
for (j in 1:nrow(ora))
{
        mycol  <- ora[j,"fname"]
        mycsv  <- paste0(mycol,".csv")
        rdcsv  <- noquote(paste0("w:/project/_Joe.B/Oracle/data/", mycsv))
        rr     <- read.csv(rdcsv)
}
Also note that the rr variable will only store the last CSV, not all CSV. You 
will need to initialize the rr variable as a list to store all CSVs if that is 
what you require. Something like this:
# Initialize the rr variable as a list.
rr <- as.list(rep(NA, nrow(ora)))
# Run the for-loop to store all the CSVs in rr.
for (j in 1:nrow(ora))
{
        mycol  <- ora[j,"fname"]
        mycsv  <- paste0(mycol,".csv")
        rdcsv  <- noquote(paste0("w:/project/_Joe.B/Oracle/data/", mycsv))
        rr[[j]]     <- read.csv(rdcsv)
} 

RegardsMigdonio G.

On Fri, Jul 9, 2021 at 1:10 PM Kai Yang via R-help  wrote:

Hello List,
I use for loop to read csv difference file into data frame rr.  The data frame 
rr will be deleted after a comparison and go to the next csv file.  Below is my 
code:
for (j in 1:nrow(ora))
{
  mycol  <- ora[j,"fname"]
  mycsv  <- paste0(mycol,".csv'")
  rdcsv  <- noquote(paste0("'w:/project/_Joe.B/Oracle/data/", mycsv))
  rr     <- read.csv(rdcsv)
}
but when I run this code, I got error message below:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
  cannot open file ''w:/project/_Joe.B/Oracle/data/ASSAY_DEFINITIONS.csv'': No 
such file or directory

so, I checked the rdcsv and print it out, see below:
[1] 'w:/project/_Joe.B/Oracle/data/ASSAY_DEFINITIONS.csv'
[1] 'w:/project/_Joe.B/Oracle/data/ASSAY_DISCRETE_VALUES.csv'
[1] 'w:/project/_Joe.B/Oracle/data/ASSAY_QUESTIONS.csv'
[1] 'w:/project/_Joe.B/Oracle/data/ASSAY_RUNS.csv'
[1] 'w:/project/_Joe.B/Oracle/data/DATA_ENTRY_PAGES.csv'
[1] 'w:/project/_Joe.B/Oracle/data/DISCRETE_VALUES.csv'
[1] 'w:/project/_Joe.B/Oracle/data/ENTRY_GROUPS.csv'
[1] 'w:/project/_Joe.B/Oracle/data/GEMD_CODELIST_GROUPS.csv'
[1] 'w:/project/_Joe.B/Oracle/data/GEMD_CODELIST_VALUES.csv'
[1] 'w:/project/_Joe.B/Oracle/data/GEMD_LOT_DEFINITIONS.csv'
[1] 'w:/project/_Joe.B/Oracle/data/GEMD_SAMPLES.csv'
[1] 'w:/project/_Joe.B/Oracle/data/MOLECULAR_WAREHOUSE.csv'
[1] 'w:/project/_Joe.B/Oracle/data/QUESTION_DEFINITIONS.csv'
[1] 'w:/project/_Joe.B/Oracle/data/QUESTION_GROUPS.csv'
[1] 'w:/project/_Joe.B/Oracle/data/RESPONDENTS.csv'
[1] 'w:/project/_Joe.B/Oracle/data/RESPONSES.csv'
[1] 'w:/project/_Joe.B/Oracle/data/SAMPLE_LIST.csv'
[1] 'w:/project/_Joe.B/Oracle/data/SAMPLE_LIST_NAMES.csv'
[1] 'w:/project/_Joe.B/Oracle/data/SAMPLE_PLATE_ADDRESSES.csv'
[1] 'w:/project/_Joe.B/Oracle/data/STORAGE_UNITS.csv'
it seems correct. I copy and paste it into a code :
 rr     <- read.csv( 'w:/project/_Joe.B/Oracle/data/RESPONDENTS.csv')
and it works fine.
Can someone help me debug where is the problem in my for loop code?
Thanks,
Kai





        [[alternative HTML version deleted]]

__
R-help@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/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

  
[[alternative HTML version deleted]]

__
R-help@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/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] problem for strsplit function

2021-07-09 Thread Greg Minshall
Kai,

> one more question, how can I know if the function is for column
> manipulations or for vector?

i still stumble around R code.  but, i'd say the following (and look
forward to being corrected! :):

1.  a column, when extracted from a data frame, *is* a vector.

2.  maybe your question is "is a given function for a vector, or for a
data frame/matrix/array?".  if so, i think the only way is reading
the help information (?foo).

3.  sometimes, extracting the column as a vector from a data frame-like
object might be non-intuitive.  you might find reading ?"[" and
?"[.data.frame" useful (as well as ?"[.data.table" if you use that
package).  also, the str() command can be helpful in understanding
what is happening.  (the lobstr:: package's sxp() function, as well
as more verbose .Internal(inspect()) can also give you insight.)

with the data.table:: package, for example, if "DT" is a data.table
object, with "x2" as a column, adding or leaving off quotation marks
for the column name can make all the difference between ending up
with a vector, or with a (much reduced) data table:

> is.vector(DT[, x2])
[1] TRUE
> str(DT[, x2])
 num [1:9] 32 32 32 32 32 32 32 32 32
>
> is.vector(DT[, "x2"])
[1] FALSE
> str(DT[, "x2"])
Classes ‘data.table’ and 'data.frame':  9 obs. of  1 variable:
 $ x2: num  32 32 32 32 32 32 32 32 32
 - attr(*, ".internal.selfref")=


a second level of indexing may or may not help, mostly depending on
the use of '[' versus of '[['.  this can sometimes cause confusion
when you are learning the language.

> str(DT[, "x2"][1])
Classes ‘data.table’ and 'data.frame':  1 obs. of  1 variable:
 $ x2: num 32
 - attr(*, ".internal.selfref")=
> str(DT[, "x2"][[1]])
 num [1:9] 32 32 32 32 32 32 32 32 32


the tibble:: package (used in, e.g., the dplyr:: package) also
(always?) returns a single column as a non-vector.  again, a
second indexing with double '[[]]' can produce a vector.

> DP <- tibble(DT)
> is.vector(DP[, "x2"])
[1] FALSE
> is.vector(DP[, "x2"][[1]])
[1] TRUE


but, note that a list of lists is also a vector:
> is.vector(list(list(1), list(1,2,3)))
[1] TRUE
> str(list(list(1), list(1,2,3)))
List of 2
 $ :List of 1
  ..$ : num 1
 $ :List of 3
  ..$ : num 1
  ..$ : num 2
  ..$ : num 3

etc.

hth.  good luck learning!

cheers, Greg

__
R-help@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/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] error message from read.csv in loop

2021-07-09 Thread Eric Berger
it complained about  ASSAY_DEFINITIONS not about  RESPONDENTS.
Can you try with the ASSAY_DEFINITIONS  file?


On Fri, Jul 9, 2021 at 9:10 PM Kai Yang via R-help 
wrote:

> Hello List,
> I use for loop to read csv difference file into data frame rr.  The data
> frame rr will be deleted after a comparison and go to the next csv file.
> Below is my code:
> for (j in 1:nrow(ora))
> {
>   mycol  <- ora[j,"fname"]
>   mycsv  <- paste0(mycol,".csv'")
>   rdcsv  <- noquote(paste0("'w:/project/_Joe.B/Oracle/data/", mycsv))
>   rr <- read.csv(rdcsv)
> }
> but when I run this code, I got error message below:
> Error in file(file, "rt") : cannot open the connection
> In addition: Warning message:
> In file(file, "rt") :
>   cannot open file
> ''w:/project/_Joe.B/Oracle/data/ASSAY_DEFINITIONS.csv'': No such file or
> directory
>
> so, I checked the rdcsv and print it out, see below:
> [1] 'w:/project/_Joe.B/Oracle/data/ASSAY_DEFINITIONS.csv'
> [1] 'w:/project/_Joe.B/Oracle/data/ASSAY_DISCRETE_VALUES.csv'
> [1] 'w:/project/_Joe.B/Oracle/data/ASSAY_QUESTIONS.csv'
> [1] 'w:/project/_Joe.B/Oracle/data/ASSAY_RUNS.csv'
> [1] 'w:/project/_Joe.B/Oracle/data/DATA_ENTRY_PAGES.csv'
> [1] 'w:/project/_Joe.B/Oracle/data/DISCRETE_VALUES.csv'
> [1] 'w:/project/_Joe.B/Oracle/data/ENTRY_GROUPS.csv'
> [1] 'w:/project/_Joe.B/Oracle/data/GEMD_CODELIST_GROUPS.csv'
> [1] 'w:/project/_Joe.B/Oracle/data/GEMD_CODELIST_VALUES.csv'
> [1] 'w:/project/_Joe.B/Oracle/data/GEMD_LOT_DEFINITIONS.csv'
> [1] 'w:/project/_Joe.B/Oracle/data/GEMD_SAMPLES.csv'
> [1] 'w:/project/_Joe.B/Oracle/data/MOLECULAR_WAREHOUSE.csv'
> [1] 'w:/project/_Joe.B/Oracle/data/QUESTION_DEFINITIONS.csv'
> [1] 'w:/project/_Joe.B/Oracle/data/QUESTION_GROUPS.csv'
> [1] 'w:/project/_Joe.B/Oracle/data/RESPONDENTS.csv'
> [1] 'w:/project/_Joe.B/Oracle/data/RESPONSES.csv'
> [1] 'w:/project/_Joe.B/Oracle/data/SAMPLE_LIST.csv'
> [1] 'w:/project/_Joe.B/Oracle/data/SAMPLE_LIST_NAMES.csv'
> [1] 'w:/project/_Joe.B/Oracle/data/SAMPLE_PLATE_ADDRESSES.csv'
> [1] 'w:/project/_Joe.B/Oracle/data/STORAGE_UNITS.csv'
> it seems correct. I copy and paste it into a code :
>  rr <- read.csv( 'w:/project/_Joe.B/Oracle/data/RESPONDENTS.csv')
> and it works fine.
> Can someone help me debug where is the problem in my for loop code?
> Thanks,
> Kai
>
>
>
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@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/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] error message from read.csv in loop

2021-07-09 Thread Kai Yang via R-help
Hello List,
I use for loop to read csv difference file into data frame rr.  The data frame 
rr will be deleted after a comparison and go to the next csv file.  Below is my 
code:
for (j in 1:nrow(ora))
{
  mycol  <- ora[j,"fname"]
  mycsv  <- paste0(mycol,".csv'")
  rdcsv  <- noquote(paste0("'w:/project/_Joe.B/Oracle/data/", mycsv))
  rr     <- read.csv(rdcsv)
}
but when I run this code, I got error message below:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
  cannot open file ''w:/project/_Joe.B/Oracle/data/ASSAY_DEFINITIONS.csv'': No 
such file or directory

so, I checked the rdcsv and print it out, see below:
[1] 'w:/project/_Joe.B/Oracle/data/ASSAY_DEFINITIONS.csv'
[1] 'w:/project/_Joe.B/Oracle/data/ASSAY_DISCRETE_VALUES.csv'
[1] 'w:/project/_Joe.B/Oracle/data/ASSAY_QUESTIONS.csv'
[1] 'w:/project/_Joe.B/Oracle/data/ASSAY_RUNS.csv'
[1] 'w:/project/_Joe.B/Oracle/data/DATA_ENTRY_PAGES.csv'
[1] 'w:/project/_Joe.B/Oracle/data/DISCRETE_VALUES.csv'
[1] 'w:/project/_Joe.B/Oracle/data/ENTRY_GROUPS.csv'
[1] 'w:/project/_Joe.B/Oracle/data/GEMD_CODELIST_GROUPS.csv'
[1] 'w:/project/_Joe.B/Oracle/data/GEMD_CODELIST_VALUES.csv'
[1] 'w:/project/_Joe.B/Oracle/data/GEMD_LOT_DEFINITIONS.csv'
[1] 'w:/project/_Joe.B/Oracle/data/GEMD_SAMPLES.csv'
[1] 'w:/project/_Joe.B/Oracle/data/MOLECULAR_WAREHOUSE.csv'
[1] 'w:/project/_Joe.B/Oracle/data/QUESTION_DEFINITIONS.csv'
[1] 'w:/project/_Joe.B/Oracle/data/QUESTION_GROUPS.csv'
[1] 'w:/project/_Joe.B/Oracle/data/RESPONDENTS.csv'
[1] 'w:/project/_Joe.B/Oracle/data/RESPONSES.csv'
[1] 'w:/project/_Joe.B/Oracle/data/SAMPLE_LIST.csv'
[1] 'w:/project/_Joe.B/Oracle/data/SAMPLE_LIST_NAMES.csv'
[1] 'w:/project/_Joe.B/Oracle/data/SAMPLE_PLATE_ADDRESSES.csv'
[1] 'w:/project/_Joe.B/Oracle/data/STORAGE_UNITS.csv'
it seems correct. I copy and paste it into a code :
 rr     <- read.csv( 'w:/project/_Joe.B/Oracle/data/RESPONDENTS.csv')
and it works fine.
Can someone help me debug where is the problem in my for loop code?
Thanks,
Kai





[[alternative HTML version deleted]]

__
R-help@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/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to create a matrix from a list without a for loop

2021-07-09 Thread David Winsemius



On 7/9/21 10:40 AM, Laurent Rhelp wrote:

Dear R-Help-list,

  I have a list init_l containing 16 dataframes and I want to create a 
matrix 4 x 4 from this list with a dataframe in every cell of the 
matrix. I succeeded to do that but my loop is very uggly (cf. below). 
Could somebody help me to write nice R code to do this loop ?


Thank you very much

Laurent


##
## mock data, 16 dataframes in a list
##
init_l <- lapply( seq(1,16) , function(x) {
  data.frame( V1 = rnorm(3),
  V2 = rnorm(3),
  V3 = rnorm(3)
    )
})

##
## lists matrix creation with n = 4 columns and n = 4 rows
##
n <- 4




Just assign a dimension attribute and you will have your two dimensional 
structure



> dim(init_l) <- c(n,n)
> init_l[ 2,2]
[[1]]
  V1 V2 V3
1 -1.4103259  1.9214184 -0.1590919
2  0.1899490  0.3842191  2.4502078
3 -0.4282764 -0.9992190  1.5384344

> is.matrix(init_l)
[1] TRUE

--

David


## an example of row to create the matrix with lists in the cells
one_row <- rbind( rep( list(rep(list(1),3)) , n) )
mymat <- do.call( "rbind" , rep( list(one_row) , n) )

##
## The UGGLY loop I would like to improve:
##

## populate the matrix
k <- 1
for( i in 1:n){
  for( j in 1:n){
    mymat[i,j][[1]] <- list( init_l[[ k ]] )
    k <- k+1
  }
}

colnames(mymat) <- c("X1", "X2", "X3", "X3")
rownames(mymat) <- c("X1", "X2", "X3", "X4")


mymat

# X1 X2 X3 X3
# X1 List,1 List,1 List,1 List,1
# X2 List,1 List,1 List,1 List,1
# X3 List,1 List,1 List,1 List,1
# X4 List,1 List,1 List,1 List,1


#
# verification, it works
#
mymat[2,2]
init_l[[6]]

##
init_l[[6]]

library(tidyverse)
mymat.t <- as.tibble(mymat)
mymat.t
unnest(mymat.t[2,2],cols="X2")[[1]][[1]]

mymat.df <- as.data.frame(mymat)
mymat.df[2,2][[1]][[1]]


thx





__
R-help@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/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to create a matrix from a list without a for loop

2021-07-09 Thread Bill Dunlap
Try
   matrix(init_l, nrow=4, ncol=4,
dimnames=list(c("X1","X2","X3","X4"),c("X1","X2","X3","X4")))
It doesn't give exactly what your code does, but your code introduces an
extra level of "list", which you may not want.

-Bill

On Fri, Jul 9, 2021 at 10:40 AM Laurent Rhelp  wrote:

> Dear R-Help-list,
>
>I have a list init_l containing 16 dataframes and I want to create a
> matrix 4 x 4 from this list with a dataframe in every cell of the
> matrix. I succeeded to do that but my loop is very uggly (cf. below).
> Could somebody help me to write nice R code to do this loop ?
>
> Thank you very much
>
> Laurent
>
>
> ##
> ## mock data, 16 dataframes in a list
> ##
> init_l <- lapply( seq(1,16) , function(x) {
>data.frame( V1 = rnorm(3),
>V2 = rnorm(3),
>V3 = rnorm(3)
>  )
> })
>
> ##
> ## lists matrix creation with n = 4 columns and n = 4 rows
> ##
> n <- 4
> ## an example of row to create the matrix with lists in the cells
> one_row <- rbind( rep( list(rep(list(1),3)) , n) )
> mymat <- do.call( "rbind" , rep( list(one_row) , n) )
>
> ##
> ## The UGGLY loop I would like to improve:
> ##
>
> ## populate the matrix
> k <- 1
> for( i in 1:n){
>for( j in 1:n){
>  mymat[i,j][[1]] <- list( init_l[[ k ]] )
>  k <- k+1
>}
> }
>
> colnames(mymat) <- c("X1", "X2", "X3", "X3")
> rownames(mymat) <- c("X1", "X2", "X3", "X4")
>
>
> mymat
>
> # X1 X2 X3 X3
> # X1 List,1 List,1 List,1 List,1
> # X2 List,1 List,1 List,1 List,1
> # X3 List,1 List,1 List,1 List,1
> # X4 List,1 List,1 List,1 List,1
>
>
> #
> # verification, it works
> #
> mymat[2,2]
> init_l[[6]]
>
> ##
> init_l[[6]]
>
> library(tidyverse)
> mymat.t <- as.tibble(mymat)
> mymat.t
> unnest(mymat.t[2,2],cols="X2")[[1]][[1]]
>
> mymat.df <- as.data.frame(mymat)
> mymat.df[2,2][[1]][[1]]
>
>
> thx
>
>
>
> --
> L'absence de virus dans ce courrier électronique a été vérifiée par le
> logiciel antivirus Avast.
> https://www.avast.com/antivirus
>
> __
> R-help@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/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@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/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How to create a matrix from a list without a for loop

2021-07-09 Thread Laurent Rhelp

Dear R-Help-list,

  I have a list init_l containing 16 dataframes and I want to create a 
matrix 4 x 4 from this list with a dataframe in every cell of the 
matrix. I succeeded to do that but my loop is very uggly (cf. below). 
Could somebody help me to write nice R code to do this loop ?


Thank you very much

Laurent


##
## mock data, 16 dataframes in a list
##
init_l <- lapply( seq(1,16) , function(x) {
  data.frame( V1 = rnorm(3),
  V2 = rnorm(3),
  V3 = rnorm(3)
    )
})

##
## lists matrix creation with n = 4 columns and n = 4 rows
##
n <- 4
## an example of row to create the matrix with lists in the cells
one_row <- rbind( rep( list(rep(list(1),3)) , n) )
mymat <- do.call( "rbind" , rep( list(one_row) , n) )

##
## The UGGLY loop I would like to improve:
##

## populate the matrix
k <- 1
for( i in 1:n){
  for( j in 1:n){
    mymat[i,j][[1]] <- list( init_l[[ k ]] )
    k <- k+1
  }
}

colnames(mymat) <- c("X1", "X2", "X3", "X3")
rownames(mymat) <- c("X1", "X2", "X3", "X4")


mymat

# X1 X2 X3 X3
# X1 List,1 List,1 List,1 List,1
# X2 List,1 List,1 List,1 List,1
# X3 List,1 List,1 List,1 List,1
# X4 List,1 List,1 List,1 List,1


#
# verification, it works
#
mymat[2,2]
init_l[[6]]

##
init_l[[6]]

library(tidyverse)
mymat.t <- as.tibble(mymat)
mymat.t
unnest(mymat.t[2,2],cols="X2")[[1]][[1]]

mymat.df <- as.data.frame(mymat)
mymat.df[2,2][[1]][[1]]


thx



--
L'absence de virus dans ce courrier électronique a été vérifiée par le logiciel 
antivirus Avast.
https://www.avast.com/antivirus

__
R-help@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/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to estimate the parameter for many variable?

2021-07-09 Thread SITI AISYAH ZAKARIA
Dear Rui ang Jim,

Thank you very much.

Thank you Rui Barradas, I already tried using your coding and I'm grateful
I got the answer.

ok now, I have some condition on the location parameter which is cyclic
condition.

So, I will add another 2 variables for the column.

and the condition for location is this one.

 ti[,1] = seq(1, 888, 1)
  ti[,2]=sin(2*pi*(ti[,1])/52)
  ti[,3]=cos(2*pi*(ti[,1])/52)
  fit0<-gev.fit(x[,i], ydat = ti, mul=c(2, 3))

Thank you again.

On Fri, 9 Jul 2021 at 14:38, Rui Barradas  wrote:

> Hello,
>
> The following lapply one-liner fits a GEV to each column vector, there
> is no need for the double for loop. There's also no need to create a
> data set x.
>
>
> library(ismev)
> library(mgcv)
> library(EnvStats)
>
> Ozone_weekly2 <- read.table("~/tmp/Ozone_weekly2.txt", header = TRUE)
>
> # fit a GEV to each column
> gev_fit_list <- lapply(Ozone_weekly2, gev.fit, show = FALSE)
>
> # extract the parameters MLE estimates
> mle_params <- t(sapply(gev_fit_list, '[[', 'mle'))
>
> # assign column names
> colnames(mle_params) <- c("location", "scale", "shape")
>
> # see first few rows
> head(mle_params)
>
>
>
> The OP doesn't ask for plots but, here they go.
>
>
> y_vals <- function(x, params){
>loc <- params[1]
>scale <- params[2]
>shape <- params[3]
>EnvStats::dgevd(x, loc, scale, shape)
> }
> plot_fit <- function(data, vec, verbose = FALSE){
>fit <- gev.fit(data[[vec]], show = verbose)
>x <- sort(data[[vec]])
>hist(x, freq = FALSE)
>lines(x, y_vals(x, params = fit$mle))
> }
>
> # seems a good fit
> plot_fit(Ozone_weekly2, 1)   # column number
> plot_fit(Ozone_weekly2, "CA01")  # col name, equivalent
>
> # the data seems gaussian, not a good fit
> plot_fit(Ozone_weekly2, 4)   # column number
> plot_fit(Ozone_weekly2, "CA08")  # col name, equivalent
>
>
>
> Hope this helps,
>
> Rui Barradas
>
>
> Às 00:59 de 09/07/21, SITI AISYAH ZAKARIA escreveu:
> > Dear all,
> >
> > Thank you very much for the feedback.
> >
> > Sorry for the lack of information about this problem.
> >
> > Here, I explain again.
> >
> > I use this package to run my coding.
> >
> > library(ismev)
> > library(mgcv)
> > library(nlme)
> >
> > The purpose of this is I want to get the value of parameter estimation
> > using MLE by applying the GEV distribution.
> >
> > x <- data.matrix(Ozone_weekly2)  x refers to my data
> > that consists of 19 variables. I will attach the data together.
> > x
> > head(gev.fit)[1:4]
> > ti = matrix(ncol = 3, nrow = 888)
> > ti[,1] = seq(1, 888, 1)
> > ti[,2]=sin(2*pi*(ti[,1])/52)
> > ti[,3]=cos(2*pi*(ti[,1])/52)
> >
> > /for(i in 1:nrow(x))
> >+ { for(j in 1:ncol(x))the problem in
> > here, i don't no to create the coding. i target my output will come out
> > in matrix that
> >  + {x[i,j] = 1}}   show the
> > parameter estimation for 19 variable which have 19 row and 3 column/
> > /  row --
> > refer to variable (station)  ; column -- refer to parameter estimation
> > for GEV distribution
> >
> > /thank you.
> >
> > On Thu, 8 Jul 2021 at 18:40, Rui Barradas  > > wrote:
> >
> > Hello,
> >
> > Also, in the code
> >
> > x <- data.matrix(Ozone_weekly)
> >
> > [...omited...]
> >
> > for(i in 1:nrow(x))
> > + { for(j in 1:ncol(x))
> >   + {x[i,j] = 1}}
> >
> > not only you rewrite x but the double for loop is equivalent to
> >
> >
> > x[] <- 1
> >
> >
> > courtesy R's vectorised behavior. (The square parenthesis are needed
> to
> > keep the dimensions, the matrix form.)
> > And, I'm not sure but isn't
> >
> > head(gev.fit)[1:4]
> >
> > equivalent to
> >
> > head(gev.fit, n = 4)
> >
> > ?
> >
> > Like Jim says, we need more information, can you post Ozone_weekly2
> and
> > the code that produced gev.fit? But in the mean time you can revise
> > your
> > code.
> >
> > Hope this helps,
> >
> > Rui Barradas
> >
> >
> > Às 11:08 de 08/07/21, Jim Lemon escreveu:
> >  > Hi Siti,
> >  > I think we need a bit more information to respond helpfully. I
> > have no
> >  > idea what "Ozone_weekly2" is and Google is also ignorant.
> > "gev.fit" is
> >  > also unknown. The name suggests that it is the output of some
> >  > regression or similar. What function produced it, and from what
> >  > library? "ti" is known as you have defined it. However, I don't
> know
> >  > what you want to do with it. Finally, as this is a text mailing
> list,
> >  > we don't get any highlighting, so the text to which you refer
> cannot
> >  > be identified. I can see you have a problem, but cannot offer any
> > help
> >  > right now.
> >  >
> >  > Jim
> >  >
> >  > On Thu, Jul 8, 2021 at 12:06 AM SITI AISYAH ZAKARIA
> >  >  > 

Re: [R] How to estimate the parameter for many variable?

2021-07-09 Thread Rui Barradas

Hello,

With the condition for the location it can be estimated like the following.


fit_list2 <- gev_fit_list <- lapply(Ozone_weekly2, gev.fit, ydat = ti, 
mul = c(2, 3), show = FALSE)

mle_params2 <- t(sapply(fit_list2, '[[', 'mle'))
# assign column names
colnames(mle_params2) <- c("location", "scale", "shape", "mul2", "mul3")
head(mle_params2)


Hope this helps,

Rui Barradas

Às 09:53 de 09/07/21, SITI AISYAH ZAKARIA escreveu:

Dear Rui ang Jim,

Thank you very much.

Thank you Rui Barradas, I already tried using your coding and I'm 
grateful I got the answer.


ok now, I have some condition on the location parameter which is cyclic 
condition.


So, I will add another 2 variables for the column.

and the condition for location is this one.

  ti[,1] = seq(1, 888, 1)
   ti[,2]=sin(2*pi*(ti[,1])/52)
   ti[,3]=cos(2*pi*(ti[,1])/52)
   fit0<-gev.fit(x[,i], ydat = ti, mul=c(2, 3))

Thank you again.

On Fri, 9 Jul 2021 at 14:38, Rui Barradas > wrote:


Hello,

The following lapply one-liner fits a GEV to each column vector, there
is no need for the double for loop. There's also no need to create a
data set x.


library(ismev)
library(mgcv)
library(EnvStats)

Ozone_weekly2 <- read.table("~/tmp/Ozone_weekly2.txt", header = TRUE)

# fit a GEV to each column
gev_fit_list <- lapply(Ozone_weekly2, gev.fit, show = FALSE)

# extract the parameters MLE estimates
mle_params <- t(sapply(gev_fit_list, '[[', 'mle'))

# assign column names
colnames(mle_params) <- c("location", "scale", "shape")

# see first few rows
head(mle_params)



The OP doesn't ask for plots but, here they go.


y_vals <- function(x, params){
    loc <- params[1]
    scale <- params[2]
    shape <- params[3]
    EnvStats::dgevd(x, loc, scale, shape)
}
plot_fit <- function(data, vec, verbose = FALSE){
    fit <- gev.fit(data[[vec]], show = verbose)
    x <- sort(data[[vec]])
    hist(x, freq = FALSE)
    lines(x, y_vals(x, params = fit$mle))
}

# seems a good fit
plot_fit(Ozone_weekly2, 1)       # column number
plot_fit(Ozone_weekly2, "CA01")  # col name, equivalent

# the data seems gaussian, not a good fit
plot_fit(Ozone_weekly2, 4)       # column number
plot_fit(Ozone_weekly2, "CA08")  # col name, equivalent



Hope this helps,

Rui Barradas


Às 00:59 de 09/07/21, SITI AISYAH ZAKARIA escreveu:
 > Dear all,
 >
 > Thank you very much for the feedback.
 >
 > Sorry for the lack of information about this problem.
 >
 > Here, I explain again.
 >
 > I use this package to run my coding.
 >
 > library(ismev)
 > library(mgcv)
 > library(nlme)
 >
 > The purpose of this is I want to get the value of parameter
estimation
 > using MLE by applying the GEV distribution.
 >
 > x <- data.matrix(Ozone_weekly2)                      x refers to
my data
 > that consists of 19 variables. I will attach the data together.
 > x
 > head(gev.fit)[1:4]
 > ti = matrix(ncol = 3, nrow = 888)
 > ti[,1] = seq(1, 888, 1)
 > ti[,2]=sin(2*pi*(ti[,1])/52)
 > ti[,3]=cos(2*pi*(ti[,1])/52)
 >
 > /for(i in 1:nrow(x))
 >    + { for(j in 1:ncol(x))                            the problem in
 > here, i don't no to create the coding. i target my output will
come out
 > in matrix that
 >      + {x[i,j] = 1}}                                       show the
 > parameter estimation for 19 variable which have 19 row and 3 column/
 > / 
row --

 > refer to variable (station)  ; column -- refer to parameter
estimation
 > for GEV distribution
 >
 > /thank you.
 >
 > On Thu, 8 Jul 2021 at 18:40, Rui Barradas mailto:ruipbarra...@sapo.pt>
 > >> wrote:
 >
 >     Hello,
 >
 >     Also, in the code
 >
 >     x <- data.matrix(Ozone_weekly)
 >
 >     [...omited...]
 >
 >     for(i in 1:nrow(x))
 >         + { for(j in 1:ncol(x))
 >           + {x[i,j] = 1}}
 >
 >     not only you rewrite x but the double for loop is equivalent to
 >
 >
 >     x[] <- 1
 >
 >
 >     courtesy R's vectorised behavior. (The square parenthesis are
needed to
 >     keep the dimensions, the matrix form.)
 >     And, I'm not sure but isn't
 >
 >     head(gev.fit)[1:4]
 >
 >     equivalent to
 >
 >     head(gev.fit, n = 4)
 >
 >     ?
 >
 >     Like Jim says, we need more information, can you post
Ozone_weekly2 and
 >     the code that produced gev.fit? But in the mean time you can
revise
 >     your
 >     code.
 >
 >     Hope this helps,
 >
 >     Rui Barradas
 >
 >
 >     Às 11:08 

Re: [R] How to estimate the parameter for many variable?

2021-07-09 Thread Rui Barradas

Hello,

The following lapply one-liner fits a GEV to each column vector, there 
is no need for the double for loop. There's also no need to create a 
data set x.



library(ismev)
library(mgcv)
library(EnvStats)

Ozone_weekly2 <- read.table("~/tmp/Ozone_weekly2.txt", header = TRUE)

# fit a GEV to each column
gev_fit_list <- lapply(Ozone_weekly2, gev.fit, show = FALSE)

# extract the parameters MLE estimates
mle_params <- t(sapply(gev_fit_list, '[[', 'mle'))

# assign column names
colnames(mle_params) <- c("location", "scale", "shape")

# see first few rows
head(mle_params)



The OP doesn't ask for plots but, here they go.


y_vals <- function(x, params){
  loc <- params[1]
  scale <- params[2]
  shape <- params[3]
  EnvStats::dgevd(x, loc, scale, shape)
}
plot_fit <- function(data, vec, verbose = FALSE){
  fit <- gev.fit(data[[vec]], show = verbose)
  x <- sort(data[[vec]])
  hist(x, freq = FALSE)
  lines(x, y_vals(x, params = fit$mle))
}

# seems a good fit
plot_fit(Ozone_weekly2, 1)   # column number
plot_fit(Ozone_weekly2, "CA01")  # col name, equivalent

# the data seems gaussian, not a good fit
plot_fit(Ozone_weekly2, 4)   # column number
plot_fit(Ozone_weekly2, "CA08")  # col name, equivalent



Hope this helps,

Rui Barradas


Às 00:59 de 09/07/21, SITI AISYAH ZAKARIA escreveu:

Dear all,

Thank you very much for the feedback.

Sorry for the lack of information about this problem.

Here, I explain again.

I use this package to run my coding.

library(ismev)
library(mgcv)
library(nlme)

The purpose of this is I want to get the value of parameter estimation 
using MLE by applying the GEV distribution.


x <- data.matrix(Ozone_weekly2)                      x refers to my data 
that consists of 19 variables. I will attach the data together.

x
head(gev.fit)[1:4]
ti = matrix(ncol = 3, nrow = 888)
ti[,1] = seq(1, 888, 1)
ti[,2]=sin(2*pi*(ti[,1])/52)
ti[,3]=cos(2*pi*(ti[,1])/52)

/for(i in 1:nrow(x))
   + { for(j in 1:ncol(x))                            the problem in 
here, i don't no to create the coding. i target my output will come out 
in matrix that
     + {x[i,j] = 1}}                                       show the 
parameter estimation for 19 variable which have 19 row and 3 column/
/                                                              row -- 
refer to variable (station)  ; column -- refer to parameter estimation 
for GEV distribution


/thank you.

On Thu, 8 Jul 2021 at 18:40, Rui Barradas > wrote:


Hello,

Also, in the code

x <- data.matrix(Ozone_weekly)

[...omited...]

for(i in 1:nrow(x))
    + { for(j in 1:ncol(x))
      + {x[i,j] = 1}}

not only you rewrite x but the double for loop is equivalent to


x[] <- 1


courtesy R's vectorised behavior. (The square parenthesis are needed to
keep the dimensions, the matrix form.)
And, I'm not sure but isn't

head(gev.fit)[1:4]

equivalent to

head(gev.fit, n = 4)

?

Like Jim says, we need more information, can you post Ozone_weekly2 and
the code that produced gev.fit? But in the mean time you can revise
your
code.

Hope this helps,

Rui Barradas


Às 11:08 de 08/07/21, Jim Lemon escreveu:
 > Hi Siti,
 > I think we need a bit more information to respond helpfully. I
have no
 > idea what "Ozone_weekly2" is and Google is also ignorant.
"gev.fit" is
 > also unknown. The name suggests that it is the output of some
 > regression or similar. What function produced it, and from what
 > library? "ti" is known as you have defined it. However, I don't know
 > what you want to do with it. Finally, as this is a text mailing list,
 > we don't get any highlighting, so the text to which you refer cannot
 > be identified. I can see you have a problem, but cannot offer any
help
 > right now.
 >
 > Jim
 >
 > On Thu, Jul 8, 2021 at 12:06 AM SITI AISYAH ZAKARIA
 > mailto:aisyahzaka...@unimap.edu.my>> wrote:
 >>
 >> Dear all,
 >>
 >> Can I ask something about programming in marginal distribution
for spatial
 >> extreme?
 >> I really stuck on my coding to obtain the parameter estimation for
 >> univariate or marginal distribution for new model in spatial
extreme.
 >>
 >> I want to run my data in order to get the parameter estimation
value for 25
 >> stations in one table. But I really didn't get the idea of the
correct
 >> coding. Here I attached my coding
 >>
 >> x <- data.matrix(Ozone_weekly2)
 >> x
 >> head(gev.fit)[1:4]
 >> ti = matrix(ncol = 3, nrow = 888)
 >> ti[,1] = seq(1, 888, 1)
 >> ti[,2]=sin(2*pi*(ti[,1])/52)
 >> ti[,3]=cos(2*pi*(ti[,1])/52)
 >> for(i in 1:nrow(x))
 >>    + { for(j in 1:ncol(x))
 >>      + {x[i,j] = 1}}
 >>
 >> My problem is highlighted in red color.
 >> And if are not hesitate to all. Can someone share with me the

Re: [R] How to estimate the parameter for many variable?

2021-07-09 Thread SITI AISYAH ZAKARIA
Dear all,

Thank you very much for the feedback.

Sorry for the lack of information about this problem.

Here, I explain again.

I use this package to run my coding.

library(ismev)
library(mgcv)
library(nlme)

The purpose of this is I want to get the value of parameter estimation
using MLE by applying the GEV distribution.

x <- data.matrix(Ozone_weekly2)  x refers to my data
that consists of 19 variables. I will attach the data together.
x
head(gev.fit)[1:4]
ti = matrix(ncol = 3, nrow = 888)
ti[,1] = seq(1, 888, 1)
ti[,2]=sin(2*pi*(ti[,1])/52)
ti[,3]=cos(2*pi*(ti[,1])/52)



*for(i in 1:nrow(x))  + { for(j in 1:ncol(x))
the problem in here, i don't no to create the coding. i target my
output will come out in matrix that + {x[i,j] = 1}}
   show the parameter estimation for 19 variable which have
19 row and 3 column*


*  row -- refer
to variable (station)  ; column -- refer to parameter estimation for GEV
distribution*thank you.

On Thu, 8 Jul 2021 at 18:40, Rui Barradas  wrote:

> Hello,
>
> Also, in the code
>
> x <- data.matrix(Ozone_weekly)
>
> [...omited...]
>
> for(i in 1:nrow(x))
>+ { for(j in 1:ncol(x))
>  + {x[i,j] = 1}}
>
> not only you rewrite x but the double for loop is equivalent to
>
>
> x[] <- 1
>
>
> courtesy R's vectorised behavior. (The square parenthesis are needed to
> keep the dimensions, the matrix form.)
> And, I'm not sure but isn't
>
> head(gev.fit)[1:4]
>
> equivalent to
>
> head(gev.fit, n = 4)
>
> ?
>
> Like Jim says, we need more information, can you post Ozone_weekly2 and
> the code that produced gev.fit? But in the mean time you can revise your
> code.
>
> Hope this helps,
>
> Rui Barradas
>
>
> Às 11:08 de 08/07/21, Jim Lemon escreveu:
> > Hi Siti,
> > I think we need a bit more information to respond helpfully. I have no
> > idea what "Ozone_weekly2" is and Google is also ignorant. "gev.fit" is
> > also unknown. The name suggests that it is the output of some
> > regression or similar. What function produced it, and from what
> > library? "ti" is known as you have defined it. However, I don't know
> > what you want to do with it. Finally, as this is a text mailing list,
> > we don't get any highlighting, so the text to which you refer cannot
> > be identified. I can see you have a problem, but cannot offer any help
> > right now.
> >
> > Jim
> >
> > On Thu, Jul 8, 2021 at 12:06 AM SITI AISYAH ZAKARIA
> >  wrote:
> >>
> >> Dear all,
> >>
> >> Can I ask something about programming in marginal distribution for
> spatial
> >> extreme?
> >> I really stuck on my coding to obtain the parameter estimation for
> >> univariate or marginal distribution for new model in spatial extreme.
> >>
> >> I want to run my data in order to get the parameter estimation value
> for 25
> >> stations in one table. But I really didn't get the idea of the correct
> >> coding. Here I attached my coding
> >>
> >> x <- data.matrix(Ozone_weekly2)
> >> x
> >> head(gev.fit)[1:4]
> >> ti = matrix(ncol = 3, nrow = 888)
> >> ti[,1] = seq(1, 888, 1)
> >> ti[,2]=sin(2*pi*(ti[,1])/52)
> >> ti[,3]=cos(2*pi*(ti[,1])/52)
> >> for(i in 1:nrow(x))
> >>+ { for(j in 1:ncol(x))
> >>  + {x[i,j] = 1}}
> >>
> >> My problem is highlighted in red color.
> >> And if are not hesitate to all. Can someone share with me the procedure,
> >> how can I map my data using spatial extreme.
> >> For example:
> >> After I finish my marginal distribution, what the next procedure. It is
> I
> >> need to get the spatial independent value.
> >>
> >> That's all
> >> Thank you.
> >>
> >> --
> >>
> >>
> >>
> >>
> >>
> >> "..Millions of trees are used to make papers, only to be thrown away
> >> after a couple of minutes reading from them. Our planet is at stake.
> Please
> >> be considerate. THINK TWICE BEFORE PRINTING THIS.."
> >>
> >> DISCLAIMER: This email \ and any files transmitte...{{dropped:24}}
> >>
> >> __
> >> R-help@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/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >
> > __
> > R-help@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/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>

-- 





"..Millions of trees are used to make papers, only to be thrown away 
after a couple of minutes reading from them. Our planet is at stake. Please 
be considerate. THINK TWICE BEFORE PRINTING THIS.."

DISCLAIMER: This email 
and any files transmitted with it are confidential and intended solely for 
the use of the individual orentity to whom they are addressed. If you 

Re: [R] How to estimate the parameter for many variable?

2021-07-09 Thread Jim Lemon
Hi Siti,
I think some progress has been made. You have a data set with 888 rows
and 19 columns:

ow2<-read.table("Ozone_weekly2.txt",
  header=TRUE)
dim(ow2)
[1] 888  19

The values may be parts per million ozone in the atmosphere. The
columns may represent different measuring locations and my guess is
that rows are times of measurement, perhaps in weeks given the name.
If so, you might have run ismev::gev.fit() on all of the columns,
looking for estimated maxima at each location. Is this at all close to
what you are doing?

Jim

On Fri, Jul 9, 2021 at 9:59 AM SITI AISYAH ZAKARIA
 wrote:
>
> Dear all,
>
> Thank you very much for the feedback.
>
> Sorry for the lack of information about this problem.
>
> Here, I explain again.
>
> I use this package to run my coding.
>
> library(ismev)
> library(mgcv)
> library(nlme)
>
> The purpose of this is I want to get the value of parameter estimation using 
> MLE by applying the GEV distribution.
>
> x <- data.matrix(Ozone_weekly2)  x refers to my data that 
> consists of 19 variables. I will attach the data together.
> x
> head(gev.fit)[1:4]
> ti = matrix(ncol = 3, nrow = 888)
> ti[,1] = seq(1, 888, 1)
> ti[,2]=sin(2*pi*(ti[,1])/52)
> ti[,3]=cos(2*pi*(ti[,1])/52)
>
> for(i in 1:nrow(x))
>   + { for(j in 1:ncol(x))the problem in here, i 
> don't no to create the coding. i target my output will come out in matrix that
> + {x[i,j] = 1}}   show the parameter 
> estimation for 19 variable which have 19 row and 3 column
>   row -- refer to 
> variable (station)  ; column -- refer to parameter estimation for GEV 
> distribution
>
> thank you.
>
> On Thu, 8 Jul 2021 at 18:40, Rui Barradas  wrote:
>>
>> Hello,
>>
>> Also, in the code
>>
>> x <- data.matrix(Ozone_weekly)
>>
>> [...omited...]
>>
>> for(i in 1:nrow(x))
>>+ { for(j in 1:ncol(x))
>>  + {x[i,j] = 1}}
>>
>> not only you rewrite x but the double for loop is equivalent to
>>
>>
>> x[] <- 1
>>
>>
>> courtesy R's vectorised behavior. (The square parenthesis are needed to
>> keep the dimensions, the matrix form.)
>> And, I'm not sure but isn't
>>
>> head(gev.fit)[1:4]
>>
>> equivalent to
>>
>> head(gev.fit, n = 4)
>>
>> ?
>>
>> Like Jim says, we need more information, can you post Ozone_weekly2 and
>> the code that produced gev.fit? But in the mean time you can revise your
>> code.
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>>
>> Às 11:08 de 08/07/21, Jim Lemon escreveu:
>> > Hi Siti,
>> > I think we need a bit more information to respond helpfully. I have no
>> > idea what "Ozone_weekly2" is and Google is also ignorant. "gev.fit" is
>> > also unknown. The name suggests that it is the output of some
>> > regression or similar. What function produced it, and from what
>> > library? "ti" is known as you have defined it. However, I don't know
>> > what you want to do with it. Finally, as this is a text mailing list,
>> > we don't get any highlighting, so the text to which you refer cannot
>> > be identified. I can see you have a problem, but cannot offer any help
>> > right now.
>> >
>> > Jim
>> >
>> > On Thu, Jul 8, 2021 at 12:06 AM SITI AISYAH ZAKARIA
>> >  wrote:
>> >>
>> >> Dear all,
>> >>
>> >> Can I ask something about programming in marginal distribution for spatial
>> >> extreme?
>> >> I really stuck on my coding to obtain the parameter estimation for
>> >> univariate or marginal distribution for new model in spatial extreme.
>> >>
>> >> I want to run my data in order to get the parameter estimation value for 
>> >> 25
>> >> stations in one table. But I really didn't get the idea of the correct
>> >> coding. Here I attached my coding
>> >>
>> >> x <- data.matrix(Ozone_weekly2)
>> >> x
>> >> head(gev.fit)[1:4]
>> >> ti = matrix(ncol = 3, nrow = 888)
>> >> ti[,1] = seq(1, 888, 1)
>> >> ti[,2]=sin(2*pi*(ti[,1])/52)
>> >> ti[,3]=cos(2*pi*(ti[,1])/52)
>> >> for(i in 1:nrow(x))
>> >>+ { for(j in 1:ncol(x))
>> >>  + {x[i,j] = 1}}
>> >>
>> >> My problem is highlighted in red color.
>> >> And if are not hesitate to all. Can someone share with me the procedure,
>> >> how can I map my data using spatial extreme.
>> >> For example:
>> >> After I finish my marginal distribution, what the next procedure. It is I
>> >> need to get the spatial independent value.
>> >>
>> >> That's all
>> >> Thank you.
>> >>
>> >> --
>> >>
>> >>
>> >>
>> >>
>> >>
>> >> "..Millions of trees are used to make papers, only to be thrown away
>> >> after a couple of minutes reading from them. Our planet is at stake. 
>> >> Please
>> >> be considerate. THINK TWICE BEFORE PRINTING THIS.."
>> >>
>> >> DISCLAIMER: This email \ and any files transmitte...{{dropped:24}}
>> >>
>> >> __
>> >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> >> https://stat.ethz.ch/mailman/listinfo/r-help
>> >> PLEASE do read the posting guide 
>> >>