Re: [R] How to Reformat a dataframe

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

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

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

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

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

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

Very best all,

Chris

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


Hi Iris,

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

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


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

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

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

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

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

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

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

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

Dear friends,

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


[dput snipped]



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

to

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

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



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

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

Any help will be greatly appreciated.

Best regards,
Paul

 [[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide

http://www.R-project.org/posting-guide.html

and provide commented, minimal, self-contained, reproducible code.

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

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

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

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


__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Is it possible to get a downward pointing solid triangle plotting symbol in R?

2023-10-07 Thread Chris Evans via R-help
SO helpful. Thanks to all.  I _think_ the answer to Jeff's question may 
be "It should only be problematical on R earlier than 2.10". At least,

that's how I read this:

There is a portable way to have arbitrary text in character strings 
(only) in your R code, which is to supply them in Unicode as ‘\u’
escapes (or, rarely needed except for emojis, ‘\U’ escapes). If 
there are any characters not in the current encoding the parser
will encode the character string as UTF-8 and mark it as such. This 
applies also to character strings in datasets: they can be prepared
using ‘\u’ escapes or encoded in UTF-8 in a UTF-8 locale, or even 
converted to UTF-8 /via/ |iconv()|. If you do this, make sure you have

‘R (>= 2.10)’ (or later) in the ‘Depends’ field of the DESCRIPTION file.

(Quoting from 
https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Encoding-issues. 
Thanks for that pointer Jan.)


### using UTF to solve my issue (for R versions >= 2.10 I think)
library(tidyverse)
tibble(x = 2:9, y = 2:9, c = c(rep("A", 5), rep("B", 3))) %>%
  mutate(y1 = y + 1,
 y2 = y + 2) -> tmpTibPoints
tibble(x = c(1, 5, 5, 1), y = c(1, 1, 5, 5), a = rep("a", 4)) -> tmpTibArea1
tibble(x = c(5, 10, 10, 5), y = c(1, 1, 5, 5), a = rep("b", 4)) -> 
tmpTibArea2
tibble(x = c(1, 5, 5, 1), y = c(5, 5, 10, 10), a = rep("c", 4)) -> 
tmpTibArea3
tibble(x = c(5, 10, 10, 5), y = c(5, 5, 10, 10), a = rep("d", 4)) -> 
tmpTibArea4

bind_rows(tmpTibArea1,
   tmpTibArea2,
   tmpTibArea3,
   tmpTibArea4) -> tmpTibAreas

# Unicode characters for black up- and down-pointing characters
pts_shapes <- c("\U25B2", "\U25BC") |> setNames(c("A", "B"))
pts_colors <- c("blue", "red") |> setNames(c("A", "B"))

pts_shapes

ggplot() +
  ### this was the suggestion from Rui Barradas
  geom_point(data = tmpTibPoints,
 aes(x = x, y = y, color = c, shape = c),
 size = 6) +
  ### checking what happens using geom_text (for amusement really)
  geom_text(data = tmpTibPoints,
    aes(x = x, y = y1, label = c)) +
  ### and checking using the UTF characters in annotate() too (ditto)
  annotate(geom = "text", x = 2.5, y = 8.5, label = paste(pts_shapes, 
collapse = "  ")) +

  scale_shape_manual(values = pts_shapes) +
  scale_color_manual(values = pts_colors)

Output attached.  Thanks to Jan and Rui particularly.  R-help providing 
wide and deep R education as ever.



On 06/10/2023 17:05, Jeff Newmiller wrote:

Doesn't the outcome of this suggestion still depend on which fonts and output 
device you are using? ... and that is to some degree still system dependent...

On October 6, 2023 7:50:00 AM PDT, Rui Barradas  wrote:

Às 10:09 de 06/10/2023, Chris Evans via R-help escreveu:

The reason I am asking is that I would like to mark areas on a plot using 
geom_polygon() and aes(fill = variable) to fill various polygons forming the 
background of a plot with different colours. Then I would like to overlay that 
with points representing direction of change: improved, no reliable change, 
deteriorated. The obvious symbols to use for those three directions are an 
upward arrow, a circle or square and a downward pointing arrow.  There is a 
solid upward point triangle symbol in R (ph = 17) and there are both upward and 
downward pointing open triangle symbols (pch 21 and 25) but to fill those with 
a solid colour so they will be visible over the background requires that I use 
a fill aesthetic and that gets me a mess with the legend as I will have used a 
different fill mapping to fill the polygons.  This silly reprex shows the issue 
I think.

library(tidyverse)
tibble(x = 2:9, y = 2:9, c = c(rep("A", 5), rep("B", 3))) -> tmpTibPoints
tibble(x = c(1, 5, 5, 1), y = c(1, 1, 5, 5), a = rep("a", 4)) -> tmpTibArea1
tibble(x = c(5, 10, 10, 5), y = c(1, 1, 5, 5), a = rep("b", 4)) -> tmpTibArea2
tibble(x = c(1, 5, 5, 1), y = c(5, 5, 10, 10), a = rep("c", 4)) -> tmpTibArea3
tibble(x = c(5, 10, 10, 5), y = c(5, 5, 10, 10), a = rep("d", 4)) -> tmpTibArea4
bind_rows(tmpTibArea1,
    tmpTibArea2,
    tmpTibArea3,
    tmpTibArea4) -> tmpTibAreas
ggplot(data = tmpTib,
     aes(x = x, y = y)) +
    geom_polygon(data = tmpTibAreas,
     aes(x = x, y = y, fill = a)) +
    geom_point(data = tmpTibPoints,
   aes(x = x, y = y, fill = c),
   pch = 24,
   size = 6)

Does anyone know a way to create a solid downward pointing symbol?  Or another 
workaround?

TIA,

Chris


Hello,

Maybe you can solve the problem with unicode characters.
See the two scale_*_manual at the end of the plot.



# Unicode characters for black up- and down-pointing characters

Re: [R] Is it possible to get a downward pointing solid triangle plotting symbol in R?

2023-10-06 Thread Chris Evans via R-help
Thanks again Jan.  That is lovely and clean and I probably should have 
seen that option.


I had anxieties about the portability of using text.  (The function will 
end up in my
https://github.com/cpsyctc/CECPfuns package so I'd like it to be fairly 
immune to character

sets and different platforms in different countries.

I'm morphing this question a lot now but I guess it's still on topic 
really.  I know
I need to put in some time to understand the complexities of R and 
platforms (I'm
pretty exclusively on Linux, Ubuntu or Debian now so have mostly done 
the ostrich thing
about these issues though I do hit problems exchanging things with my 
Spanish speaking
colleagues).  Jan or anyone: any simple reassurance or pointers to 
resources I should

best use for homework about these issues?

TIA (again!)

Chris

On 06/10/2023 12:55, Jan van der Laan wrote:

You are right, sorry.

Another possible solution then: use geom_text instead of geom_point 
and use a triangle shape as text:


ggplot(data = tmpTibPoints,
   aes(x = x, y = y)) +
  geom_polygon(data = tmpTibAreas,
   aes(x = x, y = y, fill = a)) +
  geom_text(data = tmpTibPoints,
 aes(x = x, y = y, label = "▼", color = c),
 size = 6) + guides(color = FALSE)


[much snipped]


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

Work web site: https://www.psyctc.org/psyctc/
CORE site: http://www.coresystemtrust.org.uk/
Personal site: https://www.psyctc.org/pelerinage2016/

__
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] Is it possible to get a downward pointing solid triangle plotting symbol in R?

2023-10-06 Thread Chris Evans via R-help
Sadly, no.  Still shows the same legend with both sets of fill 
mappings.  I have found a workaround, sadly
much longer than yours (!) that does get me what I want but it is a real 
bodge.  Still interested to see
if there is a way to create a downward pointing solid symbol but here is 
my bodge using new_scale_fill()
and new_scale_color() from the ggnewscale package (many thanks to Elio 
Campitelli for that).


library(tidyverse)
library(ggnewscale) # allows me to change the scales used
tibble(x = 2:9, y = 2:9,
   ### I have used A:C to ensure the changes sort in the correct 
order to avoid the messes of using shape to scale an ordinal variable
   ### have to say that seems a case where it is perfectly sensible 
to map shapes to an ordinal variable, scale_shape_manual() makes

   ### this difficult hence this bodge
   c = c(rep("A", 5), "B", rep("C", 2)),
   change = c(rep("Deteriorated", 5), "No change", rep("Improved", 
2))) %>%

  ### this is just keeping the original coding but not used below
  mutate(change = ordered(change,
  levels = c("Deteriorated", "No change", 
"Improved"))) -> tmpTibPoints

### create the area mapping
tibble(x = c(1, 5, 5, 1), y = c(1, 1, 5, 5), a = rep("a", 4)) -> tmpTibArea1
tibble(x = c(5, 10, 10, 5), y = c(1, 1, 5, 5), a = rep("b", 4)) -> 
tmpTibArea2
tibble(x = c(1, 5, 5, 1), y = c(5, 5, 10, 10), a = rep("c", 4)) -> 
tmpTibArea3
tibble(x = c(5, 10, 10, 5), y = c(5, 5, 10, 10), a = rep("d", 4)) -> 
tmpTibArea4

bind_rows(tmpTibArea1,
  tmpTibArea2,
  tmpTibArea3,
  tmpTibArea4) -> tmpTibAreas
### now plot
ggplot(data = tmpTib,
   aes(x = x, y = y)) +
  geom_polygon(data = tmpTibAreas,
   aes(x = x, y = y, fill = a),
   alpha = .5) +
  scale_fill_manual(name = "Areas",
    values = c("orange", "purple", "yellow", "brown"),
    labels = letters[1:4]) +
  ### next two lines use ggnewscale functions to reset the scale mappings
  new_scale_fill() +
  new_scale_colour() +
  ### can now use the open triangles and fill aesthetic to map them
  geom_point(data = tmpTibPoints,
 aes(x = x, y = y, shape = c, fill = c, colour = c),
 size = 6) +
  ### use the ordered variable c to get mapping in desired order
  ### which, sadly, isn't the alphabetical order!
  scale_shape_manual(name = "Change",
   values = c("A" = 24,
  "B" = 23,
  "C" = 25),
   labels = c("Deteriorated",
  "No change",
  "Improved")) +
  scale_colour_manual(name = "Change",
   values = c("A" = "red",
  "B" = "grey",
  "C" = "green"),
   labels = c("Deteriorated",
  "No change",
  "Improved")) +
  scale_fill_manual(name = "Change",
   values = c("A" = "red",
  "B" = "grey",
      "C" = "green"),
   labels = c("Deteriorated",
  "No change",
  "Improved"))

That gives the attached plot which is really what I want.  Long bodge 
though!*

*

On 06/10/2023 11:50, Jan van der Laan wrote:


Does adding

, show.legend = c("color"=TRUE, "fill"=FALSE)

to the geom_point do what you want?

Best,
Jan

On 06-10-2023 11:09, Chris Evans via R-help wrote:

library(tidyverse)
tibble(x = 2:9, y = 2:9, c = c(rep("A", 5), rep("B", 3))) -> 
tmpTibPoints
tibble(x = c(1, 5, 5, 1), y = c(1, 1, 5, 5), a = rep("a", 4)) -> 
tmpTibArea1
tibble(x = c(5, 10, 10, 5), y = c(1, 1, 5, 5), a = rep("b", 4)) -> 
tmpTibArea2
tibble(x = c(1, 5, 5, 1), y = c(5, 5, 10, 10), a = rep("c", 4)) -> 
tmpTibArea3
tibble(x = c(5, 10, 10, 5), y = c(5, 5, 10, 10), a = rep("d", 4)) -> 
tmpTibArea4

bind_rows(tmpTibArea1,
   tmpTibArea2,
   tmpTibArea3,
   tmpTibArea4) -> tmpTibAreas
ggplot(data = tmpTib,
    aes(x = x, y = y)) +
   geom_polygon(data = tmpTibAreas,
    aes(x = x, y = y, fill = a)) +
   geom_point(data = tmpTibPoints,
  aes(x = x, y = y, fill = c),
  pch = 24,
  size = 6)



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

Work web site: https://www.psyctc.org/psyctc/
CORE site: http://www.coresystemtrust.org.uk/
Personal site: https://www.psyctc.org/pelerinage2016/
__
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] Is it possible to get a downward pointing solid triangle plotting symbol in R?

2023-10-06 Thread Chris Evans via R-help
The reason I am asking is that I would like to mark areas on a plot 
using geom_polygon() and aes(fill = variable) to fill various polygons 
forming the background of a plot with different colours. Then I would 
like to overlay that with points representing direction of change: 
improved, no reliable change, deteriorated. The obvious symbols to use 
for those three directions are an upward arrow, a circle or square and a 
downward pointing arrow.  There is a solid upward point triangle symbol 
in R (ph = 17) and there are both upward and downward pointing open 
triangle symbols (pch 21 and 25) but to fill those with a solid colour 
so they will be visible over the background requires that I use a fill 
aesthetic and that gets me a mess with the legend as I will have used a 
different fill mapping to fill the polygons.  This silly reprex shows 
the issue I think.


library(tidyverse)
tibble(x = 2:9, y = 2:9, c = c(rep("A", 5), rep("B", 3))) -> tmpTibPoints
tibble(x = c(1, 5, 5, 1), y = c(1, 1, 5, 5), a = rep("a", 4)) -> tmpTibArea1
tibble(x = c(5, 10, 10, 5), y = c(1, 1, 5, 5), a = rep("b", 4)) -> 
tmpTibArea2
tibble(x = c(1, 5, 5, 1), y = c(5, 5, 10, 10), a = rep("c", 4)) -> 
tmpTibArea3
tibble(x = c(5, 10, 10, 5), y = c(5, 5, 10, 10), a = rep("d", 4)) -> 
tmpTibArea4

bind_rows(tmpTibArea1,
  tmpTibArea2,
  tmpTibArea3,
  tmpTibArea4) -> tmpTibAreas
ggplot(data = tmpTib,
   aes(x = x, y = y)) +
  geom_polygon(data = tmpTibAreas,
   aes(x = x, y = y, fill = a)) +
  geom_point(data = tmpTibPoints,
 aes(x = x, y = y, fill = c),
 pch = 24,
 size = 6)

Does anyone know a way to create a solid downward pointing symbol?  Or 
another workaround?


TIA,

Chris

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

Work web site: https://www.psyctc.org/psyctc/
CORE site: http://www.coresystemtrust.org.uk/
Personal site: https://www.psyctc.org/pelerinage2016/

__
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 "STRING_ELT() can only be applied to a 'character vector', not a 'list'" from rmarkdown

2023-08-25 Thread Chris Evans via R-help
Fascinating and, as ever, extremely helpful Ivan.  Duncan Murdoch nudged 
me in the right direction and for now

the solution for me is that if I don't have the lines:

description: |
  CE's pages "blog posts" about using R

in _site.yaml that have been there for ages, then everything works.  If 
I put _anything_ in there, including
two lines copied and pasted from a brand new distill site instead of 
those two lines then the error returns
so I think something else is awry in my main distill site that has 
rendered the knitting of the site
allergic to having a description in _site.yaml.  I should probably go 
further to try to debug this but across
this and the trouble I was having with the mirror.infomaniak.ch ubuntu 
mirror (R-sig-Debian thread, another
you helped with, has details of that!) I have lost too much time 
debugging in the last week and desperately

need to get back to my main work (and work I'm more competent to do!)

Many thanks again,

Chris

On 25/08/2023 15:48, Ivan Krylov wrote:

В Fri, 25 Aug 2023 11:49:03 +0200
Chris Evans via R-help  пишет:


Hm.  I tried that butI really don't know what to make of what it's
telling me.  It seemed to me that I was just stepping through the
same bits of code (with the warning that the debugger didn't
have the source so I'm not really sure what it was showing me!)

write_feed_xml is an internal function in the "distill" package. It's
supposed to create the RSS feed for a whole set of web pages. Somehow
it ends up trying to assign a list to xml2::xml_text(some_xml_node),
where only assigning a character string would make sense.

The function contains quite a lot of assignments like this. Some of
them come from site_config (which comes from _site.yml?), others are
taken from the article contents and metadata.

If you run options(error = recover) in a fresh R session before
knitting your website, you should get a debugger menu right at the
point where the code crashes. Try to find out what is the XML node
where the text is being assigned and what is the list that ends up
being assigned into it. You will also get the option to see the
variables in the callers of the failing function, which may shed some
light on the situation too.


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

Work web site: https://www.psyctc.org/psyctc/
CORE site: http://www.coresystemtrust.org.uk/
Personal site: https://www.psyctc.org/pelerinage2016/

__
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 "STRING_ELT() can only be applied to a 'character vector', not a 'list'" from rmarkdown

2023-08-25 Thread Chris Evans via R-help



On 24/08/2023 21:05, Duncan Murdoch wrote:

On 24/08/2023 1:58 p.m., Chris Evans via R-help wrote:

I have an Rmarkdown file which is part of my distill "Rblog"
(https://www.psyctc.org/Rblog/).  It was knitting fine until last week,
now knitting terminates with this:

Rscript -e 'rmarkdown::render("creating-a-shiny-server.Rmd")'


processing file: creating-a-shiny-server.Rmd

output file: creating-a-shiny-server.knit.md

/usr/bin/pandoc +RTS -K512m -RTS creating-a-shiny-server.knit.md --to
html5 --from markdown+autolink_bare_uris+tex_math_single_backslash
--output creating-a-shiny-server.html --lua-filter
/home/chris/R/x86_64-pc-linux-gnu-library/4.3/rmarkdown/rmarkdown/lua/pagebreak.lua 


--lua-filter
/home/chris/R/x86_64-pc-linux-gnu-library/4.3/rmarkdown/rmarkdown/lua/latex-div.lua 


--wrap preserve --standalone --table-of-contents --toc-depth 4
--variable toc-float=1 --highlight-style
/home/chris/R/x86_64-pc-linux-gnu-library/4.3/distill/rmarkdown/templates/distill_article/resources/arrow.theme 


--template
/home/chris/R/x86_64-pc-linux-gnu-library/4.3/distill/rmarkdown/templates/distill_article/resources/default.html 


'--metadata=link-citations:true' --include-in-header
/tmp/Rtmp7WHAIE/fileb39b179ffd801html --include-in-header
/tmp/Rtmp7WHAIE/fileb39b15b1e3532html --include-in-header
/tmp/Rtmp7WHAIE/fileb39b13239652chtml --include-in-header
/tmp/Rtmp7WHAIE/fileb39b1581627e8html --include-in-header
/tmp/Rtmp7WHAIE/fileb39b12850a405html --include-before-body
/tmp/Rtmp7WHAIE/fileb39b16f8d72a6html --include-before-body
/tmp/Rtmp7WHAIE/fileb39b16ce17d77html --include-before-body
/tmp/Rtmp7WHAIE/fileb39b12f851f86html --include-after-body
/tmp/Rtmp7WHAIE/fileb39b14447b7b1html --include-after-body
/tmp/Rtmp7WHAIE/fileb39b143ff6632html --include-after-body
/tmp/Rtmp7WHAIE/fileb39b1345b1dddhtml --mathjax --variable
'mathjax-url=https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' 


--include-in-header /tmp/Rtmp7WHAIE/rmarkdown-strb39b160f391de.html
--include-in-header /tmp/Rtmp7WHAIE/fileb39b164361476html
Error in `xml_text<-.xml_node`(`*tmp*`, value = text) :
    STRING_ELT() can only be applied to a 'character vector', not a 
'list'

Calls:  ... write_feed_xml -> add_child ->  ->
xml_text<-.xml_node
In addition: There were 20 warnings (use warnings() to see them)
Execution halted


That function `xml_text<-.xml_node` is in the xml2 package, which was 
updated in early July.  Do you think the error has been happening 
since you updated your copy, or did something else trigger it?


I wish I had good clues from that. Everything with the "blog" was 
working fine until the 21st. Since then (yesterday) I have fixed an 
issue (down to the mirror.infomaniak.ch server I was using)
that had meant a lot of my Ubuntu packages were out of date but this 
problem started before I fixed that, by the 23rd.  I update the OS daily 
(but that wasn't working for deb packages until yesterday
because of the mirror problem) and I also R packages daily but I don't 
watch what updates and what doesn't.  I guess I could put that on a cron 
job with an Email to self to keep track of it in future.




One way to debug this is as follows.  With the file in the directory 
where the error occurs, start R (or RStudio, it shouldn't matter), and 
run


  debug(xml2:::`xml_text<-.xml_node`)
  rmarkdown::render("creating-a-shiny-server.Rmd")

If you're lucky, the error will happen on the first call to that 
function, and you can trace into it to see what's so weird.  If you're 
unlucky, it will happen after many calls.  Then you'll probably have 
to use trace() to identify which call causes problems (increment and 
print a counter on each call), then break just before the bad call and 
try to see what went wrong.
Hm.  I tried that butI really don't know what to make of what it's 
telling me.  It seemed to me that I was just stepping through the same 
bits of code (with the warning that the debugger didn't
have the source so I'm not really sure what it was showing me!) Sorry, 
showing my ignorance of that level of R debugging.  Can you give me any 
more advice about that?  Or perhaps _if_ the code

does give the same error for you, you'd be able to see quickly what I can't?

Deeply grateful for the help and sorry I'm not up to scratch on my 
side.  I'd really appreciate any other thoughts as I'm stuck with the 
blog for now!


Chris



Duncan Murdoch


[rest snipped]

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

Work web site: https://www.psyctc.org/psyctc/
CORE site: http://www.coresystemtrust.org.uk/
Personal site: https://www.psyctc.org/pelerinage2016/

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

Re: [R] Error "STRING_ELT() can only be applied to a 'character vector', not a 'list'" from rmarkdown

2023-08-25 Thread Chris Evans via R-help

Thanks Duncan.  I've pushed the whole collection to:

https://github.com/cpsyctc/Rblog

On 24/08/2023 20:54, Duncan Murdoch wrote:

Could you post a link to the source for that Rmd file?

Duncan Murdoch


[rest snipped]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Error "STRING_ELT() can only be applied to a 'character vector', not a 'list'" from rmarkdown

2023-08-24 Thread Chris Evans via R-help
I have an Rmarkdown file which is part of my distill "Rblog" 
(https://www.psyctc.org/Rblog/).  It was knitting fine until last week,

now knitting terminates with this:

Rscript -e 'rmarkdown::render("creating-a-shiny-server.Rmd")'


processing file: creating-a-shiny-server.Rmd

output file: creating-a-shiny-server.knit.md

/usr/bin/pandoc +RTS -K512m -RTS creating-a-shiny-server.knit.md --to 
html5 --from markdown+autolink_bare_uris+tex_math_single_backslash 
--output creating-a-shiny-server.html --lua-filter 
/home/chris/R/x86_64-pc-linux-gnu-library/4.3/rmarkdown/rmarkdown/lua/pagebreak.lua 
--lua-filter 
/home/chris/R/x86_64-pc-linux-gnu-library/4.3/rmarkdown/rmarkdown/lua/latex-div.lua 
--wrap preserve --standalone --table-of-contents --toc-depth 4 
--variable toc-float=1 --highlight-style 
/home/chris/R/x86_64-pc-linux-gnu-library/4.3/distill/rmarkdown/templates/distill_article/resources/arrow.theme 
--template 
/home/chris/R/x86_64-pc-linux-gnu-library/4.3/distill/rmarkdown/templates/distill_article/resources/default.html 
'--metadata=link-citations:true' --include-in-header 
/tmp/Rtmp7WHAIE/fileb39b179ffd801html --include-in-header 
/tmp/Rtmp7WHAIE/fileb39b15b1e3532html --include-in-header 
/tmp/Rtmp7WHAIE/fileb39b13239652chtml --include-in-header 
/tmp/Rtmp7WHAIE/fileb39b1581627e8html --include-in-header 
/tmp/Rtmp7WHAIE/fileb39b12850a405html --include-before-body 
/tmp/Rtmp7WHAIE/fileb39b16f8d72a6html --include-before-body 
/tmp/Rtmp7WHAIE/fileb39b16ce17d77html --include-before-body 
/tmp/Rtmp7WHAIE/fileb39b12f851f86html --include-after-body 
/tmp/Rtmp7WHAIE/fileb39b14447b7b1html --include-after-body 
/tmp/Rtmp7WHAIE/fileb39b143ff6632html --include-after-body 
/tmp/Rtmp7WHAIE/fileb39b1345b1dddhtml --mathjax --variable 
'mathjax-url=https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' 
--include-in-header /tmp/Rtmp7WHAIE/rmarkdown-strb39b160f391de.html 
--include-in-header /tmp/Rtmp7WHAIE/fileb39b164361476html

Error in `xml_text<-.xml_node`(`*tmp*`, value = text) :
  STRING_ELT() can only be applied to a 'character vector', not a 'list'
Calls:  ... write_feed_xml -> add_child ->  -> 
xml_text<-.xml_node

In addition: There were 20 warnings (use warnings() to see them)
Execution halted

It's the same if I do it in Rstudio or from the command line like that 
so I think I can safely say it's not an Rstudio issue.


The same happens with other Rmd files in the distill _posts directory.

The really weird aspects are:

1) the html _is_ created fine (but if running in Rstudio it doesn't 
transfer to showing you the html)


2) the error message is only there if I run the Rmd in that directory, 
i.e. the 2023-08-19-creating-a-shiny-server directory in
_posts or in _posts above that, but I move it up againif I move it to 
higher or other directories it works fine:


Rscript -e 'rmarkdown::render("creating-a-shiny-server.Rmd")'


processing file: creating-a-shiny-server.Rmd

output file: creating-a-shiny-server.knit.md

/usr/bin/pandoc +RTS -K512m -RTS creating-a-shiny-server.knit.md --to 
html5 --from markdown+autolink_bare_uris+tex_math_single_backslash 
--output creating-a-shiny-server.html --lua-filter 
/home/chris/R/x86_64-pc-linux-gnu-library/4.3/rmarkdown/rmarkdown/lua/pagebreak.lua 
--lua-filter 
/home/chris/R/x86_64-pc-linux-gnu-library/4.3/rmarkdown/rmarkdown/lua/latex-div.lua 
--wrap preserve --standalone --table-of-contents --toc-depth 4 
--variable toc-float=1 --highlight-style 
/home/chris/R/x86_64-pc-linux-gnu-library/4.3/distill/rmarkdown/templates/distill_article/resources/arrow.theme 
--template 
/home/chris/R/x86_64-pc-linux-gnu-library/4.3/distill/rmarkdown/templates/distill_article/resources/default.html 
'--metadata=link-citations:true' --include-in-header 
/tmp/RtmpGMqTCm/fileb43a436c6281fhtml --include-in-header 
/tmp/RtmpGMqTCm/fileb43a455b7e5bdhtml --include-in-header 
/tmp/RtmpGMqTCm/fileb43a461db1ea1html --include-in-header 
/tmp/RtmpGMqTCm/fileb43a474a6e703html --include-before-body 
/tmp/RtmpGMqTCm/fileb43a494bd34html --include-before-body 
/tmp/RtmpGMqTCm/fileb43a42fbfec67html --include-before-body 
/tmp/RtmpGMqTCm/fileb43a4273d84ebhtml --include-after-body 
/tmp/RtmpGMqTCm/fileb43a4725e33cahtml --include-after-body 
/tmp/RtmpGMqTCm/fileb43a43d386888html --include-after-body 
/tmp/RtmpGMqTCm/fileb43a43f19b459html --mathjax --variable 
'mathjax-url=https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' 
--include-in-header /tmp/RtmpGMqTCm/rmarkdown-strb43a43033daa0.html 
--include-in-header /tmp/RtmpGMqTCm/fileb43a42807e405html


Output created: creating-a-shiny-server.html
Warning message:
In as.character.POSIXt(as.POSIXlt(x), ...) :
  as.character(td, ..) no longer obeys a 'format' argument; use 
format(td, ..) ?


The 20 warnings are:

> warnings()
Warning messages:
1: In as.character.POSIXt(as.POSIXlt(x), ...) :
  as.character(td, ..) no longer obeys a 'format' argument; use 
format(td, ..) ?

2: In 

[R] Puzzled by results from base::rank()

2023-08-11 Thread Chris Evans via R-help
I understand that the default ties.method is "average".  Here is what I 
get, expanding a bit on the help page example. Running R 4.3.1 on Ubuntu 
22.04.2.


> x2 <- c(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5)
> rank(x2)
 [1]  4.5  1.5  6.0  1.5  8.0 11.0  3.0 10.0  8.0  4.5  8.0

OK so the ties, each of with two members, are ranked to their mean.

So now I turn one tie from a twin to a triplet:

> x3 <- c(x2, 3)
> rank(x3)
 [1]  5.0  1.5  7.0  1.5  9.0 12.0  3.0 11.0  9.0  5.0  9.0  5.0
> sprintf("%4.3f", rank(x3))
 [1] "5.000"  "1.500"  "7.000"  "1.500"  "9.000"  "12.000" "3.000"  
"11.000"

 [9] "9.000"  "5.000"  "9.000"  "5.000"

The doublet is still given the mean of the values but the triplet is 
rounded up.  What am I missing here?!


TIA,

Chris

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

Work web site: https://www.psyctc.org/psyctc/
CORE site: http://www.coresystemtrust.org.uk/
Personal site: https://www.psyctc.org/pelerinage2016/

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to plot both lines and points by group on ggplot2

2023-07-01 Thread Chris Evans via R-help

[Whoops, forgot that default reply-to is to the sender.]


On 01/07/2023 19:20, Luigi Marongiu wrote:

Hello,
I have a dataframe with measurements stratified by the concentration
of a certain substance. I would like to plot the points of the
measures and connect the points within each series of concentrations.
When I launch ggplot2 I get the error
```
geom_path: Each group consists of only one observation. Do you need to
adjust the
group aesthetic?
```
and no lines are drawn.
Where am I going wrong?
Thank you
Luigi

```
df = data.frame(Conc = c(rep(1, 3), rep(2, 3), rep(5, 3)),
Time = rep(1:3, 3),
Value = c(0.91, 0.67, 0.71, 0.91, 0.65, 0.74, 0.95, 0.67, 0.67))
df$Time <- as.factor(df$Time)
levels(df$Time) = c(1, 4, 24)
df$Conc <- as.factor(df$Conc)
levels(df$Conc) = c(1, 2, 5)
library(ggplot2)
ggplot(df, aes(x=Time, y=Value, colour=Conc)) +
geom_point(size=6) +
geom_line(aes(x=Time, y=Value, colour=Conc)) +
scale_colour_manual(values = c("darkslategray3", "darkslategray4",
"deepskyblue4")) +
ggtitle("Working example") +
xlab(expression(bold("Time (h)"))) +
ylab(expression(bold("Concentration (mM)")))
```


If I understand what you want then I think all you need is to add "group 
= Conc".  That's what that message is telling you.


df = data.frame(Conc = c(rep(1, 3), rep(2, 3), rep(5, 3)),
    Time = rep(1:3, 3),
    Value = c(0.91, 0.67, 0.71, 0.91, 0.65, 0.74, 0.95, 
0.67, 0.67))

df$Time <- as.factor(df$Time)
levels(df$Time) = c(1, 4, 24)
df$Conc <- as.factor(df$Conc)
levels(df$Conc) = c(1, 2, 5)
library(ggplot2)
ggplot(df, aes(x=Time, y=Value, colour=Conc, group = Conc)) +
  geom_point(size=6) +
  geom_line(aes(x=Time, y=Value, colour=Conc)) +
  scale_colour_manual(values = c("darkslategray3", "darkslategray4",
 "deepskyblue4")) +
  ggtitle("Working example") +
  xlab(expression(bold("Time (h)"))) +
  ylab(expression(bold("Concentration (mM)")))

Very  best,


Chris

--
Chris Evans (he/him)
Visiting Professor, UDLA, Quito, Ecuador & Honorary Professor, University of 
Roehampton, London, UK.
Work web site: https://www.psyctc.org/psyctc/
CORE site: http://www.coresystemtrust.org.uk/
Personal site: https://www.psyctc.org/pelerinage2016/
Emeetings (Thursdays): https://www.psyctc.org/psyctc/booking-meetings-with-me/
(Beware: French time, generally an hour ahead of UK)


__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Help with regex replacements

2023-06-27 Thread Chris Evans via R-help

Magic!

tmp %>%
  as_tibble() %>%
  rename(Text = value) %>%
  mutate(Text = str_replace_all(Text, fixed("."), "")) %>%
  # filter(row_number() < 4) %>%
  mutate(Text2 = gsub("((|/)[[:alnum:]]+)|(\\([[:alnum:]-]+\\))", 
"", Text))


Which (as you have already shown!) gave me this:

# A tibble: 7 × 2
  Text Text2
   
1 "Я досяг того, чого хотів"  "Я досяг того, чого 
хотів"
2 "Мені вдалося зробити бажане"   "Мені вдалося зробити 
бажане"
3 "Я досяг (досягла) того, чого хотів (хотіла)"   "Я досяг того, чого 
хотів "
4 "Я досяг(-ла) речей, яких хотілося досягти" "Я досяг речей, яких 
хотілося досягти"
5 "Я досяг/ла того, чого хотів/ла"    "Я досяг того, чого 
хотів"
6 "Я досяг\\досягла того, чого прагнув\\прагнула" "Я досяг того, чого 
прагнув"
7 "Я досягнув(ла) того, чого хотів(ла)"   "Я досягнув того, чого 
хотів"


perfect and I will spend some time tomorrow unpacking that regex and 
trying to drive the learning points into my thick skull!


Deeply indebted, as so often here though generally only when I'm reading 
others questions and the answers!


Chris

On 27/06/2023 20:48, Bert Gunter wrote:
OK, so you want parentheses, not "brackets" + I think I misinterpreted 
your specification, which I think is actually incomplete. Based on 
what I think you meant, how does this work:


gsub("((|/)[[:alnum:]]+)|(\\([[:alnum:]-]+\\))", "",tmp$Text)
[1] "Я досяг того, чого хотів"              "Мені вдалося\nзробити 
бажане"
[3] "Я досяг  того, чого хотів "            "Я\nдосяг речей, яких 
хотілося досягти"

[5] "Я досяг того, чого\nхотів"             "Я досяг того, чого прагнув"
[7] "Я\nдосягнув того, чого хотів"

If you want it without the \n's, cat the above to get:
cat(gsub("((|/)[[:alnum:]]+)|(\\([[:alnum:]-]+\\))", "",tmp$Text))

Я досяг того, чого хотів Мені вдалося
зробити бажане Я досяг  того, чого хотів  Я
досяг речей, яких хотілося досягти Я досяг того, чого
хотів Я досяг того, чого прагнув Я
досягнув того, чого хотів

Cheers,
Bert

On Tue, Jun 27, 2023 at 11:09 AM Bert Gunter  
wrote:


Does this do it for you (or get you closer):

 gsub("\\[.*\\]|[] |/ ","",tmp$Text)
[1] "Я досяг того, чого хотів"
    [2] "Мені вдалося\nзробити бажане"
[3] "Я досяг (досягла) того, чого хотів (хотіла)"
[4] "Я\nдосяг(-ла) речей, яких хотілося досягти"
[5] "Я досяг/ла того, чого\nхотів/ла"
[6] "Я досяг\\досягла того, чого прагнув\\прагнула"
[7] "Я\nдосягнув(ла) того, чого хотів(ла)"

On Tue, Jun 27, 2023 at 10:16 AM Chris Evans via R-help
 wrote:

I am sure this is easy for people who are good at regexps but I'm
failing with it.  The situation is that I have hundreds of
lines of
Ukrainian translations of some English. They contain things
like this:

1"Я досяг того, чого хотів"2"Мені вдалося зробити бажане"3"Я
досяг
(досягла) того, чого хотів (хотіла)"4"Я досяг(-ла) речей, яких
хотілося
досягти"5"Я досяг/ла того, чого хотів/ла"6"Я досяг\\досягла
того, чого
прагнув\\прагнула."7"Я досягнув(ла) того, чого хотів(ла)"

Using dput():

tmp <- structure(list(Text = c("Я досяг того, чого хотів",
"Мені вдалося
зробити бажане", "Я досяг (досягла) того, чого хотів
(хотіла)", "Я
досяг(-ла) речей, яких хотілося досягти", "Я досяг/ла того, чого
хотів/ла", "Я досяг\\досягла того, чого прагнув\\прагнула", "Я
досягнув(ла) того, чого хотів(ла)" )), row.names = c(NA, -7L),
class =
c("tbl_df", "tbl", "data.frame" )) Those show four different ways
translators have handled gendered words: 1) Ignore them and (I'm
guessing) only give the masculine 2) Give the feminine form of
the word
(or just the feminine suffix) in brackets 3) Give the feminine
form/suffix prefixed by a forward slash 4) Give the feminine
form/suffix
prefixed by backslash (here a double backslash) I would like
just to
drop all these feminine gendered options. (Don't worry,
they'll get back
in later.) So I would like to replace 1) anything between
brackets with
nothing! 2) anything between a forward slash and the next
space with
nothing 3) anything between a backslash and the next space
w

