Re: [R] overlaying two graphs / plots /lines

2023-02-07 Thread Bogdan Tanasa
Thanks a lot Rui and Jim. Works great !

On Tue, Feb 7, 2023, 1:34 PM Rui Barradas  wrote:

> Às 21:18 de 07/02/2023, Jim Lemon escreveu:
> > Hi Bogdan,
> > Try this:
> >
> > A<-data.frame(x=c(1,7,9,20),
> >   y=c(39,91,100,3))
> > B<-data.frame(x=c(10,21,67,99,200),
> >   y=c(9,89,1000,90,1001)) # one value omitted to equalize the rows
> > xrange<-range(c(unlist(A$x),unlist(B$x)))
> > yrange<-range(c(unlist(A$y),unlist(B$y)))
> > plot(A,type="l",xlim=xrange,ylim=yrange,col="red")
> > lines(B,lty=2,col="blue")
> > legend(150,400,c("A","B"),lty=1:2,col=c("red","blue"))
> >
> > There are other tricks to deal with the differences in range between A
> and B.
> >
> > Jim
> >
> > On Wed, Feb 8, 2023 at 7:57 AM Bogdan Tanasa  wrote:
> >>
> >>   Dear all,
> >>
> >> Any suggestions on how I could overlay two or more graphs / plots /
> lines
> >> that have different sizes and the x axes have different breakpoints.
> >>
> >> One dataframe is : A :
> >>
> >> on x axis : 1 , 7, 9, 20, etc ... (100 elements)
> >> on y axis : 39, 91, 100, 3, etc ... (100 elements)
> >>
> >>
> >> The other dataframe is : B :
> >>
> >> on x axis : 10, 21, 67, 99, 200 etc .. (200 elements).
> >> on y axis :  9, 0, 89, 1000, 90, 1001. ... (200 elements).
> >>
> >> Thanks a lot,
> >>
> >> Bogdan
> >>
> >>  [[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.
> Hello,
>
> Here is a ggplot way.
> I'll use the same data.
>
> On each data.frame, create an id column, saying which df it is.
>
>
> A<-data.frame(x=c(1,7,9,20),
>y=c(39,91,100,3))
> B<-data.frame(x=c(10,21,67,99,200),
>y=c(9,89,1000,90,1001)) # one value omitted to equalize
> the rows
>
> suppressPackageStartupMessages({
>library(dplyr)
>library(ggplot2)
> })
>
> bind_rows(
>A %>% mutate(id = "A"),
>B %>% mutate(id = "B")
> )
> #> xy id
> #> 1   1   39  A
> #> 2   7   91  A
> #> 3   9  100  A
> #> 4  203  A
> #> 5  109  B
> #> 6  21   89  B
> #> 7  67 1000  B
> #> 8  99   90  B
> #> 9 200 1001  B
>
>
> To do this in a pipe doesn't change the original data.
> Then pipe the result to ggplot separating the lines by mapping id to
> color. ggplot will automatically take care of the axis ranges.
>
>
> bind_rows(
>A %>% mutate(id = "A"),
>B %>% mutate(id = "B")
> ) %>%
>ggplot(aes(x, y, colour = id)) +
>geom_line() +
>theme_bw()
>
>
> Hope this helps,
>
> Rui Barradas
>
>

[[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] overlaying two graphs / plots /lines

2023-02-07 Thread Rui Barradas

Às 21:18 de 07/02/2023, Jim Lemon escreveu:

Hi Bogdan,
Try this:

A<-data.frame(x=c(1,7,9,20),
  y=c(39,91,100,3))
B<-data.frame(x=c(10,21,67,99,200),
  y=c(9,89,1000,90,1001)) # one value omitted to equalize the rows
xrange<-range(c(unlist(A$x),unlist(B$x)))
yrange<-range(c(unlist(A$y),unlist(B$y)))
plot(A,type="l",xlim=xrange,ylim=yrange,col="red")
lines(B,lty=2,col="blue")
legend(150,400,c("A","B"),lty=1:2,col=c("red","blue"))

There are other tricks to deal with the differences in range between A and B.

Jim

On Wed, Feb 8, 2023 at 7:57 AM Bogdan Tanasa  wrote:


  Dear all,

Any suggestions on how I could overlay two or more graphs / plots / lines
that have different sizes and the x axes have different breakpoints.

One dataframe is : A :

on x axis : 1 , 7, 9, 20, etc ... (100 elements)
on y axis : 39, 91, 100, 3, etc ... (100 elements)


The other dataframe is : B :

on x axis : 10, 21, 67, 99, 200 etc .. (200 elements).
on y axis :  9, 0, 89, 1000, 90, 1001. ... (200 elements).

Thanks a lot,

Bogdan

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

Hello,

Here is a ggplot way.
I'll use the same data.

On each data.frame, create an id column, saying which df it is.


A<-data.frame(x=c(1,7,9,20),
  y=c(39,91,100,3))
B<-data.frame(x=c(10,21,67,99,200),
  y=c(9,89,1000,90,1001)) # one value omitted to equalize 
the rows


suppressPackageStartupMessages({
  library(dplyr)
  library(ggplot2)
})

bind_rows(
  A %>% mutate(id = "A"),
  B %>% mutate(id = "B")
)
#> xy id
#> 1   1   39  A
#> 2   7   91  A
#> 3   9  100  A
#> 4  203  A
#> 5  109  B
#> 6  21   89  B
#> 7  67 1000  B
#> 8  99   90  B
#> 9 200 1001  B


To do this in a pipe doesn't change the original data.
Then pipe the result to ggplot separating the lines by mapping id to 
color. ggplot will automatically take care of the axis ranges.



bind_rows(
  A %>% mutate(id = "A"),
  B %>% mutate(id = "B")
) %>%
  ggplot(aes(x, y, colour = id)) +
  geom_line() +
  theme_bw()


Hope this helps,

Rui Barradas

__
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] overlaying two graphs / plots /lines

2023-02-07 Thread Jim Lemon
Hi Bogdan,
Try this:

A<-data.frame(x=c(1,7,9,20),
 y=c(39,91,100,3))
B<-data.frame(x=c(10,21,67,99,200),
 y=c(9,89,1000,90,1001)) # one value omitted to equalize the rows
xrange<-range(c(unlist(A$x),unlist(B$x)))
yrange<-range(c(unlist(A$y),unlist(B$y)))
plot(A,type="l",xlim=xrange,ylim=yrange,col="red")
lines(B,lty=2,col="blue")
legend(150,400,c("A","B"),lty=1:2,col=c("red","blue"))

There are other tricks to deal with the differences in range between A and B.

Jim

On Wed, Feb 8, 2023 at 7:57 AM Bogdan Tanasa  wrote:
>
>  Dear all,
>
> Any suggestions on how I could overlay two or more graphs / plots / lines
> that have different sizes and the x axes have different breakpoints.
>
> One dataframe is : A :
>
> on x axis : 1 , 7, 9, 20, etc ... (100 elements)
> on y axis : 39, 91, 100, 3, etc ... (100 elements)
>
>
> The other dataframe is : B :
>
> on x axis : 10, 21, 67, 99, 200 etc .. (200 elements).
> on y axis :  9, 0, 89, 1000, 90, 1001. ... (200 elements).
>
> Thanks a lot,
>
> Bogdan
>
> [[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.


[R] overlaying two graphs / plots /lines

2023-02-07 Thread Bogdan Tanasa
 Dear all,

Any suggestions on how I could overlay two or more graphs / plots / lines
that have different sizes and the x axes have different breakpoints.

One dataframe is : A :

on x axis : 1 , 7, 9, 20, etc ... (100 elements)
on y axis : 39, 91, 100, 3, etc ... (100 elements)


The other dataframe is : B :

on x axis : 10, 21, 67, 99, 200 etc .. (200 elements).
on y axis :  9, 0, 89, 1000, 90, 1001. ... (200 elements).

Thanks a lot,

Bogdan

[[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] preserve class in apply function

2023-02-07 Thread Naresh Gurbuxani
Thanks for all the responses.  I need to use some text columns to determine 
method applied to numeric columns. 

Split seems to be the way to go.

Sent from my iPhone

> On Feb 7, 2023, at 8:31 AM, PIKAL Petr  wrote:
> 
> Hi Naresh
> 
> If you wanted to automate the function a bit you can use sapply to find
> numeric columns
> ind <- sapply(mydf, is.numeric)
> 
> and use it in apply construct
> apply(mydf[,ind], 1, function(row) sum(row))
> [1]  2.13002569  0.63305300  1.48420429  0.13523859  1.17515873 -0.98531131
> [7]  0.47044467  0.23914494  0.26504430  0.02037657
> 
> Cheers
> Petr
> 
>> -Original Message-
>> From: R-help  On Behalf Of Naresh Gurbuxani
>> Sent: Tuesday, February 7, 2023 1:52 PM
>> To: r-help@r-project.org
>> Subject: [R] preserve class in apply function
>> 
>> 
>>> Consider a data.frame whose different columns have numeric, character,
>>> and factor data.  In apply function, R seems to pass all elements of a
>>> row as character.  Is it possible to preserve numeric class?
>>> 
 mydf <- data.frame(x = rnorm(10), y = runif(10))
 apply(mydf, 1, function(row) {row["x"] + row["y"]})
>>> [1]  0.60150197 -0.74201827  0.80476392 -0.59729280 -0.02980335
>> 0.31351909
>>> [7] -0.63575990  0.22670658  0.55696314  0.39587314
 mydf[, "z"] <- sample(letters[1:3], 10, replace = TRUE)
 apply(mydf, 1, function(row) {row["x"] + row["y"]})
>>> Error in row["x"] + row["y"] (from #1) : non-numeric argument to binary
>> operator
 apply(mydf, 1, function(row) {as.numeric(row["x"]) +
>> as.numeric(row["y"])})
>>> [1]  0.60150194 -0.74201826  0.80476394 -0.59729282 -0.02980338
>> 0.31351912
>>> [7] -0.63575991  0.22670663  0.55696309  0.39587311
 apply(mydf[,c("x", "y")], 1, function(row) {row["x"] + row["y"]})
>>> [1]  0.60150197 -0.74201827  0.80476392 -0.59729280 -0.02980335
>> 0.31351909
>>> [7] -0.63575990  0.22670658  0.55696314  0.39587314
>> 
>> __
>> 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: [ESS] build problem

2023-02-07 Thread Vincent Goulet via ESS-help

> Le 5 févr. 2023 à 10:44, Dirk Eddelbuettel via ESS-help 
>  a écrit :
> 
> 
> On 5 February 2023 at 15:01, Sparapani, Rodney via ESS-help wrote:
> | I don�t have 28 on this machine. I am using 26 since there is not much
> | benefit upgrading.
> 
> I am really sorry but I cannot let this stand. It is too close to FUD.
> 
> As a general rule, newer *is* better. Features get added, bugs get fixed.
> 
> Now, whether to upgrade or not is a personal choice, people may have
> different circumstances or constraints. But some systems _do_ make it both
> simple and convenient, and I have worked on (and contributed to) such systems
> for longer than a quarter century.  And I for one am enjoying Emacs 28, now
> with an easily-installed .deb from Debian (thanks to Sébastien; I am running
> the Debian .deb without any hiccups on Ubuntu) also with current(ish) ESS,
> and it fabulous. In good part thanks to improved backend support (lsp server
> comes to mind, but also rendering).
> 
> Yay for progress. Yay for volunteers taking care of these upgrades.
> 
> Dirk

I wouldn't go so far as Rodney, nor for the same reason, but I'm still mainly 
on 27.2 to be able to use the good ol', stable, ESS 18.10. 

v.

Vincent Goulet
Professeur titulaire
École d'actuariat, Université Laval


__
ESS-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/ess-help


Re: [R] preserve class in apply function

2023-02-07 Thread PIKAL Petr
Hi Naresh

If you wanted to automate the function a bit you can use sapply to find
numeric columns
ind <- sapply(mydf, is.numeric)

and use it in apply construct
apply(mydf[,ind], 1, function(row) sum(row))
 [1]  2.13002569  0.63305300  1.48420429  0.13523859  1.17515873 -0.98531131
 [7]  0.47044467  0.23914494  0.26504430  0.02037657

Cheers
Petr

> -Original Message-
> From: R-help  On Behalf Of Naresh Gurbuxani
> Sent: Tuesday, February 7, 2023 1:52 PM
> To: r-help@r-project.org
> Subject: [R] preserve class in apply function
> 
> 
> > Consider a data.frame whose different columns have numeric, character,
> > and factor data.  In apply function, R seems to pass all elements of a
> > row as character.  Is it possible to preserve numeric class?
> >
> >> mydf <- data.frame(x = rnorm(10), y = runif(10))
> >> apply(mydf, 1, function(row) {row["x"] + row["y"]})
> > [1]  0.60150197 -0.74201827  0.80476392 -0.59729280 -0.02980335
> 0.31351909
> > [7] -0.63575990  0.22670658  0.55696314  0.39587314
> >> mydf[, "z"] <- sample(letters[1:3], 10, replace = TRUE)
> >> apply(mydf, 1, function(row) {row["x"] + row["y"]})
> > Error in row["x"] + row["y"] (from #1) : non-numeric argument to binary
> operator
> >> apply(mydf, 1, function(row) {as.numeric(row["x"]) +
> as.numeric(row["y"])})
> > [1]  0.60150194 -0.74201826  0.80476394 -0.59729282 -0.02980338
> 0.31351912
> > [7] -0.63575991  0.22670663  0.55696309  0.39587311
> >> apply(mydf[,c("x", "y")], 1, function(row) {row["x"] + row["y"]})
> > [1]  0.60150197 -0.74201827  0.80476392 -0.59729280 -0.02980335
> 0.31351909
> > [7] -0.63575990  0.22670658  0.55696314  0.39587314
> 
> __
> 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] preserve class in apply function

2023-02-07 Thread Rui Barradas

Às 12:51 de 07/02/2023, Naresh Gurbuxani escreveu:



Consider a data.frame whose different columns have numeric, character,
and factor data.  In apply function, R seems to pass all elements of a
row as character.  Is it possible to preserve numeric class?


mydf <- data.frame(x = rnorm(10), y = runif(10))
apply(mydf, 1, function(row) {row["x"] + row["y"]})

[1]  0.60150197 -0.74201827  0.80476392 -0.59729280 -0.02980335  0.31351909
[7] -0.63575990  0.22670658  0.55696314  0.39587314

mydf[, "z"] <- sample(letters[1:3], 10, replace = TRUE)
apply(mydf, 1, function(row) {row["x"] + row["y"]})

Error in row["x"] + row["y"] (from #1) : non-numeric argument to binary operator

apply(mydf, 1, function(row) {as.numeric(row["x"]) + as.numeric(row["y"])})

[1]  0.60150194 -0.74201826  0.80476394 -0.59729282 -0.02980338  0.31351912
[7] -0.63575991  0.22670663  0.55696309  0.39587311

apply(mydf[,c("x", "y")], 1, function(row) {row["x"] + row["y"]})

[1]  0.60150197 -0.74201827  0.80476392 -0.59729280 -0.02980335  0.31351909
[7] -0.63575990  0.22670658  0.55696314  0.39587314


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

The last form,


apply(mydf[,c("x", "y")], 1, function(row) {row["x"] + row["y"]})


is the right one. If your data has columns of a mix of classes, then the 
rows which are vectors are coerced to the greatest common denominator 
class.

From ?c:


Details
The output type is determined from the highest type of the components in 
the hierarchy NULL < raw < logical < integer < double < complex < 
character < list < expression.




Also, since you have a data.frame the following is another possible way:


apply(mydf[c("x", "y")], 1, function(row) {row["x"] + row["y"]})


This doesn't work with matrices.

Hope this helps,

Rui Barradas

__
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] preserve class in apply function

2023-02-07 Thread Andrew Simmons
It is not possible, apply() converts its argument to an array. You might be
able to use split() and lapply() to solve your problem.

On Tue, Feb 7, 2023, 07:52 Naresh Gurbuxani 
wrote:

>
> > Consider a data.frame whose different columns have numeric, character,
> > and factor data.  In apply function, R seems to pass all elements of a
> > row as character.  Is it possible to preserve numeric class?
> >
> >> mydf <- data.frame(x = rnorm(10), y = runif(10))
> >> apply(mydf, 1, function(row) {row["x"] + row["y"]})
> > [1]  0.60150197 -0.74201827  0.80476392 -0.59729280 -0.02980335
> 0.31351909
> > [7] -0.63575990  0.22670658  0.55696314  0.39587314
> >> mydf[, "z"] <- sample(letters[1:3], 10, replace = TRUE)
> >> apply(mydf, 1, function(row) {row["x"] + row["y"]})
> > Error in row["x"] + row["y"] (from #1) : non-numeric argument to binary
> operator
> >> apply(mydf, 1, function(row) {as.numeric(row["x"]) +
> as.numeric(row["y"])})
> > [1]  0.60150194 -0.74201826  0.80476394 -0.59729282 -0.02980338
> 0.31351912
> > [7] -0.63575991  0.22670663  0.55696309  0.39587311
> >> apply(mydf[,c("x", "y")], 1, function(row) {row["x"] + row["y"]})
> > [1]  0.60150197 -0.74201827  0.80476392 -0.59729280 -0.02980335
> 0.31351909
> > [7] -0.63575990  0.22670658  0.55696314  0.39587314
>
> __
> 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] preserve class in apply function

2023-02-07 Thread Naresh Gurbuxani


> Consider a data.frame whose different columns have numeric, character,
> and factor data.  In apply function, R seems to pass all elements of a
> row as character.  Is it possible to preserve numeric class?
> 
>> mydf <- data.frame(x = rnorm(10), y = runif(10))
>> apply(mydf, 1, function(row) {row["x"] + row["y"]})
> [1]  0.60150197 -0.74201827  0.80476392 -0.59729280 -0.02980335  0.31351909
> [7] -0.63575990  0.22670658  0.55696314  0.39587314
>> mydf[, "z"] <- sample(letters[1:3], 10, replace = TRUE)
>> apply(mydf, 1, function(row) {row["x"] + row["y"]})
> Error in row["x"] + row["y"] (from #1) : non-numeric argument to binary 
> operator
>> apply(mydf, 1, function(row) {as.numeric(row["x"]) + as.numeric(row["y"])})
> [1]  0.60150194 -0.74201826  0.80476394 -0.59729282 -0.02980338  0.31351912
> [7] -0.63575991  0.22670663  0.55696309  0.39587311
>> apply(mydf[,c("x", "y")], 1, function(row) {row["x"] + row["y"]})
> [1]  0.60150197 -0.74201827  0.80476392 -0.59729280 -0.02980335  0.31351909
> [7] -0.63575990  0.22670658  0.55696314  0.39587314

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