Re: [R] Need help plotting

2022-09-22 Thread DFP
That worked, and helped my understanding.  Thank you.



Hello,

You have to load the packages involved:


library(dplyr)
library(tidyr)
library(ggplot2)


then run the full sequence. Though this is not important, if you are
pivotting columnns named y1 and y2, why not name the result just y?


pivot_longer(-time, names_to = "y")


And here is complete code, runs in a fresh R session.


tst  <- read.table(text = "
time y1 y2
1  18:55 30 19
2  18:56 30 19
3  18:57 29 19
4  18:58 31 19
5  18:59 28 19
6  19:00 28 19
7  19:01 28 19
8  19:02 28 19
9  19:03 28 19
10 19:04 28 19
11 19:05 29 19
", header = TRUE)

library(dplyr)
library(tidyr)
library(ggplot2)

tst %>%
    mutate(time = paste(Sys.Date(), time),
   time = as.POSIXct(time)) %>%
    select(time, y1, y2) %>%
    # reshape to long format
    pivot_longer(-time, names_to = "y") %>%
    # now plot
    ggplot(aes(time, value, color = y)) +
    geom_line() +
    geom_point() +
    scale_color_manual(values = c("orange", "skyblue")) +
    # make datetime labels
    scale_x_datetime(date_breaks = "1 mins", date_labels = "%H:%M") +
    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] Need help plotting

2022-09-22 Thread Rui Barradas

No you don't! I do!
I don't have tst on my system.

Rui Barradas

Às 22:11 de 22/09/2022, DFP escreveu:
Do I need that read.table if the tst dataframe already exists in my 
system?  If so, why?


--

Hello,

You have to load the packages involved:


library(dplyr)
library(tidyr)
library(ggplot2)


then run the full sequence. Though this is not important, if you are
pivotting columnns named y1 and y2, why not name the result just y?


pivot_longer(-time, names_to = "y")


And here is complete code, runs in a fresh R session.


tst  <- read.table(text = "
time y1 y2
1  18:55 30 19
2  18:56 30 19
3  18:57 29 19
4  18:58 31 19
5  18:59 28 19
6  19:00 28 19
7  19:01 28 19
8  19:02 28 19
9  19:03 28 19
10 19:04 28 19
11 19:05 29 19
", header = TRUE)

library(dplyr)
library(tidyr)
library(ggplot2)

tst %>%
    mutate(time = paste(Sys.Date(), time),
   time = as.POSIXct(time)) %>%
    select(time, y1, y2) %>%
    # reshape to long format
    pivot_longer(-time, names_to = "y") %>%
    # now plot
    ggplot(aes(time, value, color = y)) +
    geom_line() +
    geom_point() +
    scale_color_manual(values = c("orange", "skyblue")) +
    # make datetime labels
    scale_x_datetime(date_breaks = "1 mins", date_labels = "%H:%M") +
    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] Need help plotting

2022-09-22 Thread Rui Barradas

Hello,

You have to load the packages involved:


library(dplyr)
library(tidyr)
library(ggplot2)


then run the full sequence. Though this is not important, if you are 
pivotting columnns named y1 and y2, why not name the result just y?



pivot_longer(-time, names_to = "y")


And here is complete code, runs in a fresh R session.


tst  <- read.table(text = "
time y1 y2
1  18:55 30 19
2  18:56 30 19
3  18:57 29 19
4  18:58 31 19
5  18:59 28 19
6  19:00 28 19
7  19:01 28 19
8  19:02 28 19
9  19:03 28 19
10 19:04 28 19
11 19:05 29 19
", header = TRUE)

library(dplyr)
library(tidyr)
library(ggplot2)

tst %>%
  mutate(time = paste(Sys.Date(), time),
 time = as.POSIXct(time)) %>%
  select(time, y1, y2) %>%
  # reshape to long format
  pivot_longer(-time, names_to = "y") %>%
  # now plot
  ggplot(aes(time, value, color = y)) +
  geom_line() +
  geom_point() +
  scale_color_manual(values = c("orange", "skyblue")) +
  # make datetime labels
  scale_x_datetime(date_breaks = "1 mins", date_labels = "%H:%M") +
  theme_bw()


Hope this helps,

Rui Barradas

Às 20:40 de 22/09/2022, DFP escreveu:
I'm trying to do as you suggest, but I'm not understanding what I need 
to do.  I asked what the line pivot_longer(-time, names_to = "y1") would 
do if applied to this df:


 > tst
     time y1 y2
1  18:55 30 19
2  18:56 30 19
3  18:57 29 19
4  18:58 31 19
5  18:59 28 19
6  19:00 28 19
7  19:01 28 19
8  19:02 28 19
9  19:03 28 19
10 19:04 28 19
11 19:05 29 19

I tried to run these lines and then look at tst, but this is what I get:

 > tst %>%
+    mutate(time = paste(Sys.Date(), time),
+  time = as.POSIXct(time)) %>%
+    select(time, y1,y2) %>%
+    # reshape to long format
+    pivot_longer(-time, names_to = "y1")
Error in tst %>% mutate(time = paste(Sys.Date(), time), time = 
as.POSIXct(time)) %>%  :

   could not find function "%>%"

How can I learn what the pivot_longer has done to tst?

Darn it--Now I try to run the whole sequence that Rui suggested--it 
worked before--and now I get this:


 > tst %>%
+    mutate(time = paste(Sys.Date(), time),
+  time = as.POSIXct(time)) %>%
+    select(time, y1,y2) %>%
+    # reshape to long format
+    pivot_longer(-time, names_to = "y1") %>%
+    # now plot
+    ggplot(aes(time, value, color = y1)) +
+    geom_line() +
+    geom_point() +
+    scale_color_manual(values = c("orange", "skyblue")) +
+    # make datetime labels
+    scale_x_datetime(date_breaks = "1 mins", date_labels = "%H:%M") +
+    theme_bw()
Error in tst %>% mutate(time = paste(Sys.Date(), time), time = 
as.POSIXct(time)) %>%  :

   could not find function "%>%"
 >

What should I try next?



__
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] Need help plotting