Re: [R] Help with regex replacements

2023-06-27 Thread Chris Evans via R-help

Thanks Avi (I am a keen follower or your, and other stalwart helpers here).

On 27/06/2023 18:27, avi.e.gr...@gmail.com wrote:

Chris,

Consider breaking up your task into multiple passes.


Sorry, I could have explained more of what I had tried.  I never know 
how long to make things here.


I had been doing that. My plan was to pick them off, one by one but I 
think I am banging my head on a fundamental incomprehension on my part.



And do them in whatever order preserves what you need.

Agree.

First, are you talking about brackets as in square brackets, or as in your 
example, parentheses?

Sorry, always get that wrong, parentheses. Mea culpa.

If you are sure you have no nested brackets, your requirement seems to be that 
anything matching [ stuff ] be replaced with nothing. Or if using parentheses, 
something similar.
> 99% sure there are no nested parentheses.  However, there are lines 
with none, one or sometimes (as in the little reprex) more than one set 
of parentheses.

Your issue here is both sets of symbols are special so you must escape them so 
they are seen as part of the pattern and not the instructions.
So, sorry to be stupid but I thought I was doing that using "\(.*\)"  
Could you reply showing me the correct escaping and the correct 
replacing?  I was using str_replace_all() but happy to use gsub() if 
that's easier/safer/better.

