Re: [R] (no subject)

2022-11-07 Thread Ivan Krylov
On Sun, 6 Nov 2022 23:34:46 +
Nick Wray  wrote:

> Most of the sets work fine with MICE but with a few I get an error
> message:
> 
> This data set, which generated the error message has five columns
> 
> iter imp variable
>   1   1  986Error in terms.formula(tmp, simplify = TRUE) :
>   invalid term in model formula

Currently, there doesn't seem to be any issues [*] related to your
problem, so you could be the first to report it. (This does look like a
bug in mice; at the very least, the error message could be improved.)
How could the maintainer reproduce your problem in order to debug it?
[**]

traceback() and options(error = recover) are invaluable when trying to
find out what is going on before a crash (see ?traceback, ?recover and
?browser for more information; also the free book R Inferno [***]).

For example, if you set options(error = recover) and then reproduce the
crash, you'll be able to print(tmp) at the time of the crash and see
the term that terms.formula is having a problem with. The next step
would be finding out how this formula came to be, and so on.

>   [[alternative HTML version deleted]]

Please post in plain text to R mailing lists. This particular message
came through mostly fine, but in many cases, the plain text version
automatically generated by the sender's mailer from the user-composed
HTML version makes for very unpleasant reading.

-- 
Best regards,
Ivan

[*]
https://github.com/amices/mice/issues?q=invalid+term+in+model+formula

[**]
See also: https://www.chiark.greenend.org.uk/~sgtatham/bugs.html

[***]
https://www.burns-stat.com/documents/books/the-r-inferno/

__
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] Associate a .R file with the RGui

2022-11-07 Thread Uwe Ligges




On 06.11.2022 19:43, Amarjit Chandhial wrote:


Hi Uwe,




I can do 1.



1. Add the line

if ( length(z <- commandArgs(TRUE)) ) utils::file.edit(z[1])

to the Rprofile.site file; and


My Rprofile.site file is: "C:/PROGRA~1/R/R-42~1.2/etc/Rprofile.site"



How do I do 2, step-by-step?


2. Edit the registry to associate the .R extension with the command

C:\Program Files\R\R-4.2.2\bin\x64\Rgui.exe --args "%1"


If you have not .R file association yet, you can add the line above when 
being asked.

If you have one, simply change the registry key

Computer\HKEY_CLASSES_ROOT\R_auto_file\shell\open\command

There is a standard value as extendable string (REG_EXPAND_SZ) and you 
simply replace its content by


"C:\Program Files\R\R-4.2.2\bin\x64\Rgui.exe" --args "%1"


and in

Computer\HKEY_CLASSES_ROOT\.R

there shoudl be a standard string (REG_SZ)
R_auto_file

Best,
Uwe Ligges









Amarjit






-- Original Message --
From: "Uwe Ligges" 
To: "Amarjit Chandhial" ; "David
Winsemius" ; "Jeff Newmiller"

Cc: "Amarjit Chandhial via R-help" 
Sent: Sunday, 6 Nov, 2022 At 17:19
Subject: Re: [R] Associate a .R file with the RGui



On 06.11.2022 17:21, Amarjit Chandhial via R-help wrote:



Hi Uwe,


RGui does have advantages vs. RStudio, and vice-versa.

RStudio is much faster than it used to be.

I currently have .R files associated with RStudio, i.e. in File Explorer
if I double-click on a .R file it opens in the editor in RStudio as an R
script.

In File Explorer if I right-click on a .R file, Open with, I get the
options: R for Windows GUI Front-end or RStudio or Search the Microsoft
Store or choose another app.
If I choose R for Windows GUI Front-end, RGui (64-bit) opens, but no .R
file, in the editor.
If I choose RStudio the .R file opens in the editor in RStudio as an R
script.

 From the adaptation, if I choose R for Windows GUI Front-end, will the
.R file open in RGui (64-bit) within the editor?


Yes.




I have a new Windows PC and maybe, at least for the time being, it's
best for me to have them associated with RStudio.

Although, yes, I agree, it should be naturally implemented in R, having
installed RStudio.


I meant to implement that one can choose the internal editor to open
.R files. File associations can be set by RStudio, too, but that is
not an R(-core) matter.

Best,
Uwe Ligges




thanks,
Amarjit




-- Original Message --
From: "Uwe Ligges" 
To: "David Winsemius" ; "Amarjit Chandhial"
; "Jeff Newmiller"

Cc: "Amarjit Chandhial via R-help" 
Sent: Sunday, 6 Nov, 2022 At 15:15
Subject: Re: [R] Associate a .R file with the RGui


On 06.11.2022 01:40, David Winsemius wrote:

On 11/5/22 09:58, Amarjit Chandhial via R-help wrote:
Hi Jeff,


Please see my original question.

You were told that RGui is not an editor.

