Re: [R] [Tagged] Re: col.names in as.data.frame() ?

2023-10-28 Thread avi.e.gross
Jef, your terse reply was so constructive that you converted me! LOL!

That is an interesting point though that I remain a bit unclear on. 

Both data.frame and as.data.frame can be used in some ways similarly as in:

> data.frame(matrix(1:12, nrow=3))
  X1 X2 X3 X4
1  1  4  7 10
2  2  5  8 11
3  3  6  9 12

> as.data.frame(matrix(1:12, nrow=3))
  V1 V2 V3 V4
1  1  4  7 10
2  2  5  8 11
3  3  6  9 12

But yes, the constructor accepts many arguments  while the converter really 
normally handles a single object.

Where do some other things like cbind() fit, not to mention a dplyr function 
like tribble()?

I do wonder though, why asking a converter to convert a matrix to a data.frame 
and perhaps adding column names, is considered changing the object contents. 
The manual page for as.data.frame seems to include quite a few options to 
specify the name of a single column, truncate column names, deal with row 
names, and more as well as whether to convert strings to factors. Are those 
things different enough than what we are discussing?

Of course, we may indeed be experiencing mission creep where something simple 
keeps being improved with new features until the original simplicity and 
clarity gets lost.


-Original Message-
From: R-help  On Behalf Of Jeff Newmiller via 
R-help
Sent: Saturday, October 28, 2023 2:54 PM
To: r-help@r-project.org; Boris Steipe ; R. Mailing 
List 
Subject: Re: [R] [Tagged] Re: col.names in as.data.frame() ?

as.data.frame is a _converter_, while data.frame is a _constructor_.  Changing 
the object contents is not what a conversion is for.