The idea would be to pass through the text once and match all instances on a 
line and then replace with nothing or whatever is needed.

Nothing.

  But there is no guarantee some of your constructs will be on the same line 
completely so be wary.


Totally agree.

I also see that my Emailer (Thunderbird) despite my exhorting it not to, 
mangled the Email.  Have tried to fix that.  The mess below should have 
said:


I am sure this is easy for people who are good at regexps but I'm
failing with it.  The situation is that I have hundreds of lines of
Ukrainian translations of some English. They contain things like this:

1"Я досяг того, чого хотів"
2"Мені вдалося зробити бажане"
3"Я досяг (досягла) того, чого хотів (хотіла)"
4"Я досяг(-ла) речей, яких хотілося досягти"
5"Я досяг/ла того, чого хотів/ла"
6"Я досяг\\досягла того, чогопрагнув\\прагнула."7"Я досягнув(ла) того, 
чого хотів(ла)"


Using dput():

tmp <- structure(list(Text = c("Я досяг того, чого хотів",
"Мені вдалося зробити бажане",
"Я досяг (досягла) того, чого хотів (хотіла)",
"Я досяг(-ла) речей, яких хотілося досягти",
"Я досяг/ла того, чого хотів/ла",
"Я досяг\\досягла того, чого прагнув\\прагнула",
"Я досягнув(ла) того, чого хотів(ла)" )),
row.names = c(NA, -7L), class = c("tbl_df", "tbl", "data.frame" ))