2022-09-22 Thread DFP
I'm trying to do as you suggest, but I'm not understanding what I need 
to do.  I asked what the line pivot_longer(-time, names_to = "y1") would 
do if applied to this df:


> tst
    time y1 y2
1  18:55 30 19
2  18:56 30 19
3  18:57 29 19
4  18:58 31 19
5  18:59 28 19
6  19:00 28 19
7  19:01 28 19
8  19:02 28 19
9  19:03 28 19
10 19:04 28 19
11 19:05 29 19

I tried to run these lines and then look at tst, but this is what I get:

> tst %>%
+    mutate(time = paste(Sys.Date(), time),
+  time = as.POSIXct(time)) %>%
+    select(time, y1,y2) %>%
+    # reshape to long format
+    pivot_longer(-time, names_to = "y1")
Error in tst %>% mutate(time = paste(Sys.Date(), time), time = 
as.POSIXct(time)) %>%  :

  could not find function "%>%"

How can I learn what the pivot_longer has done to tst?

Darn it--Now I try to run the whole sequence that Rui suggested--it 
worked before--and now I get this:


> tst %>%
+    mutate(time = paste(Sys.Date(), time),
+  time = as.POSIXct(time)) %>%
+    select(time, y1,y2) %>%
+    # reshape to long format
+    pivot_longer(-time, names_to = "y1") %>%
+    # now plot
+    ggplot(aes(time, value, color = y1)) +
+    geom_line() +
+    geom_point() +
+    scale_color_manual(values = c("orange", "skyblue")) +
+    # make datetime labels
+    scale_x_datetime(date_breaks = "1 mins", date_labels = "%H:%M") +
+    theme_bw()
Error in tst %>% mutate(time = paste(Sys.Date(), time), time = 
as.POSIXct(time)) %>%  :

  could not find function "%>%"
>

What should I try next?

__
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] Write text file in Fortran format

2022-09-22 Thread avi.e.gross
Javad,

After reading the exchanges, I conclude you are asking a somewhat different 
question than some of us expected and I see some have zoomed in on what you 
seem to want.

You seem to want to make a very focused change and save the results to be as 
identical as what you started with. You also note you really have no idea about 
the process that created the file or uses it so it is hard to know if just 
changing parts results in a valid file.

So maybe this looks like not one file but three files. You seem to have a 
header region of N lines you want kept intact but also placed in the output 
untouched. You seem to have a few lines at the end you also seem to want to 
leave untouched. You have lots of lines in the middle you want treated as if it 
contains multiple columns of data in some format that looks like it is 
whitespace-separated, or maybe tabs.

And I think you want to do this transformation perhaps only once so a general 
purpose solution is not required.

So how about you get your favorite text editor or tool and extract the first N 
lines from the file and place it in a file called HEADER. You may also then 
delete it from a copy of the original file if you wish.

Similarly, extract the last lines you want to keep from the file and place it 
in a file called FOOTER.

What is left behind in a copy of the file should then be something people here 
might easily work with. You can use one of many methods as you wish to read the 
data into some kind of data.frame and supply names for the columns and 
whatever. You can make your changes to what seems like one column. You can save 
the output conceptually to use the same format as the input and place it in a 
file called NEW_DATA.

Note no actual files are necessarily needed but conceptually you now have some 
text in places called HEADER, NEW_DATA and FOOTER and you can combine them 
carefully (as in no blank lines or two lines concatenated without a newline) 
and get a new output file that should look like the input.

If your data format in the middle section uses something like a tab, this 
should work. If it uses whitespace and perhaps a fixed-width per line, you have 
an issue to deal with if your changes need more room than is available or need 
adjustment to calculate how many spaces to remove or add to center or line up 
the columns visible.

I will end by saying some others have offered variations that are probably just 
as reasonable. The problem is we may suggest you skip the first lines and it 
turns out you want to preserve them. Most R functions and packages I know about 
have no interest in preserving what effectively are often just comments but it 
seems easy enough to do. Your lines at the end are a bigger problem if you use 
standard commands to read in the data BECAUSE many such programs do a 
look-ahead at perhaps the first thousand lines to try to determine the number 
of columns and the type of each. Your extra lines at the end may perturb that 
and there may be weird results in your data at the end or even things like an 
integer column being considered to be character.



-Original Message-
From: R-help  On Behalf Of javad bayat
Sent: Thursday, September 22, 2022 6:35 AM
To: Rui Barradas 
Cc: R-help@r-project.org
Subject: Re: [R] Write text file in Fortran format

These 2 lines were at the end of the text file, which I have attached but I had 
removed them to read the text file in R.
Just like the first 8 line that start with asterisk (*).






On Thu, 22 Sep 2022, 12:21 Rui Barradas,  wrote:

> Hello,
>
> Are those lines at the begining of the file?
>
> Rui Barradas
>
> Às 06:44 de 22/09/2022, javad bayat escreveu:
> > Dear all; Many thanks for your useful comments and codes.
> > I tried both Rui's and Jim's codes.
> > Jim's codes gave an error as below:
> > "Error in substr(inputline, 1, begincol3 - 1) :
> >object 'inputline' not found"
> > I don't know what's wrong.
> > The Rui's codes worked correctly for the attached file. But I have 
> > edited that file and removed 2 last lines from the file because I 
> > could not read it into R.
> > These 2 lines were:
> > "
> > ** CONCUNIT ug/m^3
> > ** DEPUNIT g/m^2
> > "
> > When I tried to run the code for my file that has these 2 lines at 
> > the
> end,
> > it gave me this error:
> > "
> >> df1 <- read.table(text = txt_table)
> > Error in read.table(text = txt_table) : no lines available in input 
> > "
> > The codes before the "df1 <- read.table(text = txt_table)" were run 
> > correctly.
> > Sincerely
> >
> >
> >
> > On Thu, Sep 22, 2022 at 6:58 AM javad bayat 
> wrote:
> >
> >> Dear all;
> >> I apologise, I didn't know that I have to cc the list.
> >> Thank you Mr Rui for reminding me.
> >> Let me clarify more.
> >> I have no knowledge of the FORTRAN language. The text file that has 
> >> been attached is a model's output file and I know that the format 
> >> is in
> FORTRAN.
> >> I want to write a text file exactly similar to the attached text 
> >> file
>

Re: [R] Error in if (class(networks) == "matrix") from a function

2022-09-22 Thread Chao Liu
Thank you Eric, Andrew, Rui and Martin for all your help and advice. I did
learn some good practices under the new R version!

Best,

Chao

On Thu, Sep 22, 2022 at 5:53 AM Martin Maechler 
wrote:

> > Eric Berger
> > on Wed, 21 Sep 2022 22:26:39 +0300 writes:
>
> > In R 4.2.0 there is a significant change. When you use an if()
> statement
> > with a condition of length > 1 this now reports an error.
> > e.g. this link mentions it as a change
> > https://www.jumpingrivers.com/blog/new-features-r420/
>
> > In your case this is because class(obj) can return a character
> vector of
> > length > 1 (all the classes).
> > One possible workaround would be:
>
> > if ( "matrix" %in% class(networks) ) ...
>
> Yes,  however, in general, the recommendation in the related R
> blog,
>
> would have been
>
>   if(inherits(networks, "matrix"))
>
> which I consider better *correctly readable* (and also expect
> to be more efficient).
>
> Lastly, in this special case, I'd try
>
> if(is.matrix(networks))
>
> i.e., I'd try to see if the fast  is.matrix(.)  applies to your 'networks'
> (and I'm guessing "yes" with high confidence ..).
>
> Martin
>
> > HTH,
> > Eric
>
>
> > On Wed, Sep 21, 2022 at 10:21 PM Chao Liu 
> wrote:
>
> >> Dear R-Help community,
> >>
> >> This is a crosspost on SO but I've had no luck so far. So I have a
> function
> >> which computes a centrality index for the nodes in a network or
> matrix.
> >> Here is the function:
> >>
> >> library(igraph) #load package igraph
> >> centrality <- function (networks, type = c("indegree", "outdegree",
> >> "freeman",
> >> "betweenness", "flow", "closeness", "eigenvector", "information",
> >> "load", "bonpow"), directed = TRUE, lag = 0, rescale = FALSE,
> >> center = FALSE, coefname = NULL, ...) {
> >> if (is.null(directed) || !is.logical(directed)) {
> >> stop("'directed' must be TRUE or FALSE.")
> >> }
> >> else if (length(directed) != 1) {
> >> stop("The 'directed' argument must contain a single logical
> >> value only.")
> >> }
> >> else if (directed == FALSE) {
> >> gmode <- "graph"
> >> }
> >> else {
> >> gmode <- "digraph"
> >> }
> >> objects <- checkDataTypes(y = NULL, networks = networks,
> >> lag = lag)
> >> centlist <- list()
> >> for (i in 1:objects$time.steps) {
> >> if (type[1] == "indegree") {
> >> cent <- degree(objects$networks[[i]], gmode = gmode,
> >> cmode = "indegree", rescale = rescale, ...)
> >> }
> >> else if (type[1] == "outdegree") {
> >> cent <- degree(objects$networks[[i]], gmode = gmode,
> >> cmode = "outdegree", rescale = rescale, ...)
> >> }
> >> else if (type[1] == "freeman") {
> >> cent <- degree(objects$networks[[i]], gmode = gmode,
> >> cmode = "freeman", rescale = rescale, ...)
> >> }
> >> else if (type[1] == "betweenness") {
> >> cent <- betweenness(objects$networks[[i]], gmode = gmode,
> >> rescale = rescale, ...)
> >> }
> >> else if (type[1] == "flow") {
> >> cent <- flowbet(objects$networks[[i]], gmode = gmode,
> >> rescale = rescale, ...)
> >> }
> >> else if (type[1] == "closeness") {
> >> cent <- closeness(objects$networks[[i]], gmode = gmode,
> >> rescale = rescale, ...)
> >> }
> >> else if (type[1] == "eigenvector") {
> >> cent <- evcent(objects$networks[[i]], gmode = gmode,
> >> rescale = rescale, ...)
> >> }
> >> else if (type[1] == "information") {
> >> cent <- infocent(objects$networks[[i]], gmode = gmode,
> >> rescale = rescale, ...)
> >> }
> >> else if (type[1] == "load") {
> >> cent <- loadcent(objects$networks[[i]], gmode = gmode,
> >> rescale = rescale, ...)
> >> }
> >> else if (type[1] == "bonpow") {
> >> cent <- bonpow(objects$networks[[i]], gmode = gmode,
> >> rescale = rescale, tol = 1e-20, ...)
> >> }
> >> else {
> >> stop("'type' argument was not recognized.")
> >> }
> >> centlist[[i]] <- cent
> >> }
> >> time <- numeric()
> >> y <- numeric()
> >> for (i in 1:objects$time.steps) {
> >> time <- c(time, rep(i, objects$n[[i]]))
> >> if (is.null(centlist[[i]])) {
> >> y <- c(y, rep(NA, objects$n[[i]]))
> >> }
> >> else {
> >> if (center == TRUE) {
> >> centlist[[i]] <- centlist[[i]] - mean(centlist[[i]],
> >> na.rm = TRUE)
> >> }
> >> y <- c(y, centlist[[i]])
> >> }
> >> }
> >> if (is.null(coefname) || !is.character(coefname) ||
> length(coefname) >
> >> 1) {
> >> coeflabel <- ""
> >> }
> >> else {
> >> coeflabel <- paste0(".", coefname)
> >> }
> >> if (lag == 0) {
> >> laglabel <- ""
> >> }
> >> else {
> >> laglabel <- paste0(".lag", paste(lag, collapse = "."))
> >> }
> >> label <- paste0(type[1], coeflabel, laglabel)
> >> dat <- data.frame

Re: [R] Write text file in Fortran format

2022-09-22 Thread Rui Barradas

Hello,

Maybe the following solves the problem.
I hope it does if the mailman footer is no longer there.



# this path depends on the OP's system
path <- "~/Temp"
fl <- list.files(path, pattern = "Air.txt", full.names = TRUE)
# read the file as text
txt <- readLines(fl)

# find lines starting with an asterisk
i <- grep("^\\*", txt)
# keep the other lines
txt_table <- txt[-i]
txt_table <- trimws(txt_table)
df1 <- read.table(text = txt_table)
# multiply 3rd column by 2
df1[[3]] <- 2*df1[[3]]

# format the table's lines to write them back to file
fmt <- "%14.5f %13.5f %13.5f %8.2f %8.2f %8.2f %7s %8s %10s %9s %9d"
new_data <- sprintf(fmt,
df1[[1]], df1[[2]], df1[[3]], df1[[4]],
df1[[5]], df1[[6]], df1[[7]], df1[[8]],
df1[[9]], df1[[10]], df1[[11]])

# assemble the header and the table
txt_new <- c(txt[i], new_data)

# write to text file
fl2 <- file.path(dirname(fl), "Air2.txt")
writeLines(txt_new, fl2)


Hope this helps,

Rui Barradas

Às 11:35 de 22/09/2022, javad bayat escreveu:

These 2 lines were at the end of the text file, which I have attached but I
had removed them to read the text file in R.
Just like the first 8 line that start with asterisk (*).






On Thu, 22 Sep 2022, 12:21 Rui Barradas,  wrote:


Hello,

Are those lines at the begining of the file?

Rui Barradas

Às 06:44 de 22/09/2022, javad bayat escreveu:

Dear all; Many thanks for your useful comments and codes.
I tried both Rui's and Jim's codes.
Jim's codes gave an error as below:
"Error in substr(inputline, 1, begincol3 - 1) :
object 'inputline' not found"
I don't know what's wrong.
The Rui's codes worked correctly for the attached file. But I have edited
that file and removed 2 last lines from the file because I could not read
it into R.
These 2 lines were:
"
** CONCUNIT ug/m^3
** DEPUNIT g/m^2
"
When I tried to run the code for my file that has these 2 lines at the

end,

it gave me this error:
"

df1 <- read.table(text = txt_table)

Error in read.table(text = txt_table) : no lines available in input
"
The codes before the "df1 <- read.table(text = txt_table)" were run
correctly.
Sincerely



On Thu, Sep 22, 2022 at 6:58 AM javad bayat 

wrote:



Dear all;
I apologise, I didn't know that I have to cc the list.
Thank you Mr Rui for reminding me.
Let me clarify more.
I have no knowledge of the FORTRAN language. The text file that has been
attached is a model's output file and I know that the format is in

FORTRAN.

I want to write a text file exactly similar to the attached text file

using

R programming.
The steps below explain my goal:
1- Read the text file without the first 8 and last 2 rows as a

dataframe.

Maybe I have removed the last 2 lines, but at the end it had 2 lines
similar the first 8 lines which starts with asterisk.
2- Double the 3rd column values (or multiply by specific number).
3- Insert the removed first 8 lines of the original text file as header

in

the dataframe.
4- Insert the removed last 2 lines of the original text file at the end

of

dataframe.
5- Write the dataframe as a new text file exactly similar to the

original

text file.

I have used excel but when I save it as text file, the format changes

and

is not similar to the attached text file.
I will try all your codes in a few hours, cause I am out of office.
Sincerely yours.



On Thu, 22 Sep 2022, 04:20 Richard O'Keefe,  wrote:


Oh, so you want to WRITE a file *like* that.
Use ?sprintf.
(3(1X,F13.5),3(1X,F8.2),3X,A5,2X,A8,2X,A5,5X,A8,2X,I8)

Fortran sprintf
1X  a space
3X  3 spaces
F13.5   %13.5f
F8.2%8.2f
A5  %5s
A8  %8s
I8  %8d
3(...)  the translation of ... written 3 times

We can simplify 1X,F13.5 to F14.5
Here's the sprintf() equivalent of the Fortran format,
except you'll need to change the dots to spaces.

"%14.5f%14.5f%14.5f%9.2f%9.2f%9.2f...%5s..%8s..%5s.%8s%10d\n

On Thu, 22 Sept 2022 at 04:17, javad bayat 

wrote:



Dear Rasmus;
I have no knowledge of the FORTRAN language. The text file that has

been

attached is a model's output file and I know that the format is in
FORTRAN.
I want to write a text file exactly similar to the attached text file
using
R programming.
The steps below explain my goal:
1- Read the text file without the first 8 and last 2 rows as a

dataframe.

2- Double the 3rd column values (or multiply by specific number).
3- Insert the removed first 8 rows of the original text file as

header in

the dataframe.
4- Insert the removed last 2 rows of the original text file at the

end of

dataframe.
5- Write the dataframe as a new text file exactly similar to the

original

text file.

I have used excel but when I save it as text file, the format changes

and

is not similar to the attached text file.
Sincerely

On Wed, 21 Sep 2022, 18:17 Rasmus Liland,  wrote:


Dear Javad,

Perhaps you were looking to read the
table in Air.txt (is this Fortran
format?) into R?

  b <- readLines("Air.txt")  # The text MIME attachment

w/Ma

Re: [R] Write text file in Fortran format

2022-09-22 Thread javad bayat
These 2 lines were at the end of the text file, which I have attached but I
had removed them to read the text file in R.
Just like the first 8 line that start with asterisk (*).






On Thu, 22 Sep 2022, 12:21 Rui Barradas,  wrote:

> Hello,
>
> Are those lines at the begining of the file?
>
> Rui Barradas
>
> Às 06:44 de 22/09/2022, javad bayat escreveu:
> > Dear all; Many thanks for your useful comments and codes.
> > I tried both Rui's and Jim's codes.
> > Jim's codes gave an error as below:
> > "Error in substr(inputline, 1, begincol3 - 1) :
> >object 'inputline' not found"
> > I don't know what's wrong.
> > The Rui's codes worked correctly for the attached file. But I have edited
> > that file and removed 2 last lines from the file because I could not read
> > it into R.
> > These 2 lines were:
> > "
> > ** CONCUNIT ug/m^3
> > ** DEPUNIT g/m^2
> > "
> > When I tried to run the code for my file that has these 2 lines at the
> end,
> > it gave me this error:
> > "
> >> df1 <- read.table(text = txt_table)
> > Error in read.table(text = txt_table) : no lines available in input
> > "
> > The codes before the "df1 <- read.table(text = txt_table)" were run
> > correctly.
> > Sincerely
> >
> >
> >
> > On Thu, Sep 22, 2022 at 6:58 AM javad bayat 
> wrote:
> >
> >> Dear all;
> >> I apologise, I didn't know that I have to cc the list.
> >> Thank you Mr Rui for reminding me.
> >> Let me clarify more.
> >> I have no knowledge of the FORTRAN language. The text file that has been
> >> attached is a model's output file and I know that the format is in
> FORTRAN.
> >> I want to write a text file exactly similar to the attached text file
> using
> >> R programming.
> >> The steps below explain my goal:
> >> 1- Read the text file without the first 8 and last 2 rows as a
> dataframe.
> >> Maybe I have removed the last 2 lines, but at the end it had 2 lines
> >> similar the first 8 lines which starts with asterisk.
> >> 2- Double the 3rd column values (or multiply by specific number).
> >> 3- Insert the removed first 8 lines of the original text file as header
> in
> >> the dataframe.
> >> 4- Insert the removed last 2 lines of the original text file at the end
> of
> >> dataframe.
> >> 5- Write the dataframe as a new text file exactly similar to the
> original
> >> text file.
> >>
> >> I have used excel but when I save it as text file, the format changes
> and
> >> is not similar to the attached text file.
> >> I will try all your codes in a few hours, cause I am out of office.
> >> Sincerely yours.
> >>
> >>
> >>
> >> On Thu, 22 Sep 2022, 04:20 Richard O'Keefe,  wrote:
> >>
> >>> Oh, so you want to WRITE a file *like* that.
> >>> Use ?sprintf.
> >>> (3(1X,F13.5),3(1X,F8.2),3X,A5,2X,A8,2X,A5,5X,A8,2X,I8)
> >>>
> >>> Fortran sprintf
> >>> 1X  a space
> >>> 3X  3 spaces
> >>> F13.5   %13.5f
> >>> F8.2%8.2f
> >>> A5  %5s
> >>> A8  %8s
> >>> I8  %8d
> >>> 3(...)  the translation of ... written 3 times
> >>>
> >>> We can simplify 1X,F13.5 to F14.5
> >>> Here's the sprintf() equivalent of the Fortran format,
> >>> except you'll need to change the dots to spaces.
> >>>
> >>> "%14.5f%14.5f%14.5f%9.2f%9.2f%9.2f...%5s..%8s..%5s.%8s%10d\n
> >>>
> >>> On Thu, 22 Sept 2022 at 04:17, javad bayat 
> wrote:
> >>>
>  Dear Rasmus;
>  I have no knowledge of the FORTRAN language. The text file that has
> been
>  attached is a model's output file and I know that the format is in
>  FORTRAN.
>  I want to write a text file exactly similar to the attached text file
>  using
>  R programming.
>  The steps below explain my goal:
>  1- Read the text file without the first 8 and last 2 rows as a
> dataframe.
>  2- Double the 3rd column values (or multiply by specific number).
>  3- Insert the removed first 8 rows of the original text file as
> header in
>  the dataframe.
>  4- Insert the removed last 2 rows of the original text file at the
> end of
>  dataframe.
>  5- Write the dataframe as a new text file exactly similar to the
> original
>  text file.
> 
>  I have used excel but when I save it as text file, the format changes
> and
>  is not similar to the attached text file.
>  Sincerely
> 
>  On Wed, 21 Sep 2022, 18:17 Rasmus Liland,  wrote:
> 
> > Dear Javad,
> >
> > Perhaps you were looking to read the
> > table in Air.txt (is this Fortran
> > format?) into R?
> >
> >  b <- readLines("Air.txt")  # The text MIME attachment
> w/Mailman
> > footer ...
> >  b <- b[1:(which(b=="")-1)]  # Remove the odd Mailman footer
> (at
> > end of df)
> >  idx <- max(grep("^\\*", b))+1  # Start of df after header
> uline
> >  header <- tolower(strsplit(gsub("  +", "_", gsub("\\*", "",
> > b[idx-2])), "_")[[1]])
> >  b <- read.table(text=b[-(1:idx)], header=F)
> >  colnames(b) <- header[header!=""]
> >  b <- b[,sapply(b, \

Re: [R] Mathematical working procedure of imputation methods (medianImpute, knnImpute, and bagImpute) in caret package R

2022-09-22 Thread K Purna Prakash
Thanks, I'll check them out.

On Wed, Sep 21, 2022, 03:16 Bert Gunter  wrote:

> R is open source. Look at the code and read it.
> Alternatively, look at references for all of this. e.g. on Wikipedia or
> via web search. We generally do not provide statistical instruction on this
> list.
>
> Bert
>
> On Tue, Sep 20, 2022 at 2:20 PM K Purna Prakash 
> wrote:
>
>> Dear Sir/Madam,
>> Greetings!!!
>>
>> Kindly provide the detailed internal mathematical working mechanism of the
>> following median, KNN, and bagging imputation methods available in caret
>> package R.
>>
>>  preProcess(train_data, method = "medianImpute")
>>  preProcess(train_data, method = "knnnImpute")
>>  preProcess(train_data method = "bagImpute")
>>
>> The details provided by you will help me a lot for a better understanding
>> of these imputation methods especially while dealing with large sets of
>> data.
>>
>> I will look forward to hearing from you.
>>
>> Thanks and regards,
>> K. Purna Prakash.
>>
>> [[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] Error in if (class(networks) == "matrix") from a function

2022-09-22 Thread Martin Maechler
> Eric Berger 
> on Wed, 21 Sep 2022 22:26:39 +0300 writes:

> In R 4.2.0 there is a significant change. When you use an if() statement
> with a condition of length > 1 this now reports an error.
> e.g. this link mentions it as a change
> https://www.jumpingrivers.com/blog/new-features-r420/

> In your case this is because class(obj) can return a character vector of
> length > 1 (all the classes).
> One possible workaround would be:

> if ( "matrix" %in% class(networks) ) ...

Yes,  however, in general, the recommendation in the related R
blog,

would have been

  if(inherits(networks, "matrix"))

which I consider better *correctly readable* (and also expect
to be more efficient).

Lastly, in this special case, I'd try

if(is.matrix(networks))

i.e., I'd try to see if the fast  is.matrix(.)  applies to your 'networks'
(and I'm guessing "yes" with high confidence ..).

Martin

> HTH,
> Eric


> On Wed, Sep 21, 2022 at 10:21 PM Chao Liu  wrote:

>> Dear R-Help community,
>> 
>> This is a crosspost on SO but I've had no luck so far. So I have a 
function
>> which computes a centrality index for the nodes in a network or matrix.
>> Here is the function:
>> 
>> library(igraph) #load package igraph
>> centrality <- function (networks, type = c("indegree", "outdegree",
>> "freeman",
>> "betweenness", "flow", "closeness", "eigenvector", "information",
>> "load", "bonpow"), directed = TRUE, lag = 0, rescale = FALSE,
>> center = FALSE, coefname = NULL, ...) {
>> if (is.null(directed) || !is.logical(directed)) {
>> stop("'directed' must be TRUE or FALSE.")
>> }
>> else if (length(directed) != 1) {
>> stop("The 'directed' argument must contain a single logical
>> value only.")
>> }
>> else if (directed == FALSE) {
>> gmode <- "graph"
>> }
>> else {
>> gmode <- "digraph"
>> }
>> objects <- checkDataTypes(y = NULL, networks = networks,
>> lag = lag)
>> centlist <- list()
>> for (i in 1:objects$time.steps) {
>> if (type[1] == "indegree") {
>> cent <- degree(objects$networks[[i]], gmode = gmode,
>> cmode = "indegree", rescale = rescale, ...)
>> }
>> else if (type[1] == "outdegree") {
>> cent <- degree(objects$networks[[i]], gmode = gmode,
>> cmode = "outdegree", rescale = rescale, ...)
>> }
>> else if (type[1] == "freeman") {
>> cent <- degree(objects$networks[[i]], gmode = gmode,
>> cmode = "freeman", rescale = rescale, ...)
>> }
>> else if (type[1] == "betweenness") {
>> cent <- betweenness(objects$networks[[i]], gmode = gmode,
>> rescale = rescale, ...)
>> }
>> else if (type[1] == "flow") {
>> cent <- flowbet(objects$networks[[i]], gmode = gmode,
>> rescale = rescale, ...)
>> }
>> else if (type[1] == "closeness") {
>> cent <- closeness(objects$networks[[i]], gmode = gmode,
>> rescale = rescale, ...)
>> }
>> else if (type[1] == "eigenvector") {
>> cent <- evcent(objects$networks[[i]], gmode = gmode,
>> rescale = rescale, ...)
>> }
>> else if (type[1] == "information") {
>> cent <- infocent(objects$networks[[i]], gmode = gmode,
>> rescale = rescale, ...)
>> }
>> else if (type[1] == "load") {
>> cent <- loadcent(objects$networks[[i]], gmode = gmode,
>> rescale = rescale, ...)
>> }
>> else if (type[1] == "bonpow") {
>> cent <- bonpow(objects$networks[[i]], gmode = gmode,
>> rescale = rescale, tol = 1e-20, ...)
>> }
>> else {
>> stop("'type' argument was not recognized.")
>> }
>> centlist[[i]] <- cent
>> }
>> time <- numeric()
>> y <- numeric()
>> for (i in 1:objects$time.steps) {
>> time <- c(time, rep(i, objects$n[[i]]))
>> if (is.null(centlist[[i]])) {
>> y <- c(y, rep(NA, objects$n[[i]]))
>> }
>> else {
>> if (center == TRUE) {
>> centlist[[i]] <- centlist[[i]] - mean(centlist[[i]],
>> na.rm = TRUE)
>> }
>> y <- c(y, centlist[[i]])
>> }
>> }
>> if (is.null(coefname) || !is.character(coefname) || length(coefname) >
>> 1) {
>> coeflabel <- ""
>> }
>> else {
>> coeflabel <- paste0(".", coefname)
>> }
>> if (lag == 0) {
>> laglabel <- ""
>> }
>> else {
>> laglabel <- paste0(".lag", paste(lag, collapse = "."))
>> }
>> label <- paste0(type[1], coeflabel, laglabel)
>> dat <- data.frame(y, time = time, node = objects$nodelabels)
>> dat$node <- as.character(dat$node)
>> colnames(dat)[1] <- label
>> attributes(dat)$lag <- lag
>> return(dat)}
>> 
>> Here is the matrix:
>> 
>> dat <- read.table(text="A B #this is edgelist
>> 1 2
>> 1 3
>> 1 2
>> 2 1
>> 2 3
>> 3 1
>> 3 2
>> 3 1", header=TRUE)
>> datmat <- as.matrix(get.adjacency(graph.edgelist(as.matrix(dat),
>> directed=TRUE))) #this is

Re: [R] Write text file in Fortran format

2022-09-22 Thread Rui Barradas

Hello,

Are those lines at the begining of the file?

Rui Barradas

Às 06:44 de 22/09/2022, javad bayat escreveu:

Dear all; Many thanks for your useful comments and codes.
I tried both Rui's and Jim's codes.
Jim's codes gave an error as below:
"Error in substr(inputline, 1, begincol3 - 1) :
   object 'inputline' not found"
I don't know what's wrong.
The Rui's codes worked correctly for the attached file. But I have edited
that file and removed 2 last lines from the file because I could not read
it into R.
These 2 lines were:
"
** CONCUNIT ug/m^3
** DEPUNIT g/m^2
"
When I tried to run the code for my file that has these 2 lines at the end,
it gave me this error:
"

df1 <- read.table(text = txt_table)

Error in read.table(text = txt_table) : no lines available in input
"
The codes before the "df1 <- read.table(text = txt_table)" were run
correctly.
Sincerely



On Thu, Sep 22, 2022 at 6:58 AM javad bayat  wrote:


Dear all;
I apologise, I didn't know that I have to cc the list.
Thank you Mr Rui for reminding me.
Let me clarify more.
I have no knowledge of the FORTRAN language. The text file that has been
attached is a model's output file and I know that the format is in FORTRAN.
I want to write a text file exactly similar to the attached text file using
R programming.
The steps below explain my goal:
1- Read the text file without the first 8 and last 2 rows as a dataframe.
Maybe I have removed the last 2 lines, but at the end it had 2 lines
similar the first 8 lines which starts with asterisk.
2- Double the 3rd column values (or multiply by specific number).
3- Insert the removed first 8 lines of the original text file as header in
the dataframe.
4- Insert the removed last 2 lines of the original text file at the end of
dataframe.
5- Write the dataframe as a new text file exactly similar to the original
text file.

I have used excel but when I save it as text file, the format changes and
is not similar to the attached text file.
I will try all your codes in a few hours, cause I am out of office.
Sincerely yours.



On Thu, 22 Sep 2022, 04:20 Richard O'Keefe,  wrote:


Oh, so you want to WRITE a file *like* that.
Use ?sprintf.
(3(1X,F13.5),3(1X,F8.2),3X,A5,2X,A8,2X,A5,5X,A8,2X,I8)

Fortran sprintf
1X  a space
3X  3 spaces
F13.5   %13.5f
F8.2%8.2f
A5  %5s
A8  %8s
I8  %8d
3(...)  the translation of ... written 3 times

We can simplify 1X,F13.5 to F14.5
Here's the sprintf() equivalent of the Fortran format,
except you'll need to change the dots to spaces.

"%14.5f%14.5f%14.5f%9.2f%9.2f%9.2f...%5s..%8s..%5s.%8s%10d\n

On Thu, 22 Sept 2022 at 04:17, javad bayat  wrote:


Dear Rasmus;
I have no knowledge of the FORTRAN language. The text file that has been
attached is a model's output file and I know that the format is in
FORTRAN.
I want to write a text file exactly similar to the attached text file
using
R programming.
The steps below explain my goal:
1- Read the text file without the first 8 and last 2 rows as a dataframe.
2- Double the 3rd column values (or multiply by specific number).
3- Insert the removed first 8 rows of the original text file as header in
the dataframe.
4- Insert the removed last 2 rows of the original text file at the end of
dataframe.
5- Write the dataframe as a new text file exactly similar to the original
text file.

I have used excel but when I save it as text file, the format changes and
is not similar to the attached text file.
Sincerely

On Wed, 21 Sep 2022, 18:17 Rasmus Liland,  wrote:


Dear Javad,

Perhaps you were looking to read the
table in Air.txt (is this Fortran
format?) into R?

 b <- readLines("Air.txt")  # The text MIME attachment w/Mailman
footer ...
 b <- b[1:(which(b=="")-1)]  # Remove the odd Mailman footer (at
end of df)
 idx <- max(grep("^\\*", b))+1  # Start of df after header uline
 header <- tolower(strsplit(gsub("  +", "_", gsub("\\*", "",
b[idx-2])), "_")[[1]])
 b <- read.table(text=b[-(1:idx)], header=F)
 colnames(b) <- header[header!=""]
 b <- b[,sapply(b, \(i) length(unique(i)))>1]  # Remove constant
cols

str(b)

 'data.frame':   31324 obs. of  6 variables:
  $ x   : num  583500 584000 584500 585000 585500 ...
  $ y   : num  3018700 3018700 3018700 3018700 3018700

...

  $ average conc: num  32.8 33.1 33.4 33.5 33.6 ...
  $ zelev   : num  0 0 0 0 0 0 0 0 0 0 ...
  $ zhill   : num  0 0 0 0 0 0 0 0 0 0 ...
  $ date(conc)  : int  16101706 16101706 16101706 16101706

16101706

...

Best,
Rasmus

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

Re: [R] Fatal Error: Contains Space

2022-09-22 Thread Andrew Simmons
Hello,


I'm going to quote the tempdir() doc page: "By default, tmpdir will be the
directory given by tempdir(). This will be a subdirectory of the
per-session temporary directory found by the following rule when the R
session is started. The environment variables TMPDIR, TMP and TEMP are
checked in turn and the first found which points to a writable directory is
used: if none succeeds the value of R_USER (see Rconsole) is used. If the
path to the directory contains a space in any of the components, the path
returned will use the shortnames version of the path."

You need to define one of the environment variables listed above. For
myself, I did something like

TEMP=C:\Users\iris\AppData\Local\Temp


On Thu., Sep. 22, 2022, 03:37 Kaitlyn Light,  wrote:

> Hello!
> I recently downloaded R and RStudio to my windows laptop. I downloaded the
> correct version and made sure it was for windows and not mac. However, when
> I tried to open RStudio, a message saying " R_tempdir Fatal Error:
> Contains Space" would pop-up. The program would open as a blank screen and
> usually make the rest of my laptop stall until it was shut off. I tried
> uninstalling and reinstalling the programs, however, the issue did not
> change. I checked my Microsoft username to make sure it did not "contain
> space" (as in spacebars) but it did not. I was hoping I would be able to
> get some help moving forward on how to get this fixed. Thank you for your
> help!
>
> Best,
> Kaitlyn Light
>
> [[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] Fatal Error: Contains Space

2022-09-22 Thread Ivan Krylov
On Wed, 21 Sep 2022 13:07:25 -0400
Kaitlyn Light  wrote:

> However, when I tried to open RStudio, a message saying " R_tempdir
> Fatal Error: Contains Space" would pop-up.

Does it work when you launch Rgui (part of R itself) instead of RStudio?

-- 
Best regards,
Ivan

__
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] Fatal Error: Contains Space

2022-09-22 Thread Eric Berger
Does this help?
https://stackoverflow.com/questions/72138987/r-studio-fatal-error-r-tempdircontains-space

On Thu, Sep 22, 2022 at 10:37 AM Kaitlyn Light  wrote:

> Hello!
> I recently downloaded R and RStudio to my windows laptop. I downloaded the
> correct version and made sure it was for windows and not mac. However, when
> I tried to open RStudio, a message saying " R_tempdir Fatal Error:
> Contains Space" would pop-up. The program would open as a blank screen and
> usually make the rest of my laptop stall until it was shut off. I tried
> uninstalling and reinstalling the programs, however, the issue did not
> change. I checked my Microsoft username to make sure it did not "contain
> space" (as in spacebars) but it did not. I was hoping I would be able to
> get some help moving forward on how to get this fixed. Thank you for your
> help!
>
> Best,
> Kaitlyn Light
>
> [[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] Fatal Error: Contains Space

2022-09-22 Thread Kaitlyn Light
Hello!
I recently downloaded R and RStudio to my windows laptop. I downloaded the
correct version and made sure it was for windows and not mac. However, when
I tried to open RStudio, a message saying " R_tempdir Fatal Error:
Contains Space" would pop-up. The program would open as a blank screen and
usually make the rest of my laptop stall until it was shut off. I tried
uninstalling and reinstalling the programs, however, the issue did not
change. I checked my Microsoft username to make sure it did not "contain
space" (as in spacebars) but it did not. I was hoping I would be able to
get some help moving forward on how to get this fixed. Thank you for your
help!

Best,
Kaitlyn Light

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