On October 28, 2023 11:39:22 AM PDT, Boris Steipe  
wrote:
>Thanks Duncan and Avi!
>
>That you could use NULL in a matrix() dimnames = list(...) argument wasn't 
>clear to me. I thought that would be equivalent to a one-element list - and 
>thereby define rownames. So that's good to know.
>
>The documentation could be more explicit - but it is probably more work to do 
>that than just patch the code to honour a col.names argument. (At least I 
>can't see a reason not to.)
>
>Thanks again!
>:-)
>
>
>
>
>> On Oct 28, 2023, at 14:24, avi.e.gr...@gmail.com wrote:
>> 
>> Борис,
>> 
>> Try this where you tell matrix the column names you want:
>> 
>> nouns <- as.data.frame(
>>  matrix(c(
>>"gaggle",
>>"geese",
>> 
>>"dule",
>>"doves",
>> 
>>"wake",
>>"vultures"
>>  ), 
>>  ncol = 2, 
>>  byrow = TRUE, 
>>  dimnames=list(NULL, c("collective", "category"
>> 
>> Result:
>> 
>>> nouns
>>  collective category
>> 1 gagglegeese
>> 2   duledoves
>> 3   wake vultures
>> 
>> 
>> The above simply names the columns earlier when creating the matrix.
>> 
>> There are other ways and the way you tried LOOKS like it should work but
>> fails for me with a message about it weirdly expecting three rows versus two
>> which seems to confuse rows and columns. My version of R is recent and I
>> wonder if there is a bug here.
>> 
>> Consider whether you really need the data.frame created in a single
>> statement or can you change the column names next as in:
>> 
>> 
>>> nouns
>>  V1   V2
>> 1 gagglegeese
>> 2   duledoves
>> 3   wake vultures
>>> colnames(nouns)
>> [1] "V1" "V2"
>>> colnames(nouns) <- c("collective", "category")
>>> nouns
>>  collective category
>> 1 gagglegeese
>> 2   duledoves
>> 3   wake vultures
>> 
>> Is there a known bug here or is the documentation wrong?
>> 
>> -Original Message-
>> From: R-help  On Behalf Of Boris Steipe
>> Sent: Saturday, October 28, 2023 1:54 PM
>> To: R. Mailing List 
>> Subject: [R] col.names in as.data.frame() ?
>> 
>> I have been trying to create a data frame from some structured text in a
>> single expression. Reprex:
>> 
>> nouns <- as.data.frame(
>>  matrix(c(
>>"gaggle",
>>"geese",
>> 
>>"dule",
>>"doves",
>> 
>>"wake",
>>"vultures"
>>  ), ncol = 2, byrow = TRUE),
>>  col.names = c("collective", "category")
>> )
>> 
>> But ... :
>> 
>>> str(nouns)
>> 'data.frame': 3 obs. of  2 variables:
>> $ V1: chr  "gaggle" "dule" "wake"
>> $ V2: chr  "geese" "doves" "vultures"
>> 
>> i.e. the col.names argument does nothing. From my reading of ?as.data.frame,
>> my example should have worked.
>> 
>> I know how to get the required result with colnames(), but I would like to
>> understand why the idiom as written didn't work, and how I could have known
>> that from the help file.
>> 
>> 
>> Thanks!
>> Boris
>> 
>> __
>> 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 gui

Re: [R] [Tagged] Re: col.names in as.data.frame() ?

2023-10-28 Thread Duncan Murdoch

On 28/10/2023 4:45 p.m., Bert Gunter wrote:

Jeff, et. al. : but ...

Note that as.data.frame() *already* changes the matrix object by adding
column names of *its own choosing* when the matrix has none. So the issue
here is not *whether* col names should be added, but *what*/*how* they
should be. Unless you wish to extend your criticism to the current version
for its failure to adhere to your proscription.


Dataframes have to have column names.  The function isn't modifying the 
object any more than it has to.


Duncan Murdoch



Cheers,
Bert

On Sat, Oct 28, 2023 at 11:55 AM Jeff Newmiller via R-help <
r-help@r-project.org> wrote:


as.data.frame is a _converter_, while data.frame is a _constructor_.
Changing the object contents is not what a conversion is for.

On October 28, 2023 11:39:22 AM PDT, Boris Steipe <
boris.ste...@utoronto.ca> wrote:

Thanks Duncan and Avi!

That you could use NULL in a matrix() dimnames = list(...) argument

wasn't clear to me. I thought that would be equivalent to a one-element
list - and thereby define rownames. So that's good to know.


The documentation could be more explicit - but it is probably more work

to do that than just patch the code to honour a col.names argument. (At
least I can't see a reason not to.)


Thanks again!
:-)





On Oct 28, 2023, at 14:24, avi.e.gr...@gmail.com wrote:

Борис,

Try this where you tell matrix the column names you want:

nouns <- as.data.frame(
  matrix(c(
"gaggle",
"geese",

"dule",
"doves",

"wake",
"vultures"
  ),
  ncol = 2,
  byrow = TRUE,
  dimnames=list(NULL, c("collective", "category"

Result:


nouns

  collective category
1 gagglegeese
2   duledoves
3   wake vultures


The above simply names the columns earlier when creating the matrix.

There are other ways and the way you tried LOOKS like it should work but
fails for me with a message about it weirdly expecting three rows

versus two

which seems to confuse rows and columns. My version of R is recent and I
wonder if there is a bug here.

Consider whether you really need the data.frame created in a single
statement or can you change the column names next as in:



nouns

  V1   V2
1 gagglegeese
2   duledoves
3   wake vultures

colnames(nouns)

[1] "V1" "V2"

colnames(nouns) <- c("collective", "category")
nouns

  collective category
1 gagglegeese
2   duledoves
3   wake vultures

Is there a known bug here or is the documentation wrong?

-Original Message-
From: R-help  On Behalf Of Boris Steipe
Sent: Saturday, October 28, 2023 1:54 PM
To: R. Mailing List 
Subject: [R] col.names in as.data.frame() ?

I have been trying to create a data frame from some structured text in a
single expression. Reprex:

nouns <- as.data.frame(
  matrix(c(
"gaggle",
"geese",

"dule",
"doves",

"wake",
"vultures"
  ), ncol = 2, byrow = TRUE),
  col.names = c("collective", "category")
)

But ... :


str(nouns)

'data.frame': 3 obs. of  2 variables:
$ V1: chr  "gaggle" "dule" "wake"
$ V2: chr  "geese" "doves" "vultures"

i.e. the col.names argument does nothing. From my reading of

?as.data.frame,

my example should have worked.

I know how to get the required result with colnames(), but I would like

to

understand why the idiom as written didn't work, and how I could have

known

that from the help file.


Thanks!
Boris

__
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.


--
Sent from my phone. Please excuse my brevity.

__
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-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-c

Re: [R] [Tagged] Re: col.names in as.data.frame() ?

2023-10-28 Thread Bert Gunter
Jeff, et. al. : but ...

Note that as.data.frame() *already* changes the matrix object by adding
column names of *its own choosing* when the matrix has none. So the issue
here is not *whether* col names should be added, but *what*/*how* they
should be. Unless you wish to extend your criticism to the current version
for its failure to adhere to your proscription.

Cheers,
Bert

On Sat, Oct 28, 2023 at 11:55 AM Jeff Newmiller via R-help <
r-help@r-project.org> wrote:

> as.data.frame is a _converter_, while data.frame is a _constructor_.
> Changing the object contents is not what a conversion is for.
>
> On October 28, 2023 11:39:22 AM PDT, Boris Steipe <
> boris.ste...@utoronto.ca> wrote:
> >Thanks Duncan and Avi!
> >
> >That you could use NULL in a matrix() dimnames = list(...) argument
> wasn't clear to me. I thought that would be equivalent to a one-element
> list - and thereby define rownames. So that's good to know.
> >
> >The documentation could be more explicit - but it is probably more work
> to do that than just patch the code to honour a col.names argument. (At
> least I can't see a reason not to.)
> >
> >Thanks again!
> >:-)
> >
> >
> >
> >
> >> On Oct 28, 2023, at 14:24, avi.e.gr...@gmail.com wrote:
> >>
> >> Борис,
> >>
> >> Try this where you tell matrix the column names you want:
> >>
> >> nouns <- as.data.frame(
> >>  matrix(c(
> >>"gaggle",
> >>"geese",
> >>
> >>"dule",
> >>"doves",
> >>
> >>"wake",
> >>"vultures"
> >>  ),
> >>  ncol = 2,
> >>  byrow = TRUE,
> >>  dimnames=list(NULL, c("collective", "category"
> >>
> >> Result:
> >>
> >>> nouns
> >>  collective category
> >> 1 gagglegeese
> >> 2   duledoves
> >> 3   wake vultures
> >>
> >>
> >> The above simply names the columns earlier when creating the matrix.
> >>
> >> There are other ways and the way you tried LOOKS like it should work but
> >> fails for me with a message about it weirdly expecting three rows
> versus two
> >> which seems to confuse rows and columns. My version of R is recent and I
> >> wonder if there is a bug here.
> >>
> >> Consider whether you really need the data.frame created in a single
> >> statement or can you change the column names next as in:
> >>
> >>
> >>> nouns
> >>  V1   V2
> >> 1 gagglegeese
> >> 2   duledoves
> >> 3   wake vultures
> >>> colnames(nouns)
> >> [1] "V1" "V2"
> >>> colnames(nouns) <- c("collective", "category")
> >>> nouns
> >>  collective category
> >> 1 gagglegeese
> >> 2   duledoves
> >> 3   wake vultures
> >>
> >> Is there a known bug here or is the documentation wrong?
> >>
> >> -Original Message-
> >> From: R-help  On Behalf Of Boris Steipe
> >> Sent: Saturday, October 28, 2023 1:54 PM
> >> To: R. Mailing List 
> >> Subject: [R] col.names in as.data.frame() ?
> >>
> >> I have been trying to create a data frame from some structured text in a
> >> single expression. Reprex:
> >>
> >> nouns <- as.data.frame(
> >>  matrix(c(
> >>"gaggle",
> >>"geese",
> >>
> >>"dule",
> >>"doves",
> >>
> >>"wake",
> >>"vultures"
> >>  ), ncol = 2, byrow = TRUE),
> >>  col.names = c("collective", "category")
> >> )
> >>
> >> But ... :
> >>
> >>> str(nouns)
> >> 'data.frame': 3 obs. of  2 variables:
> >> $ V1: chr  "gaggle" "dule" "wake"
> >> $ V2: chr  "geese" "doves" "vultures"
> >>
> >> i.e. the col.names argument does nothing. From my reading of
> ?as.data.frame,
> >> my example should have worked.
> >>
> >> I know how to get the required result with colnames(), but I would like
> to
> >> understand why the idiom as written didn't work, and how I could have
> known
> >> that from the help file.
> >>
> >>
> >> Thanks!
> >> Boris
> >>
> >> __
> >> 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.
>
> --
> Sent from my phone. Please excuse my brevity.
>
> __
> 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

Re: [R] Plot for 10 years extrapolation

2023-10-28 Thread varin sacha via R-help
Dear Rui,

I really thank you a lot for your precious R help. It is exactly what I was 
trying to do! Once more, many thanks!

Best,
Sacha






Le vendredi 27 octobre 2023 à 09:36:18 UTC+2, Rui Barradas 
 a écrit : 





Às 19:23 de 26/10/2023, varin sacha via R-help escreveu:
> Dear R-Experts,
> 
> Here below my R code working but I don't know how to complete/finish my R 
> code to get the final plot with the extrapolation for the10 more years.
> 
> Indeed, I try to extrapolate my data with a linear fit over the next 10 
> years. So I create a date sequence for the next 10 years and store as a 
> dataframe to make the prediction possible.
> Now, I am trying to get the plot with the actual data (from year 2004 to 
> 2018) and with the 10 more years extrapolation.
> 
> Thanks for your help.
> 
> 
> date <-as.Date(c("2018-12-31", "2017-12-31", "2016-12-31", "2015-12-31", 
> "2014-12-31", "2013-12-31", "2012-12-31", "2011-12-31", "2010-12-31", 
> "2009-12-31", "2008-12-31", "2007-12-31", "2006-12-31", "2005-12-31", 
> "2004-12-31"))
>  
> value <-c(15348, 13136, 11733, 10737, 15674, 11098, 13721, 13209, 11099, 
> 10087, 14987, 11098, 13421, 9023, 12098)
>  
> model <- lm(value~date)
>  
> plot(value~date ,col="grey",pch=20,cex=1.5,main="Plot")
> abline(model,col="darkorange",lwd=2)
>  
> dfuture <- data.frame(date=seq(as.Date("2019-12-31"), by="1 year", 
> length.out=10))
>  
> predict(model,dfuture,interval="prediction")
> 
> 
> __
> 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.
Hello,

Here is a way with base R graphics. Explained in the code comments.




date <-as.Date(c("2018-12-31", "2017-12-31", "2016-12-31",
                  "2015-12-31", "2014-12-31", "2013-12-31",
                  "2012-12-31", "2011-12-31", "2010-12-31",
                  "2009-12-31", "2008-12-31", "2007-12-31",
                  "2006-12-31", "2005-12-31", "2004-12-31"))

value <-c(15348, 13136, 11733, 10737, 15674, 11098, 13721, 13209,
          11099, 10087, 14987, 11098, 13421, 9023, 12098)

model <- lm(value ~ date)

dfuture <- data.frame(date = seq(as.Date("2019-12-31"), by="1 year", 
length.out=10))



predfuture <- predict(model, dfuture, interval="prediction")
dfuture <- cbind(dfuture, predfuture)

# start the plot with the required x and y limits
xlim <- range(c(date, dfuture$date))
ylim <- range(c(value, dfuture$fit))

plot(value ~ date, col="grey", pch=20, cex=1.5, main="Plot"
      , xlim = xlim, ylim = ylim)

# abline extends the fitted line past the x value (date)
# limit making the next ten years line ugly and not even
# completely overplotting the abline drawn line
abline(model, col="darkorange", lwd=2)
lines(fit ~ date, dfuture
      # , lty = "dashed"
      , lwd=2
      , col = "black")

# if lines() is used for both the interpolated and extrapolated
# values you will have a gap between both fitted and predicted lines
# but it is closer to what you want

# get the fitted values first (interpolated values)
ypred <- predict(model)


plot(value ~ date, col="grey", pch=20, cex=1.5, main="Plot"

      , xlim = xlim, ylim = ylim)

# plot the interpolated values
lines(ypred ~ date, col="darkorange", lwd = 2)
# and now the extrapolated values
# I use normal orange to make the difference more obvious
lines(fit ~ date, dfuture, lty = "dashed", lwd=2, col = "orange")



Hope this helps,

Rui Barradas


-- 
Este e-mail foi analisado pelo software antivírus AVG para verificar a presença 
de vírus.
www.avg.com

__
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] [Tagged] Re: col.names in as.data.frame() ?

2023-10-28 Thread Boris Steipe
Ah - that's an excellent point. Thanks.



> On Oct 28, 2023, at 14:54, Jeff Newmiller  wrote:
> 
> as.data.frame is a _converter_, while data.frame is a _constructor_.  
> Changing the object contents is not what a conversion is for.
> 
> On October 28, 2023 11:39:22 AM PDT, Boris Steipe  
> wrote:
>> Thanks Duncan and Avi!
>> 
>> That you could use NULL in a matrix() dimnames = list(...) argument wasn't 
>> clear to me. I thought that would be equivalent to a one-element list - and 
>> thereby define rownames. So that's good to know.
>> 
>> The documentation could be more explicit - but it is probably more work to 
>> do that than just patch the code to honour a col.names argument. (At least I 
>> can't see a reason not to.)
>> 
>> Thanks again!
>> :-)
>> 
>> 
>> 
>> 
>>> On Oct 28, 2023, at 14:24, avi.e.gr...@gmail.com wrote:
>>> 
>>> Борис,
>>> 
>>> Try this where you tell matrix the column names you want:
>>> 
>>> nouns <- as.data.frame(
>>> matrix(c(
>>>   "gaggle",
>>>   "geese",
>>> 
>>>   "dule",
>>>   "doves",
>>> 
>>>   "wake",
>>>   "vultures"
>>> ), 
>>> ncol = 2, 
>>> byrow = TRUE, 
>>> dimnames=list(NULL, c("collective", "category"
>>> 
>>> Result:
>>> 
 nouns
>>> collective category
>>> 1 gagglegeese
>>> 2   duledoves
>>> 3   wake vultures
>>> 
>>> 
>>> The above simply names the columns earlier when creating the matrix.
>>> 
>>> There are other ways and the way you tried LOOKS like it should work but
>>> fails for me with a message about it weirdly expecting three rows versus two
>>> which seems to confuse rows and columns. My version of R is recent and I
>>> wonder if there is a bug here.
>>> 
>>> Consider whether you really need the data.frame created in a single
>>> statement or can you change the column names next as in:
>>> 
>>> 
 nouns
>>> V1   V2
>>> 1 gagglegeese
>>> 2   duledoves
>>> 3   wake vultures
 colnames(nouns)
>>> [1] "V1" "V2"
 colnames(nouns) <- c("collective", "category")
 nouns
>>> collective category
>>> 1 gagglegeese
>>> 2   duledoves
>>> 3   wake vultures
>>> 
>>> Is there a known bug here or is the documentation wrong?
>>> 
>>> -Original Message-
>>> From: R-help  On Behalf Of Boris Steipe
>>> Sent: Saturday, October 28, 2023 1:54 PM
>>> To: R. Mailing List 
>>> Subject: [R] col.names in as.data.frame() ?
>>> 
>>> I have been trying to create a data frame from some structured text in a
>>> single expression. Reprex:
>>> 
>>> nouns <- as.data.frame(
>>> matrix(c(
>>>   "gaggle",
>>>   "geese",
>>> 
>>>   "dule",
>>>   "doves",
>>> 
>>>   "wake",
>>>   "vultures"
>>> ), ncol = 2, byrow = TRUE),
>>> col.names = c("collective", "category")
>>> )
>>> 
>>> But ... :
>>> 
 str(nouns)
>>> 'data.frame': 3 obs. of  2 variables:
>>> $ V1: chr  "gaggle" "dule" "wake"
>>> $ V2: chr  "geese" "doves" "vultures"
>>> 
>>> i.e. the col.names argument does nothing. From my reading of ?as.data.frame,
>>> my example should have worked.
>>> 
>>> I know how to get the required result with colnames(), but I would like to
>>> understand why the idiom as written didn't work, and how I could have known
>>> that from the help file.
>>> 
>>> 
>>> Thanks!
>>> Boris
>>> 
>>> __
>>> 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.
> 
> -- 
> Sent from my phone. Please excuse my brevity.

__
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] [Tagged] Re: col.names in as.data.frame() ?

2023-10-28 Thread Jeff Newmiller via R-help
as.data.frame is a _converter_, while data.frame is a _constructor_.  Changing 
the object contents is not what a conversion is for.

On October 28, 2023 11:39:22 AM PDT, Boris Steipe  
wrote:
>Thanks Duncan and Avi!
>
>That you could use NULL in a matrix() dimnames = list(...) argument wasn't 
>clear to me. I thought that would be equivalent to a one-element list - and 
>thereby define rownames. So that's good to know.
>
>The documentation could be more explicit - but it is probably more work to do 
>that than just patch the code to honour a col.names argument. (At least I 
>can't see a reason not to.)
>
>Thanks again!
>:-)
>
>
>
>
>> On Oct 28, 2023, at 14:24, avi.e.gr...@gmail.com wrote:
>> 
>> Борис,
>> 
>> Try this where you tell matrix the column names you want:
>> 
>> nouns <- as.data.frame(
>>  matrix(c(
>>"gaggle",
>>"geese",
>> 
>>"dule",
>>"doves",
>> 
>>"wake",
>>"vultures"
>>  ), 
>>  ncol = 2, 
>>  byrow = TRUE, 
>>  dimnames=list(NULL, c("collective", "category"
>> 
>> Result:
>> 
>>> nouns
>>  collective category
>> 1 gagglegeese
>> 2   duledoves
>> 3   wake vultures
>> 
>> 
>> The above simply names the columns earlier when creating the matrix.
>> 
>> There are other ways and the way you tried LOOKS like it should work but
>> fails for me with a message about it weirdly expecting three rows versus two
>> which seems to confuse rows and columns. My version of R is recent and I
>> wonder if there is a bug here.
>> 
>> Consider whether you really need the data.frame created in a single
>> statement or can you change the column names next as in:
>> 
>> 
>>> nouns
>>  V1   V2
>> 1 gagglegeese
>> 2   duledoves
>> 3   wake vultures
>>> colnames(nouns)
>> [1] "V1" "V2"
>>> colnames(nouns) <- c("collective", "category")
>>> nouns
>>  collective category
>> 1 gagglegeese
>> 2   duledoves
>> 3   wake vultures
>> 
>> Is there a known bug here or is the documentation wrong?
>> 
>> -Original Message-
>> From: R-help  On Behalf Of Boris Steipe
>> Sent: Saturday, October 28, 2023 1:54 PM
>> To: R. Mailing List 
>> Subject: [R] col.names in as.data.frame() ?
>> 
>> I have been trying to create a data frame from some structured text in a
>> single expression. Reprex:
>> 
>> nouns <- as.data.frame(
>>  matrix(c(
>>"gaggle",
>>"geese",
>> 
>>"dule",
>>"doves",
>> 
>>"wake",
>>"vultures"
>>  ), ncol = 2, byrow = TRUE),
>>  col.names = c("collective", "category")
>> )
>> 
>> But ... :
>> 
>>> str(nouns)
>> 'data.frame': 3 obs. of  2 variables:
>> $ V1: chr  "gaggle" "dule" "wake"
>> $ V2: chr  "geese" "doves" "vultures"
>> 
>> i.e. the col.names argument does nothing. From my reading of ?as.data.frame,
>> my example should have worked.
>> 
>> I know how to get the required result with colnames(), but I would like to
>> understand why the idiom as written didn't work, and how I could have known
>> that from the help file.
>> 
>> 
>> Thanks!
>> Boris
>> 
>> __
>> 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.

-- 
Sent from my phone. Please excuse my brevity.

__
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] col.names in as.data.frame() ?

2023-10-28 Thread Boris Steipe
Thanks Duncan and Avi!

That you could use NULL in a matrix() dimnames = list(...) argument wasn't 
clear to me. I thought that would be equivalent to a one-element list - and 
thereby define rownames. So that's good to know.

The documentation could be more explicit - but it is probably more work to do 
that than just patch the code to honour a col.names argument. (At least I can't 
see a reason not to.)

Thanks again!
:-)




> On Oct 28, 2023, at 14:24, avi.e.gr...@gmail.com wrote:
> 
> Борис,
> 
> Try this where you tell matrix the column names you want:
> 
> nouns <- as.data.frame(
>  matrix(c(
>"gaggle",
>"geese",
> 
>"dule",
>"doves",
> 
>"wake",
>"vultures"
>  ), 
>  ncol = 2, 
>  byrow = TRUE, 
>  dimnames=list(NULL, c("collective", "category"
> 
> Result:
> 
>> nouns
>  collective category
> 1 gagglegeese
> 2   duledoves
> 3   wake vultures
> 
> 
> The above simply names the columns earlier when creating the matrix.
> 
> There are other ways and the way you tried LOOKS like it should work but
> fails for me with a message about it weirdly expecting three rows versus two
> which seems to confuse rows and columns. My version of R is recent and I
> wonder if there is a bug here.
> 
> Consider whether you really need the data.frame created in a single
> statement or can you change the column names next as in:
> 
> 
>> nouns
>  V1   V2
> 1 gagglegeese
> 2   duledoves
> 3   wake vultures
>> colnames(nouns)
> [1] "V1" "V2"
>> colnames(nouns) <- c("collective", "category")
>> nouns
>  collective category
> 1 gagglegeese
> 2   duledoves
> 3   wake vultures
> 
> Is there a known bug here or is the documentation wrong?
> 
> -Original Message-
> From: R-help  On Behalf Of Boris Steipe
> Sent: Saturday, October 28, 2023 1:54 PM
> To: R. Mailing List 
> Subject: [R] col.names in as.data.frame() ?
> 
> I have been trying to create a data frame from some structured text in a
> single expression. Reprex:
> 
> nouns <- as.data.frame(
>  matrix(c(
>"gaggle",
>"geese",
> 
>"dule",
>"doves",
> 
>"wake",
>"vultures"
>  ), ncol = 2, byrow = TRUE),
>  col.names = c("collective", "category")
> )
> 
> But ... :
> 
>> str(nouns)
> 'data.frame': 3 obs. of  2 variables:
> $ V1: chr  "gaggle" "dule" "wake"
> $ V2: chr  "geese" "doves" "vultures"
> 
> i.e. the col.names argument does nothing. From my reading of ?as.data.frame,
> my example should have worked.
> 
> I know how to get the required result with colnames(), but I would like to
> understand why the idiom as written didn't work, and how I could have known
> that from the help file.
> 
> 
> Thanks!
> Boris
> 
> __
> 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.


Re: [R] col.names in as.data.frame() ?

2023-10-28 Thread avi.e.gross
Борис,

Try this where you tell matrix the column names you want:

nouns <- as.data.frame(
  matrix(c(
"gaggle",
"geese",

"dule",
"doves",

"wake",
"vultures"
  ), 
  ncol = 2, 
  byrow = TRUE, 
  dimnames=list(NULL, c("collective", "category"

Result:

> nouns
  collective category
1 gagglegeese
2   duledoves
3   wake vultures


The above simply names the columns earlier when creating the matrix.

There are other ways and the way you tried LOOKS like it should work but
fails for me with a message about it weirdly expecting three rows versus two
which seems to confuse rows and columns. My version of R is recent and I
wonder if there is a bug here.

Consider whether you really need the data.frame created in a single
statement or can you change the column names next as in:


> nouns
  V1   V2
1 gagglegeese
2   duledoves
3   wake vultures
> colnames(nouns)
[1] "V1" "V2"
> colnames(nouns) <- c("collective", "category")
> nouns
  collective category
1 gagglegeese
2   duledoves
3   wake vultures

Is there a known bug here or is the documentation wrong?

-Original Message-
From: R-help  On Behalf Of Boris Steipe
Sent: Saturday, October 28, 2023 1:54 PM
To: R. Mailing List 
Subject: [R] col.names in as.data.frame() ?

I have been trying to create a data frame from some structured text in a
single expression. Reprex:

nouns <- as.data.frame(
  matrix(c(
"gaggle",
"geese",

"dule",
"doves",

"wake",
"vultures"
  ), ncol = 2, byrow = TRUE),
  col.names = c("collective", "category")
)

But ... :

> str(nouns)
'data.frame':   3 obs. of  2 variables:
 $ V1: chr  "gaggle" "dule" "wake"
 $ V2: chr  "geese" "doves" "vultures"

i.e. the col.names argument does nothing. From my reading of ?as.data.frame,
my example should have worked.

I know how to get the required result with colnames(), but I would like to
understand why the idiom as written didn't work, and how I could have known
that from the help file.


Thanks!
Boris

__
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.


Re: [R] weights vs. offset (negative binomial regression)

2023-10-28 Thread Ben Bolker
  Using an offset of log(Effort) as in your second model is the more 
standard way to approach this problem; it corresponds to assuming that 
catch is strictly proportional to effort. Adding log(Effort) as a 
covariate (as illustrated below) tests whether a power-law model (catch 
propto (Effort)^(b+1), b!=0) is a better description of the data.  (In 
this case it is not, although the confidence intervals on b are very 
wide, indicating that we have very little information -- this is not 
surprising since the proportional range of effort is very small 
(246-258) in this data set.


  In general you should *not* check overdispersion of the raw data 
(i.e., the *marginal distribution* of the data, you should check 
overdispersion of a fitted (e.g. Poisson) model, as below.


  cheers
   Ben Bolker


edata <- data.frame(Catch, Effort, xx1, xx2, xx3)

## graphical exploration

library(ggplot2); theme_set(theme_bw())
library(tidyr)
edata_long <- edata |> pivot_longer(names_to="var", cols =-c("Catch", 
"Effort"))

ggplot(edata_long, aes(value, Catch)) +
geom_point(alpha = 0.2, aes(size = Effort)) +
facet_wrap(~var, scale="free_x") +
geom_smooth(method = "glm", method.args = list(family = 
"quasipoisson"))

#

library(MASS)
g1 <- glm.nb(Catch~xx1+xx2+xx3+offset(log(Effort)), data=edata)
g2 <- update(g1, . ~ . + log(Effort))
g0 <- glm(Catch~xx1+xx2+xx3+offset(log(Effort)), data=edata,
  family = poisson)
performance::check_overdispersion(g0)
summary(g1)
summary(g2)
options(digits = 3)
confint(g2)
summary(g1)



On 2023-10-28 3:30 a.m., 유준택 wrote:

Colleagues,



I have a dataset that includes five variables.

- Catch: the catch number counted in some species (ind.)

- Effort: fishing effort (the number of fishing vessels)

- xx1, xx2, xx3: some environmental factors

As an overdispersion test on the “Catch” variable, I modeled with negative
binomial distribution using a GLM. The “Effort” variable showed a gradually
decreasing trend during the study period. I was able to get the results I
wanted when considered “Effort” function as a weights function in the
negative binomial regression as follows:



library(qcc)

Catch=c(25,2,7,6,75,5,1,4,66,15,9,25,40,8,7,4,36,11,1,14,141,9,74,38,126,3)

Effort=c(258,258,258,258,258,258,258,254,252,252,252,252,252,252,252,252,252,252,252,248,246,246,246,246,246,246)

xx1=c(0.8,0.5,1.2,0.5,1.1,1.1,1.0,0.6,0.9,0.5,1.2,0.6,1.2,0.7,1.0,0.6,1.6,0.7,0.8,0.6,1.7,0.9,1.1,0.5,1.4,0.5)

xx2=c(1.7,1.6,2.7,2.6,1.5,1.5,2.8,2.5,1.7,1.9,2.2,2.4,1.6,1.4,3.0,2.4,1.4,1.5,2.2,2.3,1.7,1.7,1.9,1.9,1.4,1.4)

xx3=c(188,40,2,10,210,102,117,14,141,28,48,15,220,115,10,14,320,20,3,10,400,150,145,160,460,66)

#

edata <- data.frame(Catch, Effort, xx1, xx2, xx3)

#

qcc.overdispersion.test(edata$Catch, type="poisson")

#

summary(glm.nb(Catch~xx1+xx2+xx3, weights=Effort, data=edata))

summary(glm.nb(Catch~xx1+xx2+xx3+offset(log(Effort)), data=edata))



I am not sure the application of the weights function to the negative
binomial regression is correct. Also I wonder if there is a better way
doing this. Can anyone help?

[[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-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] col.names in as.data.frame() ?

2023-10-28 Thread Duncan Murdoch
Sent a slightly shorter version of this to your email, this one is to 
the list:


On 28/10/2023 1:54 p.m., Boris Steipe wrote:
> > I have been trying to create a data frame from some structured text 
in a single expression. Reprex:

> >
> > nouns <- as.data.frame(
> >matrix(c(
> >  "gaggle",
> >  "geese",
> >
> >  "dule",
> >  "doves",
> >
> >  "wake",
> >  "vultures"
> >), ncol = 2, byrow = TRUE),
> >col.names = c("collective", "category")
> > )
> >

You are calling it on a matrix, so the as.data.frame.matrix method is
what matters.  It doesn't have a col.names argument, only row.names.

The docs are vague about what ... does, but if you look at the method,
you can see any unnamed arguments are ignored completely.

If you want to specify the column names in a single call, you'll need to 
put them in the matrix, e.g.


as.data.frame(
  matrix(c(
"gaggle",
 "geese",

 "dule",
 "doves",

 "wake",
 "vultures"
   ), ncol = 2, byrow = TRUE,
   dimnames =  list(NULL, c("collective", "category"))
  )
)


Duncan Murdoch

__
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] col.names in as.data.frame() ?

2023-10-28 Thread Boris Steipe
I have been trying to create a data frame from some structured text in a single 
expression. Reprex:

nouns <- as.data.frame(
  matrix(c(
"gaggle",
"geese",

"dule",
"doves",

"wake",
"vultures"
  ), ncol = 2, byrow = TRUE),
  col.names = c("collective", "category")
)

But ... :

> str(nouns)
'data.frame':   3 obs. of  2 variables:
 $ V1: chr  "gaggle" "dule" "wake"
 $ V2: chr  "geese" "doves" "vultures"

i.e. the col.names argument does nothing. From my reading of ?as.data.frame, my 
example should have worked.

I know how to get the required result with colnames(), but I would like to 
understand why the idiom as written didn't work, and how I could have known 
that from the help file.


Thanks!
Boris

__
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 Reformat a dataframe

2023-10-28 Thread jim holtman
You can also use the pivot_longer to do it:

library(tidyverse)

input <- structure(list(...1 = c(92.9925354, 76.0024254, 44.99547465,
28.00536465, 120.0068103, 31.9980405, 85.0071837, 40.1532933,
19.3120917, 113.12581575, 28.45843425, 114.400074, 143.925,
46.439634, 20.7845679, 50.82874575, 36.9818061, 44.6273556, 40.57804605,
:
:
:
 row.names = c(NA, -126L), class = "data.frame")

> input$row <- seq(nrow(input))  # add row number for reference> head(input)
>...1 ...2 ...3 ...4 ...5 ...6 ...7 ...8
1  92.99254 34.99963 24.04101 43.01330 53.00914 62.01390 91.01036 88.99986
2  76.00243 22.00219 22.00219 25.00378 44.99547 60.99449 63.00499 92.99254
3  44.99547 22.99328 15.00793 15.99902 38.00121 44.00438 62.01390 79.99510
4  28.00536 19.00061 12.99743 12.99743 44.00438 49.01647 55.95410 69.00816
5 120.00681 35.99072 22.99328 47.99706 60.00341 62.01390 60.00341 66.00658
6  31.99804 23.95606 13.98852 15.99902 38.99230 54.99132 88.00877 89.99095
  ...9...10 ...11 ...12 row
1 54.00023 75.01134 111.99314  49.01647   1
2 68.01707 75.01134  82.99669  63.99608   2
3 60.99449 91.01036  84.01609  65.01549   3
4 82.99669 78.01292 135.01474  85.99827   4
5 66.99767 91.01036  88.99986  51.98974   5
6 79.00401 78.01292 113.52225 155.00644   6> > x <-
pivot_longer(input,names_to = "col",cols=1:12)> head(x, 20)# A tibble:
20 × 3
 row col   value
  1 1 ...1   93.0 2 1 ...2   35.0 3 1
...3   24.0 4 1 ...4   43.0 5 1 ...5   53.0 6 1 ...6
62.0 7 1 ...7   91.0 8 1 ...8   89.0 9 1 ...9   54.010
1 ...10  75.011 1 ...11 112. 12 1 ...12  49.013 2 ...1
76.014 2 ...2   22.015 2 ...3   22.016 2 ...4   25.017
2 ...5   45.018 2 ...6   61.019 2 ...7   63.020 2 ...8
93.0

>

Thanks

Jim Holtman
*Data Munger Guru*


*What is the problem that you are trying to solve?Tell me what you want to
do, not how you want to do it.*


On Fri, Oct 27, 2023 at 10:41 PM Paul Bernal  wrote:

> Hi Iris,
>
> Thank you so much for your valuable feedback. I wonder why your code gives
> you 1512 rows, given that the original structure has 12 columns and 126
> rows, so I would expect (125*12)+ 9=1,509 total rows.
>
> Cheers,
> Paul
> El El vie, 27 de oct. de 2023 a la(s) 10:40 p. m., Iris Simmons <
> ikwsi...@gmail.com> escribió:
>
> > You are not getting the structure you want because the indexes are
> > wrong. They should be something more like this:
> >
> > i <- 0
> > for (row in 1:nrow(alajuela_df)){
> >   for (col in 1:ncol(alajuela_df)){
> > i <- i + 1
> > df[i,1]=alajuela_df[row,col]
> >   }
> > }
> >
> > but I think what you are doing can be written much shorter and will run
> > faster:
> >
> > ## transpose here matches your original code
> > df <- data.frame(aportes_alajuela = c(t(alajuela_df)))
> >
> > ## but if you do not want to transpose, then do this
> > df <- data.frame(aportes_alajuela = unlist(alajuela_df, use.names =
> FALSE))
> >
> > However, you said you expected 1509 observations, but this gives you
> > 1512 observations. If you want to exclude the 3 NA observations, do
> > something like:
> >
> > df <- df[!is.na(df$aportes_alajuela), , drop = FALSE]
> >
> > On Fri, Oct 27, 2023 at 11:14 PM Paul Bernal 
> > wrote:
> > >
> > > Dear friends,
> > >
> > > I have the following dataframe:
> > > dim(alajuela_df)
> > > [1] 126  12
> > >
> > > dput(alajuela_df)
> > > structure(list(...1 = c(92.9925354, 76.0024254, 44.99547465,
> > > 28.00536465, 120.0068103, 31.9980405, 85.0071837, 40.1532933,
> > > 19.3120917, 113.12581575, 28.45843425, 114.400074, 143.925,
> > > 46.439634, 20.7845679, 50.82874575, 36.9818061, 44.6273556,
> 40.57804605,
> > > 30.38398005, 47.94042705, 36.38715225, 28.06199835, 28.4867511,
> > > 122.86681215, 56.4071652, 35.9057658, 52.669341, 24.94714485,
> > > 54.4249857, 61.164396, 47.88379335, 30.582198, 26.051502, 43.041612,
> > > 64.59073485, 51.6499344, 78.8202902886201, 35.2390173175627,
> > > 82.2394568898745, 47.760850180466, 54.3654763342294, 49.4878058854839,
> > > 32.8813266149642, 38.9301880693548, 51.9506275455197, 55.4404001992832,
> > > 50.7979761262545, 37.1198211413082, 36.9144309425627, 33.7829493281362,
> > > 32.8647492475806, 42.892686344, 63.9814428257048, 39.219040238172,
> > > 88.7557324417563, 42.0964144925627, 129.15973304991, 117.872998635484,
> > > 35.4004098300179, 83.4102757505377, 38.6443638074373, 100.491764259319,
> > > 40.219162961828, 35.901029409319, 85.2814714674731, 26.5821299974014,
> > > 33.0141992892473, 52.4006692386201, 62.7450310643369, 33.3732853655914,
> > > 36.4616827405018, 146.501706130466, 59.9869292767025, 132.659282967294,
> > > 29.9692075517921, 45.8436438112007, 29.3028312143369, 49.9042699517921,
> > > 27.467868886, 29.8483957580645, 26.1192323867384, 62.1371491517921,
> > > 53.92489050681, 64.5840869873656, 28.4476420455197, 52.9893180218638,
> > > 36.4730500781362, 40.8595060662186, 33.4571194806452, 35.6201445262545,
> > > 

Re: [R] How to Reformat a dataframe

2023-10-28 Thread avi.e.gross
Paul,

I have snipped away your long message and want to suggest another approach
or way of thinking to consider.

You have received other good suggestions and I likely would have used
something like that, probably within the dplyr/tidyverse but consider
something simpler.

You seem to be viewing a data.frame as similar to a matrix that you want to
reformat. There are similarities but a data.frame is also different. A
Matrix actually may be the right way for you to deal with your data. Can you
read it in as a matrix or must it be a data.frame? 

The thing about a matrix is that underneath, it is just a linear vector
which you really seem to want. All your columns seem to be the same kind of
numeric and perhaps the order does not matter whether it is row major or
column major. So consider my smaller example. I am making a data.frame that
is smaller for illustration:

> small <- data.frame(A=1:4, B=5:8, C=9:12)
> small
  A B  C
1 1 5  9
2 2 6 10
3 3 7 11
4 4 8 12

Now I am making it a matrix and keeping the columns the same:

> small.mat <- as.matrix(small)
> small.mat
 A B  C
[1,] 1 5  9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12

This can be linearized into a vector in many ways such as this:

> small.vec <- as.vector(small.mat)
> small.vec
 [1]  1  2  3  4  5  6  7  8  9 10 11 12

You can make that into a data.frame if you like:

> revised <- data.frame(colname=small.vec)
> revised
   colname
11
22
33
44
55
66
77
88
99
10  10
11  11
12  12

Of course, the above can be combined into more of a one-liner or made more
efficient. But in some cases, if you know the exact details of your
data.frame, you can spell out a way to combine the columns trivially. In my
example, I have three columns that can simply be concatenated into a vector
like so:

> small.onecol <- data.frame(onecol=c(small$A, small$B, small$C))
> small.onecol
   onecol
1   1
2   2
3   3
4   4
5   5
6   6
7   7
8   8
9   9
10 10
11 11
12 12

This is not a generalized solution but is simple enough even with the number
of columns you have. You are simply consolidating the vectors into one
bigger one. If you want to connect many, there are shorter loops that can do
it as in:

> cols <- colnames(small)
> cols
[1] "A" "B" "C"
> 
> new <- vector(mode="numeric", length=0)
> for (col in cols) {
+   new <- append(new, small[[col]])
+ }
> new
 [1]  1  2  3  4  5  6  7  8  9 10 11 12
> 
> new.df <- data.frame(newname=new)
> new.df
   newname
11
22
33
44
55
66
77
88
99
10  10
11  11
12  12

The number of ways to do what you want is huge. You can pick a way that
makes more sense to you, especially the ones others have supplied, or one
that seems more efficient. As noted, all methods may also need to deal with
your NA issue at some stage.

__
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 Reformat a dataframe

2023-10-28 Thread Rui Barradas

Às 04:13 de 28/10/2023, Paul Bernal escreveu:

Dear friends,

I have the following dataframe:
dim(alajuela_df)
[1] 126  12

dput(alajuela_df)
structure(list(...1 = c(92.9925354, 76.0024254, 44.99547465,
28.00536465, 120.0068103, 31.9980405, 85.0071837, 40.1532933,
19.3120917, 113.12581575, 28.45843425, 114.400074, 143.925,
46.439634, 20.7845679, 50.82874575, 36.9818061, 44.6273556, 40.57804605,
30.38398005, 47.94042705, 36.38715225, 28.06199835, 28.4867511,
122.86681215, 56.4071652, 35.9057658, 52.669341, 24.94714485,
54.4249857, 61.164396, 47.88379335, 30.582198, 26.051502, 43.041612,
64.59073485, 51.6499344, 78.8202902886201, 35.2390173175627,
82.2394568898745, 47.760850180466, 54.3654763342294, 49.4878058854839,
32.8813266149642, 38.9301880693548, 51.9506275455197, 55.4404001992832,
50.7979761262545, 37.1198211413082, 36.9144309425627, 33.7829493281362,
32.8647492475806, 42.892686344, 63.9814428257048, 39.219040238172,
88.7557324417563, 42.0964144925627, 129.15973304991, 117.872998635484,
35.4004098300179, 83.4102757505377, 38.6443638074373, 100.491764259319,
40.219162961828, 35.901029409319, 85.2814714674731, 26.5821299974014,
33.0141992892473, 52.4006692386201, 62.7450310643369, 33.3732853655914,
36.4616827405018, 146.501706130466, 59.9869292767025, 132.659282967294,
29.9692075517921, 45.8436438112007, 29.3028312143369, 49.9042699517921,
27.467868886, 29.8483957580645, 26.1192323867384, 62.1371491517921,
53.92489050681, 64.5840869873656, 28.4476420455197, 52.9893180218638,
36.4730500781362, 40.8595060662186, 33.4571194806452, 35.6201445262545,
47.5373940344086, 62.5177012273297, 31.970248036, 29.6637272685484,
49.9578249978495, 26.1936613831541, 30.8051128442652, 153.165249601165,
36.7652008047491, 14.3854334390681, 55.9930862447133, 87.5882628859319,
88.414763059767, 58.6046644034946, 20.5301898890681, 39.0037205346774,
67.5971386298632, 25.1295804696237, 32.7864973072581, 59.5662701215054,
56.3503435331631, 25.2768483884409, 120.187620533964, 51.3699688189953,
42.6889983487868, 34.5183987020052, 32.9513272936733, 20.6141437461215,
43.4197954932283, 107.258904254306, 23.7548470509806, 48.4457090643702,
72.3249287581108, 32.7272590911596, 48.6115621286089), ...2 = c(34.9996266,
22.00219245, 22.9932822, 19.00060635, 35.99071635, 23.9560551,
44.99547465, 22.00219245, 17.35822905, 38.9639856, 16.4804067,
93.87035775, 81.2693595, 65.4119235, 14.6114946, 33.32893245,
23.3330844, 66.68618175, 29.1097218, 18.77407155, 28.1469489,
20.64298365, 15.31941585, 25.79665035, 39.5869563, 35.9057658,
28.11863205, 27.09922545, 18.03783345, 52.3295388, 47.99706075,
21.88892505, 18.8590221, 17.64139755, 29.2796229, 27.750513,
26.9576412, 36.8932410314484, 19.1194611744253, 37.5166237491071,
37.9625421619439, 23.444984650496, 37.4408609345785, 43.1033210084325,
28.1252430606151, 38.4216392645833, 41.0532302780651, 34.4822260610119,
27.8096188059524, 25.9720387760913, 19.5989670673372, 21.1366557174603,
34.0091174203373, 111.149568174301, 24.5438738482759, 62.923693488747,
36.056976280754, 36.934798630754, 47.0895472967433, 23.0987961767857,
45.1991799260913, 23.0274422095238, 26.8716961246169, 21.5009916962302,
25.315076619246, 41.2098751296627, 15.6537752838123, 20.1723471152778,
31.7437694203373, 34.6486999232143, 27.6318028338123, 23.9637897951389,
50.6646878095238, 33.3208231933532, 38.0673028746602, 19.9469359998016,
27.0503264451389, 19.838575356, 29.0062549955939, 17.231889453373,
25.332044256, 17.651735447619, 37.12988188841, 37.7982565469246,
33.1890524548611, 15.4283008803571, 33.5040593288314, 24.9726150325397,
29.0612348127976, 30.2629592267857, 32.8065534719349, 43.3676303365079,
39.19334780424, 23.9252474159722, 15.8641806903257, 27.51501019685,
21.5861832758362, 18.5136698379577, 54.9166790494253, 29.2865335598214,
12.8130786005824, 46.2078437446569, 37.9351942968391, 31.8758398080357,
25.483024048, 17.4062173291544, 19.1528843310516, 37.2160194109127,
25.3577400835684, 21.3381885368056, 37.5842732179795, 62.4856267757438,
19.2393143990079, 49.4806272500855, 29.5274325254668, 18.7819282827049,
19.5089694997422, 25.524181172185, 15.9275217745507, 25.461470312583,
37.4026236465872, 14.6120197959536, 26.5773978462589, 27.6541154512847,
22.5193597452548, 27.9919830179818), ...3 = c(24.04100565, 22.00219245,
15.0079305, 12.99743415, 22.9932822, 13.9885239, 27.0142749,
16.3671393, 12.2895129, 28.68496905, 16.56535725, 32.70596175,
46.01488125, 24.94714485, 10.8170367, 19.68021075, 15.0079305,
26.41962105, 21.9738756, 15.71585175, 21.26595435, 13.28060265,
11.6099085, 15.5742675, 25.96655145, 20.5013994, 15.99902025,
18.03783345, 11.07188835, 32.59269435, 29.7326925, 15.82911915,
13.4788206, 22.11545985, 17.6130807, 22.3136778, 17.01842685,
24.8211230265233, 13.237941302509, 22.3448026530466, 26.816253605,
19.755485530466, 28.9524567137097, 35.9159180773958, 29.0510074712366,
29.6768770591437, 19.74645255681, 21.3046743438172, 18.2600040043011,
14.1888055630824, 13.2245440831541, 

Re: [R] How to Reformat a dataframe

2023-10-28 Thread Chris Evans via R-help

The tidyverse idiom looks very different but does what you want and I have come 
to like it.
What idiom of R one likes, for the mostly small datasets I handle, is largely a 
matter
of preferenceds for "readability", itself very personal.  Here's my tidyverse 
way of doing
what you wanted:

### start of code
library(tidyverse)
# tmpDF <- structure(list(...1 = c(92.9925354, 76.0024254, 44.99547465,
### I omitted the rest of reconstructing the dataframe for brevity, easy to 
reconstruct

tmpDF %>%
  pivot_longer(cols = everything()) -> tmpTibLong

tmpTibLong
### showed:
# # A tibble: 1,512 × 2
# name  value
#  
#   1 ...1   93.0
# 2 ...2   35.0
# 3 ...3   24.0
# 4 ...4   43.0
# 5 ...5   53.0
# 6 ...6   62.0
# 7 ...7   91.0
# 8 ...8   89.0
# 9 ...9   54.0
# 10 ...10  75.0
# # ℹ 1,502 more rows
# # ℹ Use `print(n = ...)` to see more rows

### as you don't want the missing values
tmpTibLong %>%
  drop_na()
### gave:
# # A tibble: 1,509 × 2
# name  value
#  
#   1 ...1   93.0
# 2 ...2   35.0
# 3 ...3   24.0
# 4 ...4   43.0
# 5 ...5   53.0
# 6 ...6   62.0
# 7 ...7   91.0
# 8 ...8   89.0
# 9 ...9   54.0
# 10 ...10  75.0
# # ℹ 1,499 more rows
# # ℹ Use `print(n = ...)` to see more rows
### end

Very best all,

Chris

On 28/10/2023 07:41, Paul Bernal wrote:


Hi Iris,

Thank you so much for your valuable feedback. I wonder why your code gives
you 1512 rows, given that the original structure has 12 columns and 126
rows, so I would expect (125*12)+ 9=1,509 total rows.

Cheers,
Paul
El El vie, 27 de oct. de 2023 a la(s) 10:40 p. m., Iris Simmons <
ikwsi...@gmail.com> escribió:


You are not getting the structure you want because the indexes are
wrong. They should be something more like this:

i <- 0
for (row in 1:nrow(alajuela_df)){
   for (col in 1:ncol(alajuela_df)){
 i <- i + 1
 df[i,1]=alajuela_df[row,col]
   }
}

but I think what you are doing can be written much shorter and will run
faster:

## transpose here matches your original code
df <- data.frame(aportes_alajuela = c(t(alajuela_df)))

## but if you do not want to transpose, then do this
df <- data.frame(aportes_alajuela = unlist(alajuela_df, use.names = FALSE))

However, you said you expected 1509 observations, but this gives you
1512 observations. If you want to exclude the 3 NA observations, do
something like:

df <- df[!is.na(df$aportes_alajuela), , drop = FALSE]

On Fri, Oct 27, 2023 at 11:14 PM Paul Bernal 
wrote:

Dear friends,

I have the following dataframe:
dim(alajuela_df)
[1] 126  12


[dput snipped]



What I want to do is, instead of having 12 observations  by row, I want

to

have one observation by row. I want to have a single column with 1509
observations instead of 126 rows with 12 columns per row.

I tried the following:
df = data.frame(matrix(nrow = Length, ncol = 1))
colnames(df) = c("aportes_alajuela")



for (row in 1:nrow(alajuela_df)){
   for (col in 1:ncol(alajuela_df)){
 df[i,1]=alajuela_df[i,j]
   }
}

But I am not getting the data in the structure I want.

Any help will be greatly appreciated.

Best regards,
Paul

 [[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.

--
Chris Evans (he/him)
Visiting Professor, UDLA, Quito, Ecuador & Honorary Professor, 
University of Roehampton, London, UK.

Work web site: https://www.psyctc.org/psyctc/
CORE site: http://www.coresystemtrust.org.uk/
Personal site: https://www.psyctc.org/pelerinage2016/
Emeetings (Thursdays): 
https://www.psyctc.org/psyctc/booking-meetings-with-me/

(Beware: French time, generally an hour ahead of UK)


__
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] weights vs. offset (negative binomial regression)

2023-10-28 Thread 유준택
Colleagues,



I have a dataset that includes five variables.

- Catch: the catch number counted in some species (ind.)

- Effort: fishing effort (the number of fishing vessels)

- xx1, xx2, xx3: some environmental factors

As an overdispersion test on the “Catch” variable, I modeled with negative
binomial distribution using a GLM. The “Effort” variable showed a gradually
decreasing trend during the study period. I was able to get the results I
wanted when considered “Effort” function as a weights function in the
negative binomial regression as follows:



library(qcc)

Catch=c(25,2,7,6,75,5,1,4,66,15,9,25,40,8,7,4,36,11,1,14,141,9,74,38,126,3)

Effort=c(258,258,258,258,258,258,258,254,252,252,252,252,252,252,252,252,252,252,252,248,246,246,246,246,246,246)

xx1=c(0.8,0.5,1.2,0.5,1.1,1.1,1.0,0.6,0.9,0.5,1.2,0.6,1.2,0.7,1.0,0.6,1.6,0.7,0.8,0.6,1.7,0.9,1.1,0.5,1.4,0.5)

xx2=c(1.7,1.6,2.7,2.6,1.5,1.5,2.8,2.5,1.7,1.9,2.2,2.4,1.6,1.4,3.0,2.4,1.4,1.5,2.2,2.3,1.7,1.7,1.9,1.9,1.4,1.4)

xx3=c(188,40,2,10,210,102,117,14,141,28,48,15,220,115,10,14,320,20,3,10,400,150,145,160,460,66)

#

edata <- data.frame(Catch, Effort, xx1, xx2, xx3)

#

qcc.overdispersion.test(edata$Catch, type="poisson")

#

summary(glm.nb(Catch~xx1+xx2+xx3, weights=Effort, data=edata))

summary(glm.nb(Catch~xx1+xx2+xx3+offset(log(Effort)), data=edata))



I am not sure the application of the weights function to the negative
binomial regression is correct. Also I wonder if there is a better way
doing this. Can anyone help?

[[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.