Those show four different ways translators have handled gendered words:

1) Ignore them and (I'm guessing) only give the masculine
2) Give the feminine form of the word (or just the feminine suffix) in 
brackets

3) Give the feminine form/suffix prefixed by a forward slash
4) Give the feminine form/suffix prefixed by backslash (here a double 
backslash)


I would like just to drop all these feminine gendered options. (Don't 
worry,

they'll get back in later.)

So I would like to replace
1) anything between brackets with nothing!
2) anything between a forward slash and the next space with nothing
3) anything between a backslash and the next space with nothing
but preserving the rest of the text. I have been trying to achieve this
using str_replace_all() but I am failing utterly.

Here's a silly little example of my failures.
This was just trying to get the text I wanted to
replace (as I was trying to simplify the issues for my tired wetware):

> tmp %>%
+ as_tibble() %>%
+ rename(Text = value) %>%
+ mutate(Text = str_replace_all(Text, fixed("."), "")) %>%
+ filter(row_number() < 4) %>%
+ mutate(Text2 = str_replace(Text, "\\(.*\\)", "\\1"))

Error in `mutate()`:ℹIn argument: `Text2 = str_replace(Text, "\\(.*\\)",
"\\1")`.

Caused by error in `stri_replace_first_regex()`:!
Trying to access the index that is out of bounds. 
(U_INDEX_OUTOFBOUNDS_ERROR)