Are you implying that your initial message contained an implicit
request for instructions on how to get R code in an .R file to be opened
automagically when double-clicked or to have "open in "
appear when right-clicked? (I didn't see that clearly expressed.)

If you want something else to happen with a file that has a .R
extension when double-clicked or right-clicked in a GUI file manager,
then you need to configure your OS to do whatever else it is that you
expect. This is not really an R question. It's an OS question. There are
many editors that can also bring up R consoles when the right key combo
is pressed. They do require some study for their specific actions, but
this is not really the place to get guidance on the fine details.


Friends, RGui contains an editor. It allows to send lines / code
blocks to the R concole. We know users who need braille displays use the
RGui internal editor as that apparently cooperates better with braille
displays + R as many other editors.
And as it does not hide half of R by its own functions, I'd pefer that
over some very popular other editors if I had no other choice.


To answer the OP's question, R does not have a built in way to get
this task directly done, but searching the web suggests a rather simple
way (well,m a hack as starting R with other command args may fail now,
but you can extend this and look for a specific naming scheme):


> >

let is adapt this for the recent version of R:


1. Add the line

if ( length(z <- commandArgs(TRUE)) ) utils::file.edit(z[1])

to the Rprofile.site file; 

Re: [R] [External] Re: Selecting a minimum value of an attribute associated with point values neighboring a given point and assigning it as a new attribute

2022-11-07 Thread Duhl, Tiffany R.
Thanks so much Eric!

 I'm going to play around with your toy code (pun intended) & see if I can make 
it work for my application.

Cheers,
-Tiffany

From: Eric Berger 
Sent: Sunday, November 6, 2022 10:27 AM
To: Bert Gunter 
Cc: Duhl, Tiffany R. ; R-help 
Subject: [External] Re: [R] Selecting a minimum value of an attribute 
associated with point values neighboring a given point and assigning it as a 
new attribute

Whoops ... left out a line in Part 2. Resending with the correction

## PART 2: You can use this code on the real data with f() defined appropriately
A <- matrix(0,N,N)
v <- 1:N
## get the indices (j,k) where j < k (as columns in a data.frame)
idx <- expand.grid(v,v) |> rename(j=Var1,k=Var2) |> filter(j < k)
u <- sapply(1:nrow(idx),
   \(i){ j <- idx$j[i]; k <- idx$k[i]; A[j,k] <<- f(j,k,myData) })
B <- A + t(A) + diag(N)
C <- diag(myData$Conc)
D <- B %*% C
D[D==0] <- NA
myData$Conc_min <- apply(D,MAR=1,\(v){min(v,na.rm=TRUE)})
print(head(myData))

On Sun, Nov 6, 2022 at 5:19 PM Eric Berger  wrote:
>
> Hi Tiffany,
> Here is some code that might help with your problem. I solve a "toy"
> problem that is conceptually the same.
> Part 1 sets up my toy problem. You would have to replace Part 1 with
> your real case. The main point is to define
> a function f(i, j, data) which returns 0 or 1 depending on whether the
> observations in rows i and j in your dataset 'data'
> are within your cutoff distance (i.e. 50m).
>
> You can then use Part 2 almost without changes (except for changing
> 'myData' to the correct name of your data).
>
> I hope this helps,
> Eric
>
> library(dplyr)
>
> ## PART 1: create fake data for minimal example
> set.seed(123) ## for reproducibility
> N <- 5   ## replace by number of locations (approx 9000 in your case)
> MAX_DISTANCE <- 2  ## 50 in your case
> myData <- data.frame(x=rnorm(N),y=rnorm(N),Conc=sample(1:N,N))
>
> ## The function which you must re-define for your actual case.
> f <- function(i,j,a) {
>  dist <- sqrt(sum((a[i,1:2] - a[j,1:2])^2)) ## Euclidean distance
>  as.integer(dist < MAX_DISTANCE)
> }
>
> ## PART 2: You can use this code on the real data with f() defined 
> appropriately
> A <- matrix(0,N,N)
> ## get the indices (j,k) where j < k (as columns in a data.frame)
> idx <- expand.grid(v,v) |> rename(j=Var1,k=Var2) |> filter(j < k)
> u <- sapply(1:nrow(idx),\(i){ j <- idx$j[i]; k <- idx$k[i]; A[j,k] <<-
> f(j,k,myData) })
> B <- A + t(A) + diag(N)
> C <- diag(myData$Conc)
> D <- B %*% C
> D[D==0] <- NA
> myData$Conc_min <- apply(D,MAR=1,\(v){min(v,na.rm=TRUE)})
> print(head(myData))
>
>
> On Sat, Nov 5, 2022 at 5:14 PM Bert Gunter  wrote:
> >
> > Probably better posted on R-sig-geo.
> >
> > -- Bert
> >
> > On Sat, Nov 5, 2022 at 12:36 AM Duhl, Tiffany R. 
> > wrote:
> >
> > > Hello,
> > >
> > > I have sets of spatial points with LAT, LON coords (unprojected, WGS84
> > > datum) and several value attributes associated with each point, from
> > > numerous csv files (with an average of 6,000-9,000 points in each file) as
> > > shown in the following example:
> > >
> > > data<- read.csv("R_find_pts_testdata.csv")
> > >
> > > > data
> > > ID  Date TimeLATLON   Conc
> > > Leg.SpeedCO2  H2O BC61 Hr Min Sec
> > > 1   76 4/19/2021 21:25:38 42.40066 -70.98802 99300   0.0 mph 428.39 9.57
> > > 578 21  25  38
> > > 2   77 4/19/2021 21:25:39 42.40066 -70.98802 96730   0.0 mph 428.04 9.57
> > > 617 21  25  39
> > > 3   79 4/19/2021 21:25:41 42.40066 -70.98802 98800   0.2 mph 427.10 9.57
> > > 1027 21  25  41
> > > 4   80 4/19/2021 21:25:42 42.40066 -70.98802 96510 2 mph 427.99 9.58
> > > 1381 21  25  42
> > > 5   81 4/19/2021 21:25:43 42.40067 -70.98801 95540 3 mph 427.99 9.58
> > > 1271 21  25  43
> > > 6   82 4/19/2021 21:25:44 42.40068 -70.98799 94720 4 mph 427.20 9.57
> > > 910 21  25  44
> > > 7   83 4/19/2021 21:25:45 42.40069 -70.98797 94040 5 mph 427.18 9.57
> > > 652 21  25  45
> > > 8   84 4/19/2021 21:25:46 42.40072 -70.98795 95710 7 mph 427.07 9.57
> > > 943 21  25  46
> > > 9   85 4/19/2021 21:25:47 42.40074 -70.98792 96200 8 mph 427.44 9.56
> > > 650 21  25  47
> > > 10  86 4/19/2021 21:25:48 42.40078 -70.98789 9375010 mph 428.76 9.57
> > > 761 21  25  48
> > > 11  87 4/19/2021 21:25:49 42.40081 -70.98785 9336011 mph 429.25 9.56
> > > 1158 21  25  49
> > > 12  88 4/19/2021 21:25:50 42.40084 -70.98781 9434012 mph 429.56 9.57
> > > 107 21  25  50
> > > 13  89 4/19/2021 21:25:51 42.40087 -70.98775 9278012 mph 428.62 9.56
> > > 720 21  25  51
> > >
> > >
> > > What I want to do is, for each point, identify all points within 50m of
> > > that point, find the minimum value of the "Conc" attribute of each nearby
> > > set of points (including the original point) and then create a new 
> > > variable
> > > ("Conc_min") and assign this minimum value to a new variable added to
> > > "data".
> > >
> > > So far, I have the following 

Re: [R] Associate a .R file with the RGui

2022-11-07 Thread Amarjit Chandhial via R-help


Hi Uwe,


Many Thanks.


Amarjiit 



> On 7 Nov 2022, at 14:00, Uwe Ligges  wrote:
> 
> 
> 
>> On 06.11.2022 19:43, Amarjit Chandhial wrote:
>> Hi Uwe,
>> I can do 1.
>> 1. Add the line
>> if ( length(z <- commandArgs(TRUE)) ) utils::file.edit(z[1])
>> to the Rprofile.site file; and
>> My Rprofile.site file is: "C:/PROGRA~1/R/R-42~1.2/etc/Rprofile.site"
>> How do I do 2, step-by-step?
>> 2. Edit the registry to associate the .R extension with the command
>> C:\Program Files\R\R-4.2.2\bin\x64\Rgui.exe --args "%1"
> 
> If you have not .R file association yet, you can add the line above when 
> being asked.
> If you have one, simply change the registry key
> 
> Computer\HKEY_CLASSES_ROOT\R_auto_file\shell\open\command
> 
> There is a standard value as extendable string (REG_EXPAND_SZ) and you simply 
> replace its content by
> 
> "C:\Program Files\R\R-4.2.2\bin\x64\Rgui.exe" --args "%1"
> 
> 
> and in
> 
> Computer\HKEY_CLASSES_ROOT\.R
> 
> there shoudl be a standard string (REG_SZ)
> R_auto_file
> 
> Best,
> Uwe Ligges
> 
> 
> 
> 
> 
>> Amarjit
>>-- Original Message --
>>From: "Uwe Ligges" 
>>To: "Amarjit Chandhial" ; "David
>>Winsemius" ; "Jeff Newmiller"
>>
>>Cc: "Amarjit Chandhial via R-help" 
>>Sent: Sunday, 6 Nov, 2022 At 17:19
>>Subject: Re: [R] Associate a .R file with the RGui
>>On 06.11.2022 17:21, Amarjit Chandhial via R-help wrote:
>>
>>Hi Uwe,
>>RGui does have advantages vs. RStudio, and vice-versa.
>>RStudio is much faster than it used to be.
>>I currently have .R files associated with RStudio, i.e. in File Explorer
>>if I double-click on a .R file it opens in the editor in RStudio as an R
>>script.
>>In File Explorer if I right-click on a .R file, Open with, I get the
>>options: R for Windows GUI Front-end or RStudio or Search the Microsoft
>>Store or choose another app.
>>If I choose R for Windows GUI Front-end, RGui (64-bit) opens, but no .R
>>file, in the editor.
>>If I choose RStudio the .R file opens in the editor in RStudio as an R
>>script.
>> From the adaptation, if I choose R for Windows GUI Front-end, will the
>>.R file open in RGui (64-bit) within the editor?
>>Yes.
>>
>>I have a new Windows PC and maybe, at least for the time being, it's
>>best for me to have them associated with RStudio.
>>Although, yes, I agree, it should be naturally implemented in R, having
>>installed RStudio.
>>I meant to implement that one can choose the internal editor to open
>>.R files. File associations can be set by RStudio, too, but that is
>>not an R(-core) matter.
>>Best,
>>Uwe Ligges
>>
>>thanks,
>>Amarjit
>>-- Original Message --
>>From: "Uwe Ligges" 
>>To: "David Winsemius" ; "Amarjit Chandhial"
>>; "Jeff Newmiller"
>>
>>Cc: "Amarjit Chandhial via R-help" 
>>Sent: Sunday, 6 Nov, 2022 At 15:15
>>Subject: Re: [R] Associate a .R file with the RGui
>>On 06.11.2022 01:40, David Winsemius wrote:
>>On 11/5/22 09:58, Amarjit Chandhial via R-help wrote:
>>Hi Jeff,
>>Please see my original question.
>>You were told that RGui is not an editor.
>>Are you implying that your initial message contained an implicit
>>request for instructions on how to get R code in an .R file to be opened
>>automagically when double-clicked or to have "open in "
>>appear when right-clicked? (I didn't see that clearly expressed.)
>>If you want something else to happen with a file that has a .R
>>extension when double-clicked or right-clicked in a GUI file manager,
>>then you need to configure your OS to do whatever else it is that you
>>expect. This is not really an R question. It's an OS question. There are
>>many editors that can also bring up R consoles when the right key combo
>>is pressed. They do require some study for their specific actions, but
>>this is not really the place to get guidance on the fine details.
>>Friends, RGui contains an editor. It allows to send lines / code
>>blocks to the R concole. We know users who need braille displays use the
>>RGui internal editor as that apparently cooperates better with braille
>>displays + R as many other editors.
>>And as it does not hide half of R by its own functions, I'd pefer that
>>over some very popular other editors if I had no other choice.
>>To answer the OP's question, R does not have a built in way to get
>>this task directly done, but searching the web suggests a rather simple
>>way (well,m a hack as starting R with other command args may fail now,
>>but you can extend this and look for a specific naming scheme):
>>>
>>>

Re: [R] [External] Re: Selecting a minimum value of an attribute associated with point values neighboring a given point and assigning it as a new attribute

2022-11-07 Thread Micha Silver

Eric's solution notwithstanding, here's a more "spatial" approach.


I first create a fictitious set of 1000 points (and save to CSV to 
replicate your workflow)


library(sf)
library(spdep)

# Prepare fictitious data
# Create a data.frame with 1000 random points, and save to CSV
LON <- runif(1000, -70.0, -69.0)
LAT <- runif(1000, 42.0, 43.0)
Conc <- runif(1000, 9, 10)
df <- data.frame(LON, LAT, Conc)
csv_file = "/tmp/pts_testdata.csv"
write.csv(df, csv_file)


Now read that CSV back in directly as an sf object (No need for the old 
SpatialPointsDataFrame). THen create a distance matrix for all points, 
which contains indicies to those points within a certain buffer 
distance, just as you did in your example.



# Read back in as sf object, including row index
pts <- st_as_sf(read.csv(csv_file), coords=c('LON', 'LAT'), crs=4326)
dist_matrix <- dnearneigh(pts, 0, 100, use_s2=TRUE) # use_s2 since these 
are lon/lat


Now I prepare a function to get the minimum Conv value among all points 
within the buffer distance to a given single point:

# Function to get minimum Conc values for one row of distance matrix
MinConc <- function(x, lst, pts) {
  # x is an index to a single point,
  # lst is a list of point indices from distance matrix
  # that are within the buffer distance
  Concs <- lapply(lst, function(p) {
    pts$Conc[p]
  })
  return(min(Concs[[1]]))
}

Next run that function on all points to get a list of minimum Conv 
values for all points, and merge back to pts.



# Now apply this function to all points in pts
Conc_min <- lapply(pts$X, function(i){
  MinConc(i, dist_matrix[i], pts)
})
Conc_min <- data.frame("Conc_min" = as.integer(Conc_min))

# Add back as new attrib to original points sf object
pts_with_min <- do.call(cbind, c(pts, Conc_min))

HTH,

Micha



On 06/11/2022 18:40, Duhl, Tiffany R. wrote:

Thanks so much Eric!

  I'm going to play around with your toy code (pun intended) & see if I can 
make it work for my application.

Cheers,
-Tiffany

From: Eric Berger 
Sent: Sunday, November 6, 2022 10:27 AM
To: Bert Gunter 
Cc: Duhl, Tiffany R. ; R-help 
Subject: [External] Re: [R] Selecting a minimum value of an attribute 
associated with point values neighboring a given point and assigning it as a 
new attribute

Whoops ... left out a line in Part 2. Resending with the correction

## PART 2: You can use this code on the real data with f() defined appropriately
A <- matrix(0,N,N)
v <- 1:N
## get the indices (j,k) where j < k (as columns in a data.frame)
idx <- expand.grid(v,v) |> rename(j=Var1,k=Var2) |> filter(j < k)
u <- sapply(1:nrow(idx),
\(i){ j <- idx$j[i]; k <- idx$k[i]; A[j,k] <<- f(j,k,myData) })
B <- A + t(A) + diag(N)
C <- diag(myData$Conc)
D <- B %*% C
D[D==0] <- NA
myData$Conc_min <- apply(D,MAR=1,\(v){min(v,na.rm=TRUE)})
print(head(myData))

On Sun, Nov 6, 2022 at 5:19 PM Eric Berger  wrote:

Hi Tiffany,
Here is some code that might help with your problem. I solve a "toy"
problem that is conceptually the same.
Part 1 sets up my toy problem. You would have to replace Part 1 with
your real case. The main point is to define
a function f(i, j, data) which returns 0 or 1 depending on whether the
observations in rows i and j in your dataset 'data'
are within your cutoff distance (i.e. 50m).

You can then use Part 2 almost without changes (except for changing
'myData' to the correct name of your data).

I hope this helps,
Eric

library(dplyr)

## PART 1: create fake data for minimal example
set.seed(123) ## for reproducibility
N <- 5   ## replace by number of locations (approx 9000 in your case)
MAX_DISTANCE <- 2  ## 50 in your case
myData <- data.frame(x=rnorm(N),y=rnorm(N),Conc=sample(1:N,N))

## The function which you must re-define for your actual case.
f <- function(i,j,a) {
  dist <- sqrt(sum((a[i,1:2] - a[j,1:2])^2)) ## Euclidean distance
  as.integer(dist < MAX_DISTANCE)
}

## PART 2: You can use this code on the real data with f() defined appropriately
A <- matrix(0,N,N)
## get the indices (j,k) where j < k (as columns in a data.frame)
idx <- expand.grid(v,v) |> rename(j=Var1,k=Var2) |> filter(j < k)
u <- sapply(1:nrow(idx),\(i){ j <- idx$j[i]; k <- idx$k[i]; A[j,k] <<-
f(j,k,myData) })
B <- A + t(A) + diag(N)
C <- diag(myData$Conc)
D <- B %*% C
D[D==0] <- NA
myData$Conc_min <- apply(D,MAR=1,\(v){min(v,na.rm=TRUE)})
print(head(myData))


On Sat, Nov 5, 2022 at 5:14 PM Bert Gunter  wrote:

Probably better posted on R-sig-geo.

-- Bert

On Sat, Nov 5, 2022 at 12:36 AM Duhl, Tiffany R. 
wrote:


Hello,

I have sets of spatial points with LAT, LON coords (unprojected, WGS84
datum) and several value attributes associated with each point, from
numerous csv files (with an average of 6,000-9,000 points in each file) as
shown in the following example:

data<- read.csv("R_find_pts_testdata.csv")


data

 ID  Date TimeLATLON   Conc
Leg.SpeedCO2  H2O BC61 Hr Min Sec
1   76 4/19/2021 

Re: [R] print and lapply....

2022-11-07 Thread akshay kulkarni
Dear Tim,
   Actually, I am replacing a big for loop by the lapply() 
function, and report the progress:

lapply(TP, function(i) { BODY; print(i)})

Can you please adjust your solution in this light?

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Ebert,Timothy Aaron 
Sent: Monday, November 7, 2022 11:07 PM
To: Andrew Simmons ; akshay kulkarni 
Cc: R help Mailing list 
Subject: RE: [R] print and lapply

Another option is use paste() within print()

lapply(TP,function(x){print(paste("x= ",x, " x^2 = ", x^2))})


Tim

-Original Message-
From: R-help  On Behalf Of Andrew Simmons
Sent: Monday, November 7, 2022 12:21 PM
To: akshay kulkarni 
Cc: R help Mailing list 
Subject: Re: [R] print and lapply

[External Email]

put print() around x^2

On Mon, Nov 7, 2022, 12:18 akshay kulkarni  wrote:

> Dear members,
>  I have the following code and output:
>
> > TP <- 1:4
> > lapply(TP,function(x){print(x);x^2})
> [1] 1
> [1] 2
> [1] 3
> [1] 4
> [[1]]
> [1] 1
>
> [[2]]
> [1] 4
>
> [[3]]
> [1] 9
>
> [[4]]
> [1] 16
>
> How do I make the print function output x along with x^2, i.e not at
> the beginning but before each of x^2?
>
> Many thanks in advance
>
> THanking you,
> Yours sincerely
> AKSHAY M KULKARNI
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%40ufl
> .edu%7C28ae59febb2d43ce03b108dac0e46e79%7C0d4da0f84a314d76ace60a62331e
> 1b84%7C0%7C0%7C638034384601744085%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w
> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
> sdata=1D8FQ07q2NcYL8PaJW84PqdUAte3pZJy8XJuJXENbJ4%3Dreserved
> =0
> PLEASE do read the posting guide
> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r
> -project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.edu%
> 7C28ae59febb2d43ce03b108dac0e46e79%7C0d4da0f84a314d76ace60a62331e1b84%
> 7C0%7C0%7C638034384601744085%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwM
> DAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
> sdata=aEeZJ8FHVKGX%2BqOJaKskC1onjqKcON2Ux5cj3MimTGw%3Dreserved=0
> 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://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%40ufl.edu%7C28ae59febb2d43ce03b108dac0e46e79%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638034384601744085%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7Csdata=1D8FQ07q2NcYL8PaJW84PqdUAte3pZJy8XJuJXENbJ4%3Dreserved=0
PLEASE do read the posting guide 
https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.edu%7C28ae59febb2d43ce03b108dac0e46e79%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638034384601744085%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7Csdata=aEeZJ8FHVKGX%2BqOJaKskC1onjqKcON2Ux5cj3MimTGw%3Dreserved=0
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] print and lapply....

2022-11-07 Thread Ebert,Timothy Aaron
Dear Akshay,
I think we have provided several solutions to the question asked. Can you 
please adjust your question to more closely align with what you need. It would 
be nice if you can provide sufficient detail so that we can see how you have 
adapted our solutions and how these have not quite addressed this problem. 

Tim

-Original Message-
From: R-help  On Behalf Of akshay kulkarni
Sent: Monday, November 7, 2022 1:10 PM
To: Bert Gunter ; Andrew Simmons 
Cc: R help Mailing list 
Subject: Re: [R] print and lapply

[External Email]

Dear Bert,
   Actually, I am replacing a big for loop by the lapply() 
function, and report the progress:

lapply(TP, function(i) { BODY; print(i)})

Can you please adjust your solution in this light?

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Bert Gunter 
Sent: Monday, November 7, 2022 11:16 PM
To: Andrew Simmons 
Cc: akshay kulkarni ; R help Mailing list 

Subject: Re: [R] print and lapply

Well... yes, of course. But assuming the sole purpose is to print the results 
and not to save them for further processing, the OP's approach seems rather 
painful. My preference would be to vectorize:

> print(cbind(TP, TPsq =TP^2), print.gap = 3)
   TP   TPsq
[1,]1  1
[2,]2  4
[3,]3  9
[4,]4 16

See ?print.default for details

-- Bert


On Mon, Nov 7, 2022 at 9:20 AM Andrew Simmons  wrote:
>
> put print() around x^2
>
> On Mon, Nov 7, 2022, 12:18 akshay kulkarni  wrote:
>
> > Dear members,
> >  I have the following code and output:
> >
> > > TP <- 1:4
> > > lapply(TP,function(x){print(x);x^2})
> > [1] 1
> > [1] 2
> > [1] 3
> > [1] 4
> > [[1]]
> > [1] 1
> >
> > [[2]]
> > [1] 4
> >
> > [[3]]
> > [1] 9
> >
> > [[4]]
> > [1] 16
> >
> > How do I make the print function output x along with x^2, i.e not at 
> > the beginning but before each of x^2?
> >
> > Many thanks in advance
> >
> > THanking you,
> > Yours sincerely
> > AKSHAY M KULKARNI
> >
> > [[alternative HTML version deleted]]
> >
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fst
> > at.ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%4
> > 0ufl.edu%7Cb726a89349ed4860265708dac0eb5d0a%7C0d4da0f84a314d76ace60a
> > 62331e1b84%7C0%7C0%7C638034414365456384%7CUnknown%7CTWFpbGZsb3d8eyJW
> > IjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C300
> > 0%7C%7C%7Csdata=dQk1JPxWI4D0D6%2FSoC8LWw56749ulaFrGQc6affgBeM%3
> > Dreserved=0
> > PLEASE do read the posting guide
> > https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww
> > .r-project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.
> > edu%7Cb726a89349ed4860265708dac0eb5d0a%7C0d4da0f84a314d76ace60a62331
> > e1b84%7C0%7C0%7C638034414365456384%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiM
> > C4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%
> > 7C%7Csdata=KDskHBxmxe0DV9XO7GvkESemUNTq2w%2BT%2Fro%2Bgb4DS2w%3D
> > reserved=0 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://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%40ufl
> .edu%7Cb726a89349ed4860265708dac0eb5d0a%7C0d4da0f84a314d76ace60a62331e
> 1b84%7C0%7C0%7C638034414365456384%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w
> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
> sdata=dQk1JPxWI4D0D6%2FSoC8LWw56749ulaFrGQc6affgBeM%3Dreserv
> ed=0 PLEASE do read the posting guide 
> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r
> -project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.edu%
> 7Cb726a89349ed4860265708dac0eb5d0a%7C0d4da0f84a314d76ace60a62331e1b84%
> 7C0%7C0%7C638034414365612626%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwM
> DAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
> sdata=OxZmpA4ORbfUq0gqM8ebZvK5bQTaiCwo0Y1ekn%2F3zBE%3Dreserved=0
> 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://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%40ufl.edu%7Cb726a89349ed4860265708dac0eb5d0a%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638034414365612626%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7Csdata=OCyLwP3YBC3kfih9kaX6vEbPdzvAq1a48LeDRyqlYd0%3Dreserved=0
PLEASE do read the posting guide 

Re: [R] print and lapply....

2022-11-07 Thread Bert Gunter
" The lapply() caches the result, and prints the output of the
function in question  immediately after printing the final i. "

I don't believe you understand how lists or lapply works. You seem to
be trying to intuit from empirical behavior. Bad idea, if so. You need
to read the docs or spend time with suitable tutorials.

Consider:
lapply produces a **list,** each component of which is the result of
applying a function, FUN, to the components of its first argument
(here 1:4), which is itself a list. Results are **not** cached in the
sense you seem to think they are. The list object that is produced by
the function is **only** printed if you ask it to be (via cat, print,
or whatever) or, by default, if the result is unassigned. The default
printing can be turned off via the invisible() function.

So:

> res <- lapply(TP, function(x){
+print(x)
+x^2}
+)
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
## Nothing printed, but the results are the components of **list**
res, which can be printed on the console by print(res) or simply:

> res ## note the list syntax below
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

[[5]]
[1] 25

Now can you explain what happens here:

> invisible(lapply(TP, function(x){
+print(x)
+x^2}
+))
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
> ## results not printed; nothing assigned. Where can you find them? (See below)

or here:

> lapply(TP, function(x){
+print(x)
+x^2}
+)
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

[[5]]
[1] 25

## How could you recover the results of the lapply and not just look
at the console printout if the results have not been assigned?
## Hint:  See ?.Last.value

-- Bert

On Mon, Nov 7, 2022 at 11:22 AM akshay kulkarni  wrote:
>
> Dear Rui,
>  THanks for your reply...The point is the loop is a scraping 
> code, and in your examples you have assumed that the body acts on i, the loop 
> variable. Can you adapt your code to JUST PRINT the loop variable i ?
>
> By the by, I think I have stumbled upon the answer: The lapply() caches the 
> result, and prints the output of the function in question  immediately after 
> printing the final i. The i's get printed serially, as the function 
> progresses
>
> > lapply(1:4,function(x){print(x);Sys.sleep(x^2);x^2})
> [1] 1
> [1] 2
> [1] 3
> [1] 4
> [[1]]
> [1] 1
>
> [[2]]
> [1] 4
>
> [[3]]
> [1] 9
>
> [[4]]
> [1] 16
>
> Here x^2 's print only after 4 is printed on the console
>
> tHanks anyways for your reply
>
> THanking you,
> Yours sincerely,
> AKSHAY M KULKARNI
> 
> From: Rui Barradas 
> Sent: Tuesday, November 8, 2022 12:24 AM
> To: akshay kulkarni ; R help Mailing list 
> 
> Subject: Re: [R] print and lapply
>
> Às 18:33 de 07/11/2022, akshay kulkarni escreveu:
> > Dear Rui,
> > Actually, I am replacing a big for loop by the lapply() 
> > function, and report the progress:
> >
> > lapply(TP, function(i) { BODY; print(i)})
> >
> > Can you please adjust your solution in this light?
> >
> > THanking you,
> > Yours sincerely,
> > AKSHAY M KULKARNI
> > 
> > From: Rui Barradas 
> > Sent: Monday, November 7, 2022 11:59 PM
> > To: akshay kulkarni ; R help Mailing list 
> > 
> > Subject: Re: [R] print and lapply
> >
> > Às 17:17 de 07/11/2022, akshay kulkarni escreveu:
> >> Dear members,
> >>I have the following code and output:
> >>
> >>> TP <- 1:4
> >>> lapply(TP,function(x){print(x);x^2})
> >> [1] 1
> >> [1] 2
> >> [1] 3
> >> [1] 4
> >> [[1]]
> >> [1] 1
> >>
> >> [[2]]
> >> [1] 4
> >>
> >> [[3]]
> >> [1] 9
> >>
> >> [[4]]
> >> [1] 16
> >>
> >> How do I make the print function output x along with x^2, i.e not at the 
> >> beginning but before each of x^2?
> >>
> >> Many thanks in advance
> >>
> >> THanking you,
> >> Yours sincerely
> >> AKSHAY M KULKARNI
> >>
> >> [[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.
> > Hello,
> >
> > Here are two options, with ?cat and with ?message.
> >
> >
> > TP <- 1:4
> > lapply(TP, function(x){
> > cat("x =", x, "x^2 =", x^2, "\n")
> > })
> >
> > lapply(TP, function(x){
> > msg <- paste("x =", x, "x^2 =", x^2)
> > message(msg)
> > })
> >
> >
> > Hope this helps,
> >
> > Rui Barradas
> >
> >
> >
> Hello,
>
>
> What do you want the lapply loop to return? If you have a BODY doing
> computations, do you want the lapply to return those values and report
> the progress?
>
> I have chosen cat or message over print because
>
>   - cat returns invisible(NULL),
>   - message returns invisible()
>   - print returns a value, what it prints.
>
> Can you 

Re: [R] print and lapply....

2022-11-07 Thread Rui Barradas

Às 19:22 de 07/11/2022, akshay kulkarni escreveu:

Dear Rui,
  THanks for your reply...The point is the loop is a scraping 
code, and in your examples you have assumed that the body acts on i, the loop 
variable. Can you adapt your code to JUST PRINT the loop variable i ?

By the by, I think I have stumbled upon the answer: The lapply() caches the 
result, and prints the output of the function in question  immediately after 
printing the final i. The i's get printed serially, as the function 
progresses


lapply(1:4,function(x){print(x);Sys.sleep(x^2);x^2})

[1] 1
[1] 2
[1] 3
[1] 4
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

Here x^2 's print only after 4 is printed on the console

tHanks anyways for your reply

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Rui Barradas 
Sent: Tuesday, November 8, 2022 12:24 AM
To: akshay kulkarni ; R help Mailing list 

Subject: Re: [R] print and lapply

Às 18:33 de 07/11/2022, akshay kulkarni escreveu:

Dear Rui,
 Actually, I am replacing a big for loop by the lapply() 
function, and report the progress:

lapply(TP, function(i) { BODY; print(i)})

Can you please adjust your solution in this light?

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Rui Barradas 
Sent: Monday, November 7, 2022 11:59 PM
To: akshay kulkarni ; R help Mailing list 

Subject: Re: [R] print and lapply

Às 17:17 de 07/11/2022, akshay kulkarni escreveu:

Dear members,
I have the following code and output:


TP <- 1:4
lapply(TP,function(x){print(x);x^2})

[1] 1
[1] 2
[1] 3
[1] 4
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

How do I make the print function output x along with x^2, i.e not at the 
beginning but before each of x^2?

Many thanks in advance

THanking you,
Yours sincerely
AKSHAY M KULKARNI

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

Hello,

Here are two options, with ?cat and with ?message.


TP <- 1:4
lapply(TP, function(x){
 cat("x =", x, "x^2 =", x^2, "\n")
})

lapply(TP, function(x){
 msg <- paste("x =", x, "x^2 =", x^2)
 message(msg)
})


Hope this helps,

Rui Barradas




Hello,


What do you want the lapply loop to return? If you have a BODY doing
computations, do you want the lapply to return those values and report
the progress?

I have chosen cat or message over print because

   - cat returns invisible(NULL),
   - message returns invisible()
   - print returns a value, what it prints.

Can you adapt the code below to your use case?



TP <- 1:4
lapply(TP, function(x, verbose = TRUE){
# BODY
y <- rnorm(100, mean = x)

# show progress
if(verbose)
  cat("x =", x, "x^2 =", x^2, "\n")

#return value
c(x = x, mean = mean(y))
})

lapply(TP, function(x, verbose = TRUE){
# BODY
y <- rnorm(100, mean = x)

# show progress
if(verbose) {
  msg <- paste("x =", x, "x^2 =", x^2)
  message(msg)
}

#return value
c(x = x, mean = mean(y))
})



Hope this helps,

Rui Barradas



Hello,

No, the x^2 are not printed after the i's. The x^2 are the function's 
return values. The function prints the i's, then returns x^2.


As for your problem, it is now more clerar.
I would write a function accepting a url to take care of scraping and 
call it in the lapply loop. The progress report can be in the loop, like 
below.


This is a complete working example, scraping the Wikipedia list of 
countries by GDP. The urls are in a list (it's always the same, I'm not 
complicating things) and in a real scraping function I would wrap 
tryCatch around it, just in case.


First the function, then the urls list, then the lapply loop.



library(rvest)

scrape <- function(url) {
  page <- read_html(url)
  gdp <- page |>
html_element(".wikitable") |>
html_table() |>
as.data.frame()
  names(gdp) <- unlist(gdp[1,, drop = TRUE])
  gdp <- gdp[-1,]
  row.names(gdp) <- NULL

  #return value
  gdp
}

wiki_gdp_url <- 
"https://en.wikipedia.org/wiki/List_of_countries_by_GDP_(nominal)"

urls_list <- list(wiki_gdp_url, wiki_gdp_url)
TP <- seq_along(urls_list)

TP
# [1] 1 2

df_list <- lapply(TP, \(i) {
  URL <- urls_list[[i]]
  data <- scrape(URL)
  # show progress
  message("iteration: ", i)
  #return value
  data
})

str(df_list)


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 

Re: [R] print and lapply....

2022-11-07 Thread akshay kulkarni
Dear Rui,
 THanks for your reply...The point is the loop is a scraping 
code, and in your examples you have assumed that the body acts on i, the loop 
variable. Can you adapt your code to JUST PRINT the loop variable i ?

By the by, I think I have stumbled upon the answer: The lapply() caches the 
result, and prints the output of the function in question  immediately after 
printing the final i. The i's get printed serially, as the function 
progresses

> lapply(1:4,function(x){print(x);Sys.sleep(x^2);x^2})
[1] 1
[1] 2
[1] 3
[1] 4
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

Here x^2 's print only after 4 is printed on the console

tHanks anyways for your reply

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Rui Barradas 
Sent: Tuesday, November 8, 2022 12:24 AM
To: akshay kulkarni ; R help Mailing list 

Subject: Re: [R] print and lapply

�s 18:33 de 07/11/2022, akshay kulkarni escreveu:
> Dear Rui,
> Actually, I am replacing a big for loop by the lapply() 
> function, and report the progress:
>
> lapply(TP, function(i) { BODY; print(i)})
>
> Can you please adjust your solution in this light?
>
> THanking you,
> Yours sincerely,
> AKSHAY M KULKARNI
> 
> From: Rui Barradas 
> Sent: Monday, November 7, 2022 11:59 PM
> To: akshay kulkarni ; R help Mailing list 
> 
> Subject: Re: [R] print and lapply
>
> �s 17:17 de 07/11/2022, akshay kulkarni escreveu:
>> Dear members,
>>I have the following code and output:
>>
>>> TP <- 1:4
>>> lapply(TP,function(x){print(x);x^2})
>> [1] 1
>> [1] 2
>> [1] 3
>> [1] 4
>> [[1]]
>> [1] 1
>>
>> [[2]]
>> [1] 4
>>
>> [[3]]
>> [1] 9
>>
>> [[4]]
>> [1] 16
>>
>> How do I make the print function output x along with x^2, i.e not at the 
>> beginning but before each of x^2?
>>
>> Many thanks in advance
>>
>> THanking you,
>> Yours sincerely
>> AKSHAY M KULKARNI
>>
>> [[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.
> Hello,
>
> Here are two options, with ?cat and with ?message.
>
>
> TP <- 1:4
> lapply(TP, function(x){
> cat("x =", x, "x^2 =", x^2, "\n")
> })
>
> lapply(TP, function(x){
> msg <- paste("x =", x, "x^2 =", x^2)
> message(msg)
> })
>
>
> Hope this helps,
>
> Rui Barradas
>
>
>
Hello,


What do you want the lapply loop to return? If you have a BODY doing
computations, do you want the lapply to return those values and report
the progress?

I have chosen cat or message over print because

  - cat returns invisible(NULL),
  - message returns invisible()
  - print returns a value, what it prints.

Can you adapt the code below to your use case?



TP <- 1:4
lapply(TP, function(x, verbose = TRUE){
   # BODY
   y <- rnorm(100, mean = x)

   # show progress
   if(verbose)
 cat("x =", x, "x^2 =", x^2, "\n")

   #return value
   c(x = x, mean = mean(y))
})

lapply(TP, function(x, verbose = TRUE){
   # BODY
   y <- rnorm(100, mean = x)

   # show progress
   if(verbose) {
 msg <- paste("x =", x, "x^2 =", x^2)
 message(msg)
   }

   #return value
   c(x = x, mean = mean(y))
})



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] print and lapply....

2022-11-07 Thread akshay kulkarni
Dear Bert,
   Actually, I am replacing a big for loop by the lapply() 
function, and report the progress:

lapply(TP, function(i) { BODY; print(i)})

Can you please adjust your solution in this light?

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Bert Gunter 
Sent: Monday, November 7, 2022 11:16 PM
To: Andrew Simmons 
Cc: akshay kulkarni ; R help Mailing list 

Subject: Re: [R] print and lapply

Well... yes, of course. But assuming the sole purpose is to print the
results and not to save them for further processing, the OP's approach
seems rather painful. My preference would be to vectorize:

> print(cbind(TP, TPsq =TP^2), print.gap = 3)
   TP   TPsq
[1,]1  1
[2,]2  4
[3,]3  9
[4,]4 16

See ?print.default for details

-- Bert


On Mon, Nov 7, 2022 at 9:20 AM Andrew Simmons  wrote:
>
> put print() around x^2
>
> On Mon, Nov 7, 2022, 12:18 akshay kulkarni  wrote:
>
> > Dear members,
> >  I have the following code and output:
> >
> > > TP <- 1:4
> > > lapply(TP,function(x){print(x);x^2})
> > [1] 1
> > [1] 2
> > [1] 3
> > [1] 4
> > [[1]]
> > [1] 1
> >
> > [[2]]
> > [1] 4
> >
> > [[3]]
> > [1] 9
> >
> > [[4]]
> > [1] 16
> >
> > How do I make the print function output x along with x^2, i.e not at the
> > beginning but before each of x^2?
> >
> > Many thanks in advance
> >
> > THanking you,
> > Yours sincerely
> > AKSHAY M KULKARNI
> >
> > [[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.

[[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] print and lapply....

2022-11-07 Thread Andrew Simmons
put print() around x^2

On Mon, Nov 7, 2022, 12:18 akshay kulkarni  wrote:

> Dear members,
>  I have the following code and output:
>
> > TP <- 1:4
> > lapply(TP,function(x){print(x);x^2})
> [1] 1
> [1] 2
> [1] 3
> [1] 4
> [[1]]
> [1] 1
>
> [[2]]
> [1] 4
>
> [[3]]
> [1] 9
>
> [[4]]
> [1] 16
>
> How do I make the print function output x along with x^2, i.e not at the
> beginning but before each of x^2?
>
> Many thanks in advance
>
> THanking you,
> Yours sincerely
> AKSHAY M KULKARNI
>
> [[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] print and lapply....

2022-11-07 Thread akshay kulkarni
Dear members,
 I have the following code and output:

> TP <- 1:4
> lapply(TP,function(x){print(x);x^2})
[1] 1
[1] 2
[1] 3
[1] 4
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

How do I make the print function output x along with x^2, i.e not at the 
beginning but before each of x^2?

Many thanks in advance

THanking you,
Yours sincerely
AKSHAY M KULKARNI

[[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] print and lapply....

2022-11-07 Thread Ebert,Timothy Aaron
Another option is use paste() within print()

lapply(TP,function(x){print(paste("x= ",x, " x^2 = ", x^2))})


Tim

-Original Message-
From: R-help  On Behalf Of Andrew Simmons
Sent: Monday, November 7, 2022 12:21 PM
To: akshay kulkarni 
Cc: R help Mailing list 
Subject: Re: [R] print and lapply

[External Email]

put print() around x^2

On Mon, Nov 7, 2022, 12:18 akshay kulkarni  wrote:

> Dear members,
>  I have the following code and output:
>
> > TP <- 1:4
> > lapply(TP,function(x){print(x);x^2})
> [1] 1
> [1] 2
> [1] 3
> [1] 4
> [[1]]
> [1] 1
>
> [[2]]
> [1] 4
>
> [[3]]
> [1] 9
>
> [[4]]
> [1] 16
>
> How do I make the print function output x along with x^2, i.e not at 
> the beginning but before each of x^2?
>
> Many thanks in advance
>
> THanking you,
> Yours sincerely
> AKSHAY M KULKARNI
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%40ufl
> .edu%7C28ae59febb2d43ce03b108dac0e46e79%7C0d4da0f84a314d76ace60a62331e
> 1b84%7C0%7C0%7C638034384601744085%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w
> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
> sdata=1D8FQ07q2NcYL8PaJW84PqdUAte3pZJy8XJuJXENbJ4%3Dreserved
> =0
> PLEASE do read the posting guide
> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r
> -project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.edu%
> 7C28ae59febb2d43ce03b108dac0e46e79%7C0d4da0f84a314d76ace60a62331e1b84%
> 7C0%7C0%7C638034384601744085%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwM
> DAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
> sdata=aEeZJ8FHVKGX%2BqOJaKskC1onjqKcON2Ux5cj3MimTGw%3Dreserved=0
> 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://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%40ufl.edu%7C28ae59febb2d43ce03b108dac0e46e79%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638034384601744085%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7Csdata=1D8FQ07q2NcYL8PaJW84PqdUAte3pZJy8XJuJXENbJ4%3Dreserved=0
PLEASE do read the posting guide 
https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.edu%7C28ae59febb2d43ce03b108dac0e46e79%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638034384601744085%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7Csdata=aEeZJ8FHVKGX%2BqOJaKskC1onjqKcON2Ux5cj3MimTGw%3Dreserved=0
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] print and lapply....

2022-11-07 Thread Bert Gunter
Well... yes, of course. But assuming the sole purpose is to print the
results and not to save them for further processing, the OP's approach
seems rather painful. My preference would be to vectorize:

> print(cbind(TP, TPsq =TP^2), print.gap = 3)
   TP   TPsq
[1,]1  1
[2,]2  4
[3,]3  9
[4,]4 16

See ?print.default for details

-- Bert


On Mon, Nov 7, 2022 at 9:20 AM Andrew Simmons  wrote:
>
> put print() around x^2
>
> On Mon, Nov 7, 2022, 12:18 akshay kulkarni  wrote:
>
> > Dear members,
> >  I have the following code and output:
> >
> > > TP <- 1:4
> > > lapply(TP,function(x){print(x);x^2})
> > [1] 1
> > [1] 2
> > [1] 3
> > [1] 4
> > [[1]]
> > [1] 1
> >
> > [[2]]
> > [1] 4
> >
> > [[3]]
> > [1] 9
> >
> > [[4]]
> > [1] 16
> >
> > How do I make the print function output x along with x^2, i.e not at the
> > beginning but before each of x^2?
> >
> > Many thanks in advance
> >
> > THanking you,
> > Yours sincerely
> > AKSHAY M KULKARNI
> >
> > [[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-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] print and lapply....

2022-11-07 Thread akshay kulkarni
Dear Andrew
 It doesn't work:

> lapply(TP,function(x){print(x^2)})
[1] 1
[1] 4
[1] 9
[1] 16
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

Basically, lapply() is implemented by a for loop. So there must be some way 
right?

tHanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Andrew Simmons 
Sent: Monday, November 7, 2022 10:50 PM
To: akshay kulkarni 
Cc: R help Mailing list 
Subject: Re: [R] print and lapply

put print() around x^2

On Mon, Nov 7, 2022, 12:18 akshay kulkarni 
mailto:akshay...@hotmail.com>> wrote:
Dear members,
 I have the following code and output:

> TP <- 1:4
> lapply(TP,function(x){print(x);x^2})
[1] 1
[1] 2
[1] 3
[1] 4
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

How do I make the print function output x along with x^2, i.e not at the 
beginning but before each of x^2?

Many thanks in advance

THanking you,
Yours sincerely
AKSHAY M KULKARNI

[[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] print and lapply....

2022-11-07 Thread Andrew Simmons
I meant something like:

TP <- 1:4
lapply(TP, function(x) {
print(x)
print(x^2)
})

You may wish to add cat("\n") after print(x^2) so that your results
from each iteration are separated.
You may also wish to add invisible() around lapply() if you're not
saving / / using the return list in any way.

On Mon, Nov 7, 2022 at 12:38 PM akshay kulkarni  wrote:
>
> Dear Andrew
>  It doesn't work:
>
> > lapply(TP,function(x){print(x^2)})
> [1] 1
> [1] 4
> [1] 9
> [1] 16
> [[1]]
> [1] 1
>
> [[2]]
> [1] 4
>
> [[3]]
> [1] 9
>
> [[4]]
> [1] 16
>
> Basically, lapply() is implemented by a for loop. So there must be some way 
> right?
>
> tHanking you,
> Yours sincerely,
> AKSHAY M KULKARNI
> 
> From: Andrew Simmons 
> Sent: Monday, November 7, 2022 10:50 PM
> To: akshay kulkarni 
> Cc: R help Mailing list 
> Subject: Re: [R] print and lapply
>
> put print() around x^2
>
> On Mon, Nov 7, 2022, 12:18 akshay kulkarni  wrote:
>
> Dear members,
>  I have the following code and output:
>
> > TP <- 1:4
> > lapply(TP,function(x){print(x);x^2})
> [1] 1
> [1] 2
> [1] 3
> [1] 4
> [[1]]
> [1] 1
>
> [[2]]
> [1] 4
>
> [[3]]
> [1] 9
>
> [[4]]
> [1] 16
>
> How do I make the print function output x along with x^2, i.e not at the 
> beginning but before each of x^2?
>
> Many thanks in advance
>
> THanking you,
> Yours sincerely
> AKSHAY M KULKARNI
>
> [[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] print and lapply....

2022-11-07 Thread akshay kulkarni
Dear Rui,
   Actually, I am replacing a big for loop by the lapply() 
function, and report the progress:

lapply(TP, function(i) { BODY; print(i)})

Can you please adjust your solution in this light?

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Rui Barradas 
Sent: Monday, November 7, 2022 11:59 PM
To: akshay kulkarni ; R help Mailing list 

Subject: Re: [R] print and lapply

�s 17:17 de 07/11/2022, akshay kulkarni escreveu:
> Dear members,
>   I have the following code and output:
>
>> TP <- 1:4
>> lapply(TP,function(x){print(x);x^2})
> [1] 1
> [1] 2
> [1] 3
> [1] 4
> [[1]]
> [1] 1
>
> [[2]]
> [1] 4
>
> [[3]]
> [1] 9
>
> [[4]]
> [1] 16
>
> How do I make the print function output x along with x^2, i.e not at the 
> beginning but before each of x^2?
>
> Many thanks in advance
>
> THanking you,
> Yours sincerely
> AKSHAY M KULKARNI
>
>[[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.
Hello,

Here are two options, with ?cat and with ?message.


TP <- 1:4
lapply(TP, function(x){
   cat("x =", x, "x^2 =", x^2, "\n")
})

lapply(TP, function(x){
   msg <- paste("x =", x, "x^2 =", x^2)
   message(msg)
})


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] print and lapply....

2022-11-07 Thread Rui Barradas

Às 17:17 de 07/11/2022, akshay kulkarni escreveu:

Dear members,
  I have the following code and output:


TP <- 1:4
lapply(TP,function(x){print(x);x^2})

[1] 1
[1] 2
[1] 3
[1] 4
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

How do I make the print function output x along with x^2, i.e not at the 
beginning but before each of x^2?

Many thanks in advance

THanking you,
Yours sincerely
AKSHAY M KULKARNI

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

Hello,

Here are two options, with ?cat and with ?message.


TP <- 1:4
lapply(TP, function(x){
  cat("x =", x, "x^2 =", x^2, "\n")
})

lapply(TP, function(x){
  msg <- paste("x =", x, "x^2 =", x^2)
  message(msg)
})


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] print and lapply....

2022-11-07 Thread Rui Barradas

Às 18:33 de 07/11/2022, akshay kulkarni escreveu:

Dear Rui,
Actually, I am replacing a big for loop by the lapply() 
function, and report the progress:

lapply(TP, function(i) { BODY; print(i)})

Can you please adjust your solution in this light?

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Rui Barradas 
Sent: Monday, November 7, 2022 11:59 PM
To: akshay kulkarni ; R help Mailing list 

Subject: Re: [R] print and lapply

Às 17:17 de 07/11/2022, akshay kulkarni escreveu:

Dear members,
   I have the following code and output:


TP <- 1:4
lapply(TP,function(x){print(x);x^2})

[1] 1
[1] 2
[1] 3
[1] 4
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

How do I make the print function output x along with x^2, i.e not at the 
beginning but before each of x^2?

Many thanks in advance

THanking you,
Yours sincerely
AKSHAY M KULKARNI

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

Hello,

Here are two options, with ?cat and with ?message.


TP <- 1:4
lapply(TP, function(x){
cat("x =", x, "x^2 =", x^2, "\n")
})

lapply(TP, function(x){
msg <- paste("x =", x, "x^2 =", x^2)
message(msg)
})


Hope this helps,

Rui Barradas




Hello,


What do you want the lapply loop to return? If you have a BODY doing 
computations, do you want the lapply to return those values and report 
the progress?


I have chosen cat or message over print because

 - cat returns invisible(NULL),
 - message returns invisible()
 - print returns a value, what it prints.

Can you adapt the code below to your use case?



TP <- 1:4
lapply(TP, function(x, verbose = TRUE){
  # BODY
  y <- rnorm(100, mean = x)

  # show progress
  if(verbose)
cat("x =", x, "x^2 =", x^2, "\n")

  #return value
  c(x = x, mean = mean(y))
})

lapply(TP, function(x, verbose = TRUE){
  # BODY
  y <- rnorm(100, mean = x)

  # show progress
  if(verbose) {
msg <- paste("x =", x, "x^2 =", x^2)
message(msg)
  }

  #return value
  c(x = x, mean = mean(y))
})



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] [External] Re: Selecting a minimum value of an attribute associated with point values neighboring a given point and assigning it as a new attribute

2022-11-07 Thread Eric Berger
## Some further comments and approaches, divided into 3 sections
##
## Section 1: Micha's modified code
## Section 2: Eric's modified code:
##a) uses Micha's dist_matrix from dnearneigh instead of the f() function
##b) add check that it gets the same solution as Micha's modified code
## Section 3: Solution explicitly using a graph (via package igraph)

## ###

## Section 1: Micha's modified code
##
## Major change: OP wanted the Min Conc value
##   to include the Conc of the point itself
## Minor Changes: no need to write/read csv file;
##change MAX_DIST to 50 for fewer neighbors

library(sf)
library(spdep)

## Prepare fictitious data
## Create a data.frame with n random points
set.seed(234) ## for reproducibility
N <- 1000L

LON <- runif(N, -70.0, -69.0)
LAT <- runif(N, 42.0, 43.0)
Conc <- runif(N, 9, 10)
df <- data.frame(LON, LAT, Conc)

## Create a distance matrix for all points,
## which contains indices to those points within
## a certain buffer distance, just as you did in your example.

MAX_DIST <- 50
pts <- st_as_sf(df, coords=c('LON', 'LAT'), crs=4326)
dist_matrix <- dnearneigh(pts, 0, MAX_DIST, use_s2=TRUE)

cat("Average number of neighbors within cutoff = ",
mean(unlist(lapply(dist_matrix,length))),"\n")

## Function to get minimum Conc values for one row of distance matrix
MinConc <- function(x, lst, pts) {
## x is an index to a single point,
## lst is a list of point indices from distance matrix
## that are within the buffer distance
Concs <- lapply(lst, function(p) {
pts$Conc[p]
})
## return(min(c(Concs[[1]])) - ## original code - forgets to
include the point itself
return(min(c(Concs[[1]], pts$Conc[x]))) ## modified code
}

## Now apply this function to all points in pts
Conc_min <- lapply(1:N, function(i) {
  MinConc(i, dist_matrix[i], pts)
})

Conc_min <- data.frame("Conc_min" = as.numeric(Conc_min))

# Add back as new attrib to original points sf object
pts_with_min <- do.call(cbind, c(pts, Conc_min))

## ###

## Section 2: Eric's modified code:

A <- matrix(0,N,N)
z <- sapply(1:N, \(i) A[i, dist_matrix[[i]]] <<- 1) ## z is ignored
B <- A + diag(N)
C <- diag(Conc)
D <- B %*% C
D[D==0] <- NA
Conc_min.eric <- apply(D,MAR=1,\(v) min(v,na.rm=TRUE) )
test <- identical(Conc_min$Conc_min, Conc_min.eric)
cat("test = ", test, "\n")

## ###

## Section 3: Solution explicitly using a graph (via package igraph)

## For those with some familiarity with graphs, the matrix 'B' is
## an adjacency matrix. This suggests using graphs explicitly to solve
## the problem. Here is how to rewrite my code using graphs.

library(igraph)
g <- graph_from_adjacency_matrix(B,"undirected")
g <- set_vertex_attr(g, "Conc", 1:N, Conc )

Conc_min.igraph <- sapply(1:N, \(i) min(vertex_attr(g,"Conc",neighbors(g,i
test.igraph <- identical(Conc_min$Conc_min, Conc_min.igraph)
cat("test.igraph = ", test.igraph, "\n")

Eric

On Mon, Nov 7, 2022 at 3:11 PM Micha Silver  wrote:
>
> Eric's solution notwithstanding, here's a more "spatial" approach.
>
>
> I first create a fictitious set of 1000 points (and save to CSV to
> replicate your workflow)
>
> library(sf)
> library(spdep)
>
> # Prepare fictitious data
> # Create a data.frame with 1000 random points, and save to CSV
> LON <- runif(1000, -70.0, -69.0)
> LAT <- runif(1000, 42.0, 43.0)
> Conc <- runif(1000, 9, 10)
> df <- data.frame(LON, LAT, Conc)
> csv_file = "/tmp/pts_testdata.csv"
> write.csv(df, csv_file)
>
>
> Now read that CSV back in directly as an sf object (No need for the old
> SpatialPointsDataFrame). THen create a distance matrix for all points,
> which contains indicies to those points within a certain buffer
> distance, just as you did in your example.
>
>
> # Read back in as sf object, including row index
> pts <- st_as_sf(read.csv(csv_file), coords=c('LON', 'LAT'), crs=4326)
> dist_matrix <- dnearneigh(pts, 0, 100, use_s2=TRUE) # use_s2 since these
> are lon/lat
>
> Now I prepare a function to get the minimum Conv value among all points
> within the buffer distance to a given single point:
> # Function to get minimum Conc values for one row of distance matrix
> MinConc <- function(x, lst, pts) {
># x is an index to a single point,
># lst is a list of point indices from distance matrix
># that are within the buffer distance
>Concs <- lapply(lst, function(p) {
>  pts$Conc[p]
>})
>return(min(Concs[[1]]))
> }
>
> Next run that function on all points to get a list of minimum Conv
> values for all points, and merge back to pts.
>
>
> # Now apply this function to all points in pts
> Conc_min <- lapply(pts$X, function(i){
>MinConc(i, dist_matrix[i], pts)
> })
> Conc_min <- data.frame("Conc_min" = as.integer(Conc_min))
>
> # Add back as new attrib to original points sf object
> 

Re: [R-es] Problemas libreria dplyr -pide libreria cli y lifecycle pero no me deja descargaralas

2022-11-07 Thread Emilio L. Cano
Hola,

Expande un poco más el “no me deja” para poder ayudarte mejor . Cuando un 
paquete no se logra instalar aparece un mensaje de error que normalmente tiene 
que ver con dependencias (de otros paquetes o del sistema). A ver qué dice ese 
mensaje. Comparte también tu versión de R y el mensaje de error al intentar 
cargar o usar dplyr.

Emilio



> El 7 nov 2022, a las 13:10, ligelai...@gmail.com escribió:
> 
> 
>> 
>> Buenos dias,
>> De repente la libreria dplyr ha dejado de funcionar y me dice que no ha 
>> podido instalar “cil”y “lifecycle”
>> He intentado instalarlos por separado pero no me deja.ademas que he 
>> actualizado todos los paqetes que tenia instalados y no me ha dejado.
>> 
>> He intentado descargarlo a traves de RStudio y tampoco me ha dejado 
>> 
>> Gracias por adelantado,
>> Maria
> 
> ___
> R-help-es mailing list
> R-help-es@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-help-es


[[alternative HTML version deleted]]

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


[R-es] Problemas libreria dplyr -pide libreria cli y lifecycle pero no me deja descargaralas

2022-11-07 Thread ligelaii76


> 
> Buenos dias,
> De repente la libreria dplyr ha dejado de funcionar y me dice que no ha 
> podido instalar “cil”y “lifecycle”
> He intentado instalarlos por separado pero no me deja.ademas que he 
> actualizado todos los paqetes que tenia instalados y no me ha dejado.
> 
> He intentado descargarlo a traves de RStudio y tampoco me ha dejado 
> 
> Gracias por adelantado,
> Maria

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


Re: [R-es] Problemas libreria dplyr -pide libreria cli y lifecycle pero no me deja descargaralas

2022-11-07 Thread Emilio L. Cano
El mensaje que tienes dice que necesitas una versión actualizada de rlang.

Me despista lo de “pegar la librería”. Los paquetes no se pegan, se instalan:

install.packages("lifecycle”)

La expresión anterior instalaría por defecto las dependencias, pero si tienes 
algún error puedes instalar el paquete del que depende aparte.

No conozco ningún curso para reparar errores, pero puedes probar a reinstalar R 
y los paquetes que necesitas.


Emilio L. Cano



> El 7 nov 2022, a las 17:44, ligelai...@gmail.com escribió:
> 
> La version de R es R3.6.1
> No puedo instalar otra version mas nueva.
> 
> 
> Enviado desde mi iPhone
> 
> > El 7 nov 2022, a las 17:36, ligelai...@gmail.com escribió:
> > 
> > Buenas tardes,
> > No puedo comoartir mucha informacion,puesto que es del trabajo.
> > Despues de pegar la lobreria lofecycle y dplyr,me sigue dando error y me 
> > dice que la version deberia ser de lifecycle >= 1.0.0
> > Para hacerlo instale el paquete remote,pero me volvio a dar mas errores.
> 
> > 
> > El error de arriba ha sido al instalar otra vez dplyr.
> > 
> > He probado a cargar otras librerias y es como si no las reconociese,ni 
> > car,ni pysch…..
> > ¿Hay algun curso de R donde se pueda aprender a repararlo?
> > Gracias
> > 
> >> El 7 nov 2022, a las 13:27, Emilio L. Cano  
> >> escribió:
> >> 
> >> Hola,
> >> 
> >> Expande un poco más el “no me deja” para poder ayudarte mejor . Cuando un 
> >> paquete no se logra instalar aparece un mensaje de error que normalmente 
> >> tiene que ver con dependencias (de otros paquetes o del sistema). A ver 
> >> qué dice ese mensaje. Comparte también tu versión de R y el mensaje de 
> >> error al intentar cargar o usar dplyr.
> >> 
> >> Emilio
> >> 
> >> 
> >> 
> > El 7 nov 2022, a las 13:10, ligelai...@gmail.com escribió:
> > 
> > 
> > 
> > Buenos dias,
> > De repente la libreria dplyr ha dejado de funcionar y me dice que no ha 
> > podido instalar “cil”y “lifecycle”
> > He intentado instalarlos por separado pero no me deja.ademas que he 
> > actualizado todos los paqetes que tenia instalados y no me ha dejado.
> > 
> > He intentado descargarlo a traves de RStudio y tampoco me ha dejado 
> > 
> > Gracias por adelantado,
> > Maria
> >>> 
> >>> ___
> >>> R-help-es mailing list
> >>> R-help-es@r-project.org
> >>> https://stat.ethz.ch/mailman/listinfo/r-help-es
> >> 


[[alternative HTML version deleted]]

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


Re: [R-es] Problemas libreria dplyr -pide libreria cli y lifecycle pero no me deja descargaralas

2022-11-07 Thread ligelaii76
Buenas tardes,
No puedo comoartir mucha informacion,puesto que es del trabajo.
Despues de pegar la lobreria lofecycle y dplyr,me sigue dando error y me dice 
que la version deberia ser de lifecycle >= 1.0.0
Para hacerlo instale el paquete remote,pero me volvio a dar mas errores.

El error de arriba ha sido al instalar otra vez dplyr.

He probado a cargar otras librerias y es como si no las reconociese,ni car,ni 
pysch…..
¿Hay algun curso de R donde se pueda aprender a repararlo?
Gracias

> El 7 nov 2022, a las 13:27, Emilio L. Cano  escribió:
> 
> Hola,
> 
> Expande un poco más el “no me deja” para poder ayudarte mejor . Cuando un 
> paquete no se logra instalar aparece un mensaje de error que normalmente 
> tiene que ver con dependencias (de otros paquetes o del sistema). A ver qué 
> dice ese mensaje. Comparte también tu versión de R y el mensaje de error al 
> intentar cargar o usar dplyr.
> 
> Emilio
> 
> 
> 
>>> El 7 nov 2022, a las 13:10, ligelai...@gmail.com escribió:
>>> 
>>> 
>>> 
>>> Buenos dias,
>>> De repente la libreria dplyr ha dejado de funcionar y me dice que no ha 
>>> podido instalar “cil”y “lifecycle”
>>> He intentado instalarlos por separado pero no me deja.ademas que he 
>>> actualizado todos los paqetes que tenia instalados y no me ha dejado.
>>> 
>>> He intentado descargarlo a traves de RStudio y tampoco me ha dejado 
>>> 
>>> Gracias por adelantado,
>>> Maria
>> 
>> ___
>> R-help-es mailing list
>> R-help-es@r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-help-es
> 
___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


Re: [R-es] Problemas libreria dplyr -pide libreria cli y lifecycle pero no me deja descargaralas

2022-11-07 Thread ligelaii76
La version de R es R3.6.1
No puedo instalar otra version mas nueva.


Enviado desde mi iPhone

> El 7 nov 2022, a las 17:36, ligelai...@gmail.com escribió:
> 
> Buenas tardes,
> No puedo comoartir mucha informacion,puesto que es del trabajo.
> Despues de pegar la lobreria lofecycle y dplyr,me sigue dando error y me dice 
> que la version deberia ser de lifecycle >= 1.0.0
> Para hacerlo instale el paquete remote,pero me volvio a dar mas errores.
> 
> El error de arriba ha sido al instalar otra vez dplyr.
> 
> He probado a cargar otras librerias y es como si no las reconociese,ni car,ni 
> pysch…..
> ¿Hay algun curso de R donde se pueda aprender a repararlo?
> Gracias
> 
>> El 7 nov 2022, a las 13:27, Emilio L. Cano  escribió:
>> 
>> Hola,
>> 
>> Expande un poco más el “no me deja” para poder ayudarte mejor . Cuando un 
>> paquete no se logra instalar aparece un mensaje de error que normalmente 
>> tiene que ver con dependencias (de otros paquetes o del sistema). A ver qué 
>> dice ese mensaje. Comparte también tu versión de R y el mensaje de error al 
>> intentar cargar o usar dplyr.
>> 
>> Emilio
>> 
>> 
>> 
> El 7 nov 2022, a las 13:10, ligelai...@gmail.com escribió:
> 
> 
> 
> Buenos dias,
> De repente la libreria dplyr ha dejado de funcionar y me dice que no ha 
> podido instalar “cil”y “lifecycle”
> He intentado instalarlos por separado pero no me deja.ademas que he 
> actualizado todos los paqetes que tenia instalados y no me ha dejado.
> 
> He intentado descargarlo a traves de RStudio y tampoco me ha dejado 
> 
> Gracias por adelantado,
> Maria
>>> 
>>> ___
>>> R-help-es mailing list
>>> R-help-es@r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-help-es
>> 
___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


Re: [R-es] Problemas libreria dplyr -pide libreria cli y lifecycle pero no me deja descargaralas

2022-11-07 Thread ligelaii76
Hola,
Al pegar la libreria me refiero a que cogi manualmente los archivos con 
librerias y pegarlos en el directorio correspondiente de la otra persona.
Al copiar mis librerias donde mi compañera,me pidio la version >= 1.0.0 de 
lifecycle.
Ademas de otras librerias diferentes.
Por otro lado,las librerias que ella ya tenia insataladas no lograba cargarlas 
en R.
Intentare desinstalar e instalar a ver si funciona.

Gracias de todas formas ,
Maria

Enviado desde mi iPhone

> El 7 nov 2022, a las 18:43, Emilio L. Cano  escribió:
> 
> El mensaje que tienes dice que necesitas una versión actualizada de rlang.
> 
> Me despista lo de “pegar la librería”. Los paquetes no se pegan, se instalan:
> 
> install.packages("lifecycle”)
> 
> La expresión anterior instalaría por defecto las dependencias, pero si tienes 
> algún error puedes instalar el paquete del que depende aparte.
> 
> No conozco ningún curso para reparar errores, pero puedes probar a reinstalar 
> R y los paquetes que necesitas.
> 
> 
> Emilio L. Cano
> 
> 
> 
>> El 7 nov 2022, a las 17:44, ligelai...@gmail.com escribió:
>> 
>> La version de R es R3.6.1
>> No puedo instalar otra version mas nueva.
>> 
>> 
>> Enviado desde mi iPhone
>> 
>> > El 7 nov 2022, a las 17:36, ligelai...@gmail.com escribió:
>> > 
>> > Buenas tardes,
>> > No puedo comoartir mucha informacion,puesto que es del trabajo.
>> > Despues de pegar la lobreria lofecycle y dplyr,me sigue dando error y me 
>> > dice que la version deberia ser de lifecycle >= 1.0.0
>> > Para hacerlo instale el paquete remote,pero me volvio a dar mas errores.
>> 
>> > 
>> > El error de arriba ha sido al instalar otra vez dplyr.
>> > 
>> > He probado a cargar otras librerias y es como si no las reconociese,ni 
>> > car,ni pysch…..
>> > ¿Hay algun curso de R donde se pueda aprender a repararlo?
>> > Gracias
>> > 
>> >> El 7 nov 2022, a las 13:27, Emilio L. Cano  
>> >> escribió:
>> >> 
>> >> Hola,
>> >> 
>> >> Expande un poco más el “no me deja” para poder ayudarte mejor . Cuando 
>> >> un paquete no se logra instalar aparece un mensaje de error que 
>> >> normalmente tiene que ver con dependencias (de otros paquetes o del 
>> >> sistema). A ver qué dice ese mensaje. Comparte también tu versión de R y 
>> >> el mensaje de error al intentar cargar o usar dplyr.
>> >> 
>> >> Emilio
>> >> 
>> >> 
>> >> 
>> > El 7 nov 2022, a las 13:10, ligelai...@gmail.com escribió:
>> > 
>> > 
>> > 
>> > Buenos dias,
>> > De repente la libreria dplyr ha dejado de funcionar y me dice que no 
>> > ha podido instalar “cil”y “lifecycle”
>> > He intentado instalarlos por separado pero no me deja.ademas que he 
>> > actualizado todos los paqetes que tenia instalados y no me ha dejado.
>> > 
>> > He intentado descargarlo a traves de RStudio y tampoco me ha dejado 
>> > 
>> > Gracias por adelantado,
>> > Maria
>> >>> 
>> >>> ___
>> >>> R-help-es mailing list
>> >>> R-help-es@r-project.org
>> >>> https://stat.ethz.ch/mailman/listinfo/r-help-es
>> >> 
> 

[[alternative HTML version deleted]]

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


Re: [R-es] Problemas libreria dplyr -pide libreria cli y lifecycle pero no me deja descargaralas

2022-11-07 Thread ligelaii76
Buenos días Carlos,
Por motivos corporativos no piedo instalar otra version diferente a la 3.6.1.

Intente instalar RTools y tanpoco me lo permitió.

Lo único que se me ocurre es desinstalar e instalar de nuevo. Al desinstalar R 
,limpia todas las librerías descargadas?
Tambien,me surge la duda,al instalarse en mi caso y el de mi compañera las 
lobrerias las tenemos en documentos como en C:\windows\archivos de programa\R

¿Por que la duplicidad de las librerías?
¿Por favor,me podrias indicar si hay algun libro o curso donde aprender a 
configurar R?


Muchas gracias,
Maria
> El 7 nov 2022, a las 22:54, Carlos Ortega  
> escribió:
> 
> procede

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


[ESS] Tab completion for CamelCase

2022-11-07 Thread Naresh Gurbuxani via ESS-help
My ess setup on mac works well with tab completion of CamelCase variable names. 
 However, on Windows, tab completes variable names with all lowercase letters.  
So it does give me complete variable name, which needs some letters to be 
converted to uppercase.

Can tab completion on Windows give proper CamelCase variables?

Thanks,
Naresh

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