Run `rlang::last_trace()` to see where the error occurred.

I have tried gurgling around the internet but am striking out so 
throwing myself on

the list. Apologies if this is trivial but I'd hate to have to clean
these hundreds of lines by hand though it's starting to look as if I'd
achieve that faster by hand than I will by banging my ignorance of R
regexp syntax on the problem. TIA, Chris




  


-Original Message-
From: R-help  On Behalf Of Chris Evans via R-help
Sent: Tuesday, June 27, 2023 1:16 PM
To: r-help@r-project.org
Subject: [R] Help with regex replacements

I am sure this is easy for people who are good at regexps but I'm
failing with it.  The situation is th

[R] Help with regex replacements

2023-06-27 Thread Chris Evans via R-help
I am sure this is easy for people who are good at regexps but I'm 
failing with it.  The situation is that I have hundreds of lines of 
Ukrainian translations of some English. They contain things like this:


1"Я досяг того, чого хотів"2"Мені вдалося зробити бажане"3"Я досяг 
(досягла) того, чого хотів (хотіла)"4"Я досяг(-ла) речей, яких хотілося 
досягти"5"Я досяг/ла того, чого хотів/ла"6"Я досяг\\досягла того, чого 
прагнув\\прагнула."7"Я досягнув(ла) того, чого хотів(ла)"


Using dput():

tmp <- structure(list(Text = c("Я досяг того, чого хотів", "Мені вдалося 
зробити бажане", "Я досяг (досягла) того, чого хотів (хотіла)", "Я 
досяг(-ла) речей, яких хотілося досягти", "Я досяг/ла того, чого 
хотів/ла", "Я досяг\\досягла того, чого прагнув\\прагнула", "Я 
досягнув(ла) того, чого хотів(ла)" )), row.names = c(NA, -7L), class = 
c("tbl_df", "tbl", "data.frame" )) Those show four different ways 
translators have handled gendered words: 1) Ignore them and (I'm 
guessing) only give the masculine 2) Give the feminine form of the word 
(or just the feminine suffix) in brackets 3) Give the feminine 
form/suffix prefixed by a forward slash 4) Give the feminine form/suffix 
prefixed by backslash (here a double backslash) I would like just to 
drop all these feminine gendered options. (Don't worry, they'll get back 
in later.) So I would like to replace 1) anything between brackets with 
nothing! 2) anything between a forward slash and the next space with 
nothing 3) anything between a backslash and the next space with nothing 
but preserving the rest of the text. I have been trying to achieve this 
using str_replace_all() but I am failing utterly. Here's a silly little 
example of my failures. This was just trying to get the text I wanted to 
replace (as I was trying to simplify the issues for my tired wetware): > 
tmp %>%+ as_tibble() %>% + rename(Text = value) %>% + mutate(Text = 
str_replace_all(Text, fixed("."), "")) %>% + filter(row_number() < 4) 
%>% + mutate(Text2 = str_replace(Text, "\\(.*\\)", "\\1")) Errorin 
`mutate()`:ℹIn argument: `Text2 = str_replace(Text, "\\(.*\\)", 
"\\1")`.Caused by error in `stri_replace_first_regex()`:!Trying to 
access the index that is out of bounds. (U_INDEX_OUTOFBOUNDS_ERROR) Run 
`rlang::last_trace()` to see where the error occurred. I have tried 
gurgling around the internet but am striking out so throwing myself on 
the list. Apologies if this is trivial but I'd hate to have to clean 
these hundreds of lines by hand though it's starting to look as if I'd 
achieve that faster by hand than I will by banging my ignorance of R 
regexp syntax on the problem. TIA, Chris


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

Work web site: https://www.psyctc.org/psyctc/
CORE site: http://www.coresystemtrust.org.uk/
Personal site: https://www.psyctc.org/pelerinage2016/

__
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] Asking about R "Security" ..

2023-05-17 Thread Chris Evans via R-help
Fortune nomination (if trimmed a little) ... thanks to Martin, Ivan and 
so many others who never cease to teach me things here!

(As an ex-NHS person who has used R for countless centuries, slowly less 
awfully courtesy of this list and other angeles, I contacted Ms Majid 
off list to see if I can help!)

Chris

On 17/05/2023 11:11, Martin Maechler wrote:
>> Ivan Krylov
>>  on Wed, 17 May 2023 11:52:27 +0300 writes:
>  > В Tue, 16 May 2023 13:47:19 +
>  > "MAJID, Ayesha \(NHS ENGLAND - X26\) via R-help"
>
> [ . ]
> [ . ]
> [ .. helpful & useful answers / pointers to public information .. ]
> [ . ]
> [ . ]
>
>  > (Did you mean to ask these questions at the public mailing list open
>  > for J. Random Hackers like me to answer?)
>
>  > --
>  > Best regards,
>  > Ivan
>
> Actually, people typically ask "the R Foundation" or even
> individual RF / R-core members such as me about this ...
>
> ... as if we were a company with staff to answer such questions;
> but we (volunteering individuals) really do *not* have the time
> resources for that,
> and consequently, also in my function---shared with another few
> individuals---as gatekeeper to the R foundation / R core / R webmaster
> e-mail addresses, I typically deflect such questions to the
> public web sites *and* public e-mail lists.
>
> The big advantage of this approach is that at least the answers
> are findable by web searches in the future, and so, hopefully
> have to be answered less frequently by volunteers as you, Ivan,
> for whom we are really very grateful.
>
> Martin
>
> __
> 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 guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
-- 
Chris Evans (he/him)
Visiting Professor, UDLA, Quito, Ecuador & Honorary Professor, 
University of Roehampton, London, UK.
Work web site: https://www.psyctc.org/psyctc/
CORE site: http://www.coresystemtrust.org.uk/
Personal site: https://www.psyctc.org/pelerinage2016/


[[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] Reg: Help regarding ggplot2

2023-05-02 Thread Chris Evans via R-help
I'm only seeing six dates, by the look of them five at one day intervals 
and the last three days later.  According to my calendar those were 
weekdays in 2005.  I don't see much "volatility" there and that's not a 
term I'm familiar with, I suspect it's econometric.  I don't know what 
you're looking for and I think we're starting to get into issues about 
interpretation of data not issues about using R so off list.  I think 
you need statistical or econometric support from somewhere.

Good luck!

Chris

On 02/05/2023 17:50, Upananda Pani wrote:
> Hi Chrish,
>
> I am grateful to you for your reply. Your code is working fine.
> Moreover, you have guided me how to improve my knowledge, I appreciate
> it. I will be very careful next time.
> The data which i am working on is given below:
>
> dput(head(data_vol3))
> structure(list(index = structure(c(12786, 12787, 12788, 12789,
> 12790, 12793), tzone = "UTC", tclass = "Date", class = "Date"),
>  crepub = c(1.20601312, 1.176601845, 1.14945752, 1.112667506,
>  1.076184043, 1.042147848), finland = c(0.614973309, 0.615008409,
>  0.615034446, 0.615053761, 0.615068089, 0.615078717), france = 
> c(1.896830857,
>  1.849908737, 1.807150091, 1.763296422, 1.719573044, 1.690600819
>  ), germany = c(3.01041925, 2.892518667, 2.780918603, 2.672356826,
>  2.567306135, 2.479892045), italy = c(0.345659867, 0.345675874,
>  0.345686934, 0.345694578, 0.34569986, 0.34570351), netherlands =
> c(0.509263785,
>  0.509279495, 0.509289967, 0.509296947, 0.509301605, 0.509304705
>  ), norway = c(1.052509528, 0.889357215, 0.784607722, 0.710551664,
>  0.661473027, 0.629951323), poland = c(1.127163733, 1.12432629,
>  1.087704091, 1.056705592, 1.024417693, 1.007962456), slovakia =
> c(0.715652234,
>  0.706087191, 0.706077173, 0.706104559, 0.70622666, 0.706098981
>  ), slovenia = c(0.831886154, 0.831945994, 0.832003445, 0.832058602,
>  0.832111556, 0.832162397), uk = c(1.504813191, 1.463648326,
>  1.424235397, 1.38618692, 1.349628127, 1.318653737), usa = c(1.521675109,
>  1.488286869, 1.451778376, 1.418378195, 1.384742397, 1.363477506
>  )), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
> ))
>
> As there are some countries which are having higher volatilities than
> countries with lower, is there any suggestion to improve the graph?
>
> Looking forward to your suggestion in this regard.
>
> With sincere regards,
> Upananda Pani
>
>
> On Tue, May 2, 2023 at 6:14 PM Chris Evans  wrote:
>> I suspect that you have tried to pass your own data into the pivot_longer() 
>> function and your data probably doesn't have the rowN variable.
>>
>> First try running the whole of what I sent you.  It definitively works for 
>> me.  It gives me the attached graph.
>>
>> My code, using tribble() to read in the data you sent us, creates a rowN 
>> variable to save me the trouble of deleting those row numbers one by one.  
>> Then I drop that variable with that select(-rowN) line.
>>
>> Assuming that works for you running my code on that tiny dataset, and that 
>> you are trying to use the code on your full data frame, called data_vol3 
>> then try this:
>>
>> data_vol3 %>%
>># select(-rowN) %>% # I have commented out this line
>>pivot_longer(cols = -index, # as you want to pivot the other 
>> variables/columns
>> names_to = "countries") -> tibDataVol3Long
>>
>> ggplot(data = tibDataVol3Long,
>> aes(x = index, y = value, group = countries, colour = countries)) +
>>geom_line()
>>
>> This is a very good illustration of why you should supply data using 
>> dput(data), dput(head(data)) if you have a large dataset. I know it doesn't 
>> feel a very sympathetic piece of advice, but I think you need to spend some 
>> days working on your understanding of R, ggplot and the tidyverse realm of 
>> R.  You can use ggplot() without using much of the tidyverse but they are 
>> designed to complement each other and the more I understand of the tidyverse 
>> way of doing things, the better I use ggplot().
>>
>> On 02/05/2023 13:44, Upananda Pani wrote:
>>
>> Hi Chris,
>>
>> Thank for your solutions and time. I am getting the following error
>> while trying to execute the code you suggested.
>>
>> Error in select(., -rowN) : unused argument (-rowN)
>>
>> Regards,
>> Upananda
>>
>> On Tue, May 2, 2023 at 3:08 PM Chris Evans via R-help
>>   wrote:
>>
>> It's not clear what you want but ...
>>
>> On 02/05/

Re: [R] Reg: Help regarding ggplot2

2023-05-02 Thread Chris Evans via R-help
It's not clear what you want but ...

On 02/05/2023 10:57, Upananda Pani wrote:
> Dear All,
>
> I have a dataset which contains date and 12 other countries data. I
> have extracted the data as xts object.
>
> I am not able to recall all the series in the Y axis. My data set
> looks like this
>
> index crepub finland france germany italy netherlands norway poland
> 
> 1 2005-01-03 1.21 0.615 1.90 3.01 0.346 0.509 1.05 1.13
> 2 2005-01-04 1.18 0.615 1.85 2.89 0.346 0.509 0.889 1.12
> 3 2005-01-05 1.15 0.615 1.81 2.78 0.346 0.509 0.785 1.09
> 4 2005-01-06 1.11 0.615 1.76 2.67 0.346 0.509 0.711 1.06
> 5 2005-01-07 1.08 0.615 1.72 2.57 0.346 0.509 0.661 1.02
> 6 2005-01-10 1.04 0.615 1.69 2.48 0.346 0.509 0.630 1.01
>
> My code for the same is as follows
>
> ggplot(data=data_vol3, aes(x=index, y=data_vol3$usa)+
> geom_line())

Well you don't need to say that the y data are from data_vol3, your data 
= declaration says that.

I wonder if what you want is:

library(tidyverse)

tribble(
   ~rowN, ~index, ~crepub, ~finland, ~france, ~germany, ~italy, 
~netherlands, ~norway, ~poland,
   1, "2005-01-03", 1.21, 0.615, 1.90, 3.01, 0.346, 0.509, 1.05, 1.13,
   2, "2005-01-04", 1.18, 0.615, 1.85, 2.89, 0.346, 0.509, 0.889, 1.12,
   3, "2005-01-05", 1.15, 0.615, 1.81, 2.78, 0.346, 0.509, 0.785, 1.09,
   4, "2005-01-06", 1.11, 0.615, 1.76, 2.67, 0.346, 0.509, 0.711, 1.06,
   5, "2005-01-07", 1.08, 0.615, 1.72, 2.57, 0.346, 0.509, 0.661, 1.02,
   6, "2005-01-10", 1.04, 0.615, 1.69, 2.48, 0.346, 0.509, 0.630, 1.01) 
-> data_vol3

### please give us data using dput in future: saves us having to do 
something like that to reclaim it!

### pivot that longer to make it easy to get country data as separate lines

data_vol3 %>%  select(-rowN) %>%
   pivot_longer(cols = -index, # as you want to pivot the other 
variables/columns
    names_to = "countries") -> tibDataVol3Long

ggplot(data = tibDataVol3Long,
    aes(x = index, y = value,
  ### now get the grouping and use it for a colour legend
  group = countries, colour = countries)) +
   geom_line()

> Any help in this regard will be highly appreciated
>
> With regards,
> Upananda Pani
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
-- 
Chris Evans (he/him)
Visiting Professor, UDLA, Quito, Ecuador & Honorary Professor, 
University of Roehampton, London, UK.
Work web site: https://www.psyctc.org/psyctc/
CORE site: http://www.coresystemtrust.org.uk/
Personal site: https://www.psyctc.org/pelerinage2016/
Emeetings (Thursdays): 
https://www.psyctc.org/psyctc/booking-meetings-with-me/
(Beware: French time, generally an hour ahead of UK)

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