Re: [R] problems in converting numeric to character

2018-06-07 Thread Adrian Dușa
Does this helps?

> formatC(x, digits = 1, format = "f")
[1] "1.0" "2.0" "2.0" "2.1"


On Thu, Jun 7, 2018 at 10:08 PM 刘瑞阳  wrote:

> Hi,
> I am having trouble converting numeric to characters in the format I
> desire. To be more specific, I have a number of numeric as follows:
>
> x<-c(1.0,2.0,2.00,2.1)
> I want to convert them to characters so that the out put would be
> c(“1.0”,”2.0”,”2.00”,”2.1”).
>
> However, I used as.character(x) and the output is:
> "1"   "2"   "2"   “2.1"
>
> The decimals are removed if the numeric ends with “.0”. Is there a way to
> circumvent this problem?
>
> Thanks very much!
>
> Sincerely,
>
> Ruiyang Liu
> __
> 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] Revolutions blog: May 2018 roundup

2018-06-07 Thread David Smith (CDA) via R-help
Since 2008, Microsoft staff and guests have written about R at the Revolutions
blog (http://blog.revolutionanalytics.com) and every month I post a summary of
articles from the previous month of particular interest to readers of r-help.

In case you missed them, here are some articles related to R from the
month of May:

The R Consortium has announced a new round of grants for projects proposed by
the R community:
http://blog.revolutionanalytics.com/2018/05/r-consortium-spring-2018.html

A look back at the ROpenSci unconference held in Seattle:
http://blog.revolutionanalytics.com/2018/05/reflections-on-the-ropensci-unconference.html

Video of my European R Users Meeting talk, "Speeding up R with Parallel
Programming in the Cloud":
http://blog.revolutionanalytics.com/2018/05/video-speeding-up-r-foreach.html

Slides from my talk at the Microsoft Build conference, "Open-Source Machine
Learning in Azure":
http://blog.revolutionanalytics.com/2018/05/open-source-machine-learning-in-azure.html

Discussions on Twitter: R packages by stage of data analysis; thinking
differently about AI development; and, why is package management harder in 
Python
than R? http://blog.revolutionanalytics.com/2018/05/three-twitter-threads.html

Microsoft R Open 3.4.4 is now available:
http://blog.revolutionanalytics.com/2018/04/microsoft-r-open-344-now-available.html

And some general interest stories (not necessarily related to R):

* A really bad road in Nepal:
  http://blog.revolutionanalytics.com/2018/05/because-its-friday-bad-road.html

* Our May 2018 roundup of AI and data science news:
  http://blog.revolutionanalytics.com/2018/05/ai-roundup-may-2018.html 

* The definitive answer to "Laurel" or "Yanny" (it's Laurel):
  
http://blog.revolutionanalytics.com/2018/05/because-its-friday-laurel-or-yanny.html

* Panelist Francesca Lazzeri reviews the Mind Bytes AI conference in Chicago:
  http://blog.revolutionanalytics.com/2018/05/mind-bytes.html

* A parody of air travel:
  
http://blog.revolutionanalytics.com/2018/05/because-its-friday-planes-pains-and-automobiles.html

* When magpies attack:
  
http://blog.revolutionanalytics.com/2018/05/because-its-friday-the-eyes-dont-work.html

As always, thanks for the comments and please keep sending suggestions to
me at david...@microsoft.com or via Twitter (I'm @revodavid).

Cheers,
# David

-- 
David M Smith 
Developer Advocate, Microsoft Cloud & Enterprise 
Tel: +1 (312) 9205766 (Chicago IL, USA)
Twitter: @revodavid | Blog:  http://blog.revolutionanalytics.com

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


Re: [R] aggregate and list elements of variables in data.frame

2018-06-07 Thread Bert Gunter
which() is unnecessary. Use logical subscripting:

... t$id[t$A ==x]

Further simplification can be gotten by using the with() function:

l <- with(t, sapply(unique(A), function(x) id[A ==x]))

Check this though -- there might be scoping issues.

Cheers,
Bert



On Thu, Jun 7, 2018, 6:49 AM Massimo Bressan 
wrote:

> #ok, finally this is my final "best and more compact" solution of the
> problem by merging different contributions (thanks to all indeed)
>
> t<-data.frame(id=c(18,91,20,68,54,27,26,15,4,97),A=c(123,345,123,678,345,123,789,345,123,789))
>
> l<-sapply(unique(t$A), function(x) t$id[which(t$A==x)])
> r<-data.frame(unique_A= unique(t$A), list_id=unlist(lapply(l, paste,
> collapse = ", ")))
> r
>
>
> [[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] problems in converting numeric to character

2018-06-07 Thread Jeff Newmiller
?formatC (digits, drop0trailing)
?sprintf (format %f)
?cat 
?options (digits)

You appear to be confusing source code formatting with output formatting. The 
internal representation of a numeric value has no notion of the number of 
decimals that were used to enter it into memory from source code. By the time 
you (or R) decide to re-convert it to a visual representation (characters), all 
trace of the original representation has been forgotten, so you have to be 
explicit about your output format, or R will make assumptions.

On June 7, 2018 8:12:27 AM PDT, "刘瑞阳"  wrote:
>Hi,
>I am having trouble converting numeric to characters in the format I
>desire. To be more specific, I have a number of numeric as follows:
>
>x<-c(1.0,2.0,2.00,2.1)
>I want to convert them to characters so that the out put would be
>c(“1.0”,”2.0”,”2.00”,”2.1”). 
>
>However, I used as.character(x) and the output is:
>"1"   "2"   "2"   “2.1"
>
>The decimals are removed if the numeric ends with “.0”. Is there a way
>to circumvent this problem?
>
>Thanks very much!
>
>Sincerely,
>
>Ruiyang Liu
>__
>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

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

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


[R] problems in converting numeric to character

2018-06-07 Thread 刘瑞阳
Hi,
I am having trouble converting numeric to characters in the format I desire. To 
be more specific, I have a number of numeric as follows:

x<-c(1.0,2.0,2.00,2.1)
I want to convert them to characters so that the out put would be 
c(“1.0”,”2.0”,”2.00”,”2.1”). 

However, I used as.character(x) and the output is:
"1"   "2"   "2"   “2.1"

The decimals are removed if the numeric ends with “.0”. Is there a way to 
circumvent this problem?

Thanks very much!

Sincerely,

Ruiyang Liu
__
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] verInd= and HorInd= arguments to pairs() function

2018-06-07 Thread Martin Maechler
> Gerrit Eichner 
> on Thu, 7 Jun 2018 09:03:46 +0200 writes:

> Hi, Chris, had the same problem (and first thought it was
> my fault), but there seems to be a typo in the code of
> pairs.default. Below is a workaround.  Look for two
> comments (starting with #) in the code to see what I
> have changed to make it work at least the way I'd expect
> it in one of your examples.

>   Hth -- Gerrit

> mypairs <- function (x, labels, panel = points, ...,
>  horInd = 1:nc, verInd = 1:nc,
>  lower.panel = panel, upper.panel = panel, diag.panel = NULL,
>  text.panel = textPanel, label.pos = 0.5 + has.diag/3, line.main = 3,
>  cex.labels = NULL, font.labels = 1, row1attop = TRUE, gap = 1,
>  log = "") {
>  if (doText <- missing(text.panel) || is.function(text.panel))
>  textPanel <- function(x = 0.5, y = 0.5, txt, cex, font) text(x,
>  y, txt, cex = cex, font = font)
>  localAxis <- function(side, x, y, xpd, bg, col = NULL, main,
>  oma, ...) {
>  xpd <- NA
>  if (side%%2L == 1L && xl[j])
>  xpd <- FALSE
>  if (side%%2L == 0L && yl[i])
>  xpd <- FALSE
>  if (side%%2L == 1L)
>  Axis(x, side = side, xpd = xpd, ...)
>  else Axis(y, side = side, xpd = xpd, ...)
>  }
>  localPlot <- function(..., main, oma, font.main, cex.main) plot(...)
>  localLowerPanel <- function(..., main, oma, font.main, cex.main) 
> lower.panel(...)
>  localUpperPanel <- function(..., main, oma, font.main, cex.main) 
> upper.panel(...)
>  localDiagPanel <- function(..., main, oma, font.main, cex.main) 
> diag.panel(...)
>  dots <- list(...)
>  nmdots <- names(dots)
>  if (!is.matrix(x)) {
>  x <- as.data.frame(x)
>  for (i in seq_along(names(x))) {
>  if (is.factor(x[[i]]) || is.logical(x[[i]]))
>  x[[i]] <- as.numeric(x[[i]])
>  if (!is.numeric(unclass(x[[i]])))
>  stop("non-numeric argument to 'pairs'")
>  }
>  }
>  else if (!is.numeric(x))
>  stop("non-numeric argument to 'pairs'")
>  panel <- match.fun(panel)
>  if ((has.lower <- !is.null(lower.panel)) && !missing(lower.panel))
>  lower.panel <- match.fun(lower.panel)
>  if ((has.upper <- !is.null(upper.panel)) && !missing(upper.panel))
>  upper.panel <- match.fun(upper.panel)
>  if ((has.diag <- !is.null(diag.panel)) && !missing(diag.panel))
>  diag.panel <- match.fun(diag.panel)
>  if (row1attop) {
>  tmp <- lower.panel
>  lower.panel <- upper.panel
>  upper.panel <- tmp
>  tmp <- has.lower
>  has.lower <- has.upper
>  has.upper <- tmp
>  }
>  nc <- ncol(x)
>  if (nc < 2L)
>  stop("only one column in the argument to 'pairs'")
>  if (!all(horInd >= 1L && horInd <= nc))
>  stop("invalid argument 'horInd'")
>  if (!all(verInd >= 1L && verInd <= nc))
>  stop("invalid argument 'verInd'")
>  if (doText) {
>  if (missing(labels)) {
>  labels <- colnames(x)
>  if (is.null(labels))
>  labels <- paste("var", 1L:nc)
>  }
>  else if (is.null(labels))
>  doText <- FALSE
>  }
>  oma <- if ("oma" %in% nmdots)
>  dots$oma
>  main <- if ("main" %in% nmdots)
>  dots$main
>  if (is.null(oma))
>  oma <- c(4, 4, if (!is.null(main)) 6 else 4, 4)
>  opar <- par(mfcol = c(length(horInd), length(verInd)),
> # Changed from mfrow to mfcol
>  mar = rep.int(gap/2, 4), oma = oma)
>  on.exit(par(opar))
>  dev.hold()
>  on.exit(dev.flush(), add = TRUE)
>  xl <- yl <- logical(nc)
>  if (is.numeric(log))
>  xl[log] <- yl[log] <- TRUE
>  else {
>  xl[] <- grepl("x", log)
>  yl[] <- grepl("y", log)
>  }
>  for (j in if (row1attop) verInd else rev(verInd))
>   for (i in horInd) {
> # Exchanged i and j. (i used to be in
> # the outer and j in the inner loop!)
>  l <- paste0(ifelse(xl[j], "x", ""), ifelse(yl[i], "y", ""))
>  localPlot(x[, j], x[, i], xlab = "", ylab = "", axes = FALSE,
>  type = "n", ..., log = l)
>  if (i == j || (i < j && has.lower) || (i > j && has.upper)) {
>  box()
>  if (i == 1 && (!(j%%2L) || !has.upper || !has.lower))
>  localAxis(1L + 2L * row1attop, x[, j], x[, i],
>...)
>  if (i == nc && (j%%2L || !has.upper || !has.lower))
>  localAxis(3L - 2L * row1attop, x[, j], x[, i],
>...)
>  if (j == 1 && (!(i%%2L) || !has.upper || !has.lower))
>  localAxis(2L, x[, j], x[, i], ...)
>  if (j == nc && (i%%2L || !has.upper || !has.lower))
>  localAxis(4L, x[, j], x[, i], ...)
> 

Re: [R] open help files from Terminal

2018-06-07 Thread Sarah Goslee
Here are two options:

> # for one occurrence
> help("help", help_type="html")
starting httpd help server ... done
>
> # to set the default for your R session
> options(help_type = "html")
> ?help

If you read the help file for help(), you will see all the possibilities.

Sarah

On Thu, Jun 7, 2018 at 11:21 AM, Dmitri Popavenko
 wrote:
> Dear R users,
>
> I am sometimes using R from a Terminal (either from Linux but most often on
> MacOS).
> When looking at a help file using the question mark (e.g. ?sd) the help
> file opens in the Terminal itself.
>
> If possible, I would like to open the HTML version of the help file in a
> webpage, but I am completely unaware of how this might be done.
>
> If anyone has a suggestion, I would be very grateful.
>
> Best,
> Dmitri

-- 
Sarah Goslee
http://www.functionaldiversity.org

__
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] open help files from Terminal

2018-06-07 Thread Dmitri Popavenko
Dear Sarah, dear David,

Thank you very much indeed, this is exactly what I needed.

Best,
Dmitri

On Thu, Jun 7, 2018 at 6:39 PM, Sarah Goslee  wrote:

> Here are two options:
>
> > # for one occurrence
> > help("help", help_type="html")
> starting httpd help server ... done
> >
> > # to set the default for your R session
> > options(help_type = "html")
> > ?help
>
> If you read the help file for help(), you will see all the possibilities.
>
> Sarah
>
> On Thu, Jun 7, 2018 at 11:21 AM, Dmitri Popavenko
>  wrote:
> > Dear R users,
> >
> > I am sometimes using R from a Terminal (either from Linux but most often
> on
> > MacOS).
> > When looking at a help file using the question mark (e.g. ?sd) the help
> > file opens in the Terminal itself.
> >
> > If possible, I would like to open the HTML version of the help file in a
> > webpage, but I am completely unaware of how this might be done.
> >
> > If anyone has a suggestion, I would be very grateful.
> >
> > Best,
> > Dmitri
>
> --
> Sarah Goslee
> http://www.functionaldiversity.org
>

[[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] open help files from Terminal

2018-06-07 Thread David Winsemius


> On Jun 7, 2018, at 8:21 AM, Dmitri Popavenko  
> wrote:
> 
> Dear R users,
> 
> I am sometimes using R from a Terminal (either from Linux but most often on
> MacOS).
> When looking at a help file using the question mark (e.g. ?sd) the help
> file opens in the Terminal itself.
> 
> If possible, I would like to open the HTML version of the help file in a
> webpage, but I am completely unaware of how this might be done.

First I looked at:

?help

Seeing the help-type parameter included "html" as a potential value, I 
therefore tried this from a Terminal-launched session on a Mac (although 
normally I work in the R.app GUI):

help(sd, help_type="html")

I get the expected sd-help page displayed in my open Chrome browser.


> 
> If anyone has a suggestion, I would be very grateful.
> 
> Best,
> Dmitri
> 
>   [[alternative HTML version deleted]]

R is a plain text mailing list.
> 
> __
> 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.

David Winsemius
Alameda, CA, USA

'Any technology distinguishable from magic is insufficiently advanced.'   
-Gehm's Corollary to Clarke's Third Law

__
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] stat_function with data frames in ggplot2

2018-06-07 Thread David Winsemius


> On Jun 7, 2018, at 7:18 AM, Veerappa Chetty  wrote:
> 
> I use solve(A,b) inside my function, myfun2; it works fine when I return
> one value or a list.
> I want use the return values in ggplot as below: ggplot(data.frame(
> x=c(0.1,0.8)),aes(x=x))+stat_function(fun=myfun.2,geom="line")
> I get a blank graph. Would greatly appreciate help! Thanks.
> 
> Here are my codes:
> p.lm<-0.05 ##to initialze only
> p.lh<-0.1
> p.ll<-1-p.lm-p.lh
> p.ml<-0.3
> p.mh<-0.1
> p.mm<-1-p.ml-p.mh
> p.hl<-0.05
> p.hm<-0.5
> p.hh<-1-p.hl-p.hm
> myfun.5<-function(xvar){
> y<-numeric(2)
> p.lm<-xvar
> A<-matrix(c(p.lm+p.lh+p.hl,p.hl-p.ml,p.hm-p.lm,p.ml+p.mh+p.hm),nrow=2,byrow
> = TRUE)
> b<-c(p.hl,p.hm)
> y<-solve(A,b)
> z<-list(y[1],y[2],1-y[1]-y[2])
> z[1]
> }
> 
> g.2<-ggplot(data.frame(
> x=c(0.1,0.8)),aes(x=x))+stat_function(fun=myfun.5,geom="line")
> g.2

You are a) failing to pay attention to the warning message:

> g.2
Warning message:
Computation failed in `stat_function()`:
'a' (2 x 102) must be square 

> 

 and b) as a consequence failing to debug your function. Add a print(A) 
line immediately after your construction of A:

 myfun.5(1:10)
  [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10] [,11]
[1,]  1.15  2.15  3.15  4.15  5.15  6.15  7.15  8.15  9.15 10.15 -0.25
[2,] -0.50 -1.50 -2.50 -3.50 -4.50 -5.50 -6.50 -7.50 -8.50 -9.50  0.90
Error in solve.default(A, b) : 'a' (2 x 11) must be square

The fun argument in stat_fun is supposed to accept a vector with x values and 
return y values for "predictions". You have not indicated what plot was 
expected, You've given no indication what the various constants are supposed to 
represent, and you are only giving x-values to aes.ggplot, so I'm unable to 
infer what is intended.



> -- 
> Professor of Family Medicine
> Boston University
> Tel: 617-414-6221, Fax:617-414-3345
> emails: chett...@gmail.com,vche...@bu.edu
> 
>   [[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.

David Winsemius
Alameda, CA, USA

'Any technology distinguishable from magic is insufficiently advanced.'   
-Gehm's Corollary to Clarke's Third Law

__
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] open help files from Terminal

2018-06-07 Thread Dmitri Popavenko
Dear R users,

I am sometimes using R from a Terminal (either from Linux but most often on
MacOS).
When looking at a help file using the question mark (e.g. ?sd) the help
file opens in the Terminal itself.

If possible, I would like to open the HTML version of the help file in a
webpage, but I am completely unaware of how this might be done.

If anyone has a suggestion, I would be very grateful.

Best,
Dmitri

[[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] stat_function with data frames in ggplot2

2018-06-07 Thread Veerappa Chetty
I use solve(A,b) inside my function, myfun2; it works fine when I return
one value or a list.
I want use the return values in ggplot as below: ggplot(data.frame(
x=c(0.1,0.8)),aes(x=x))+stat_function(fun=myfun.2,geom="line")
I get a blank graph. Would greatly appreciate help! Thanks.

Here are my codes:
p.lm<-0.05 ##to initialze only
p.lh<-0.1
p.ll<-1-p.lm-p.lh
p.ml<-0.3
p.mh<-0.1
p.mm<-1-p.ml-p.mh
p.hl<-0.05
p.hm<-0.5
p.hh<-1-p.hl-p.hm
myfun.5<-function(xvar){
y<-numeric(2)
p.lm<-xvar
A<-matrix(c(p.lm+p.lh+p.hl,p.hl-p.ml,p.hm-p.lm,p.ml+p.mh+p.hm),nrow=2,byrow
= TRUE)
b<-c(p.hl,p.hm)
y<-solve(A,b)
z<-list(y[1],y[2],1-y[1]-y[2])
z[1]
}

g.2<-ggplot(data.frame(
x=c(0.1,0.8)),aes(x=x))+stat_function(fun=myfun.5,geom="line")
g.2

-- 
Professor of Family Medicine
Boston University
Tel: 617-414-6221, Fax:617-414-3345
emails: chett...@gmail.com,vche...@bu.edu

[[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] aggregate and list elements of variables in data.frame

2018-06-07 Thread Massimo Bressan
#ok, finally this is my final "best and more compact" solution of the problem 
by merging different contributions (thanks to all indeed) 

t<-data.frame(id=c(18,91,20,68,54,27,26,15,4,97),A=c(123,345,123,678,345,123,789,345,123,789))
 
l<-sapply(unique(t$A), function(x) t$id[which(t$A==x)]) 
r<-data.frame(unique_A= unique(t$A), list_id=unlist(lapply(l, paste, collapse = 
", "))) 
r 


[[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] aggregate and list elements of variables in data.frame

2018-06-07 Thread Massimo Bressan
thank you for the help 

this is my solution based on your valuable hint but without the need to pass 
through the use of a 'tibble' 

x<-data.frame(id=LETTERS[1:10], A=c(123,345,123,678,345,123,789,345,123,789)) 
uA<-unique(x$A) 
idx<-lapply(uA, function(v) which(x$A %in% v)) 
vals<- lapply(idx, function(index) x$id[index]) 
data.frame(unique_A = uA, list_vals=unlist(lapply(vals, paste, collapse = ", 
"))) 

best 



Da: "Ben Tupper"  
A: "Massimo Bressan"  
Cc: "r-help"  
Inviato: Giovedì, 7 giugno 2018 14:47:55 
Oggetto: Re: [R] aggregate and list elements of variables in data.frame 

Hi, 

Does this do what you want? I had to change the id values to something more 
obvious. It uses tibbles which allow each variable to be a list. 

library(tibble) 
library(dplyr) 
x <- tibble(id=LETTERS[1:10], 
A=c(123,345,123,678,345,123,789,345,123,789)) 
uA <- unique(x$A) 
idx <- lapply(uA, function(v) which(x$A %in% v)) 
vals <- lapply(idx, function(index) x$id[index]) 

r <- tibble(unique_A = uA, list_idx = idx, list_vals = vals) 


> r 
# A tibble: 4 x 3 
unique_A list_idx list_vals 
   
1 123.   
2 345.   
3 678.   
4 789.   
> r$list_idx[1] 
[[1]] 
[1] 1 3 6 9 

> r$list_vals[1] 
[[1]] 
[1] "A" "C" "F" "I" 


Cheers, 
ben 


[[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] aggregate and list elements of variables in data.frame

2018-06-07 Thread Ben Tupper
Hi,

Does this do what you want?  I had to change the id values to something more 
obvious.  It uses tibbles which allow each variable to be a list.

library(tibble)
library(dplyr)
x   <- tibble(id=LETTERS[1:10],
A=c(123,345,123,678,345,123,789,345,123,789))
uA  <- unique(x$A)
idx <- lapply(uA, function(v) which(x$A %in% v))
vals<- lapply(idx, function(index) x$id[index])

r <- tibble(unique_A = uA, list_idx = idx, list_vals = vals)


> r
# A tibble: 4 x 3
  unique_A list_idx  list_vals
 
1 123.  
2 345.  
3 678.  
4 789.  
> r$list_idx[1]
[[1]]
[1] 1 3 6 9

> r$list_vals[1]
[[1]]
[1] "A" "C" "F" "I"


Cheers,
ben



> On Jun 7, 2018, at 8:21 AM, Massimo Bressan  
> wrote:
> 
> sorry, but by further looking at the example I just realised that the posted 
> solution it's not completely what I need because in fact I do not need to get 
> back the 'indices' but instead the corrisponding values of column A 
> 
> #please consider this new example 
> 
> t<-data.frame(id=c(18,91,20,68,54,27,26,15,4,97),A=c(123,345,123,678,345,123,789,345,123,789))
>  
> t 
> 
> # I need to get this result 
> r<-data.frame(unique_A=c(123, 345, 678, 
> 789),list_id=c('18,20,27,4','91,54,15','68','26,97')) 
> r 
> 
> # any help for this, please? 
> 
> 
> 
> 
> 
> Da: "Massimo Bressan"  
> A: "r-help"  
> Inviato: Giovedì, 7 giugno 2018 10:09:55 
> Oggetto: Re: aggregate and list elements of variables in data.frame 
> 
> thanks for the help 
> 
> I'm posting here the complete solution 
> 
> t<-data.frame(id=1:10,A=c(123,345,123,678,345,123,789,345,123,789)) 
> t$A <- factor(t$A) 
> l<-sapply(levels(t$A), function(x) which(t$A==x)) 
> r<-data.frame(list_id=unlist(lapply(l, paste, collapse = ", "))) 
> r<-cbind(unique_A=row.names(r),r) 
> row.names(r)<-NULL 
> r 
> 
> best 
> 
> 
> 
> Da: "Massimo Bressan"  
> A: "r-help"  
> Inviato: Mercoledì, 6 giugno 2018 10:13:10 
> Oggetto: aggregate and list elements of variables in data.frame 
> 
> #given the following reproducible and simplified example 
> 
> t<-data.frame(id=1:10,A=c(123,345,123,678,345,123,789,345,123,789)) 
> t 
> 
> #I need to get the following result 
> 
> r<-data.frame(unique_A=c(123, 345, 678, 
> 789),list_id=c('1,3,6,9','2,5,8','4','7,10')) 
> r 
> 
> # i.e. aggregate over the variable "A" and list all elements of the variable 
> "id" satisfying the criteria of having the same corrisponding value of "A" 
> #any help for that? 
> 
> #so far I've just managed to "aggregate" and "count", like: 
> 
> library(sqldf) 
> sqldf('select count(*) as count_id, A as unique_A from t group by A') 
> 
> library(dplyr) 
> t%>%group_by(unique_A=A) %>% summarise(count_id = n()) 
> 
> # thank you 
> 
> 
> -- 
> 
>  
> Massimo Bressan 
> 
> ARPAV 
> Agenzia Regionale per la Prevenzione e 
> Protezione Ambientale del Veneto 
> 
> Dipartimento Provinciale di Treviso 
> Via Santa Barbara, 5/a 
> 31100 Treviso, Italy 
> 
> tel: +39 0422 558545 
> fax: +39 0422 558516 
> e-mail: massimo.bres...@arpa.veneto.it 
>  
> 
> 
> -- 
> 
>  
> Massimo Bressan 
> 
> ARPAV 
> Agenzia Regionale per la Prevenzione e 
> Protezione Ambientale del Veneto 
> 
> Dipartimento Provinciale di Treviso 
> Via Santa Barbara, 5/a 
> 31100 Treviso, Italy 
> 
> tel: +39 0422 558545 
> fax: +39 0422 558516 
> e-mail: massimo.bres...@arpa.veneto.it 
>  
> 
>   [[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.
> 

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org

Ecological Forecasting: https://eco.bigelow.org/






[[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] aggregate and list elements of variables in data.frame

2018-06-07 Thread Ivan Calandra

Using which() to subset t$id should do the trick:

sapply(levels(t$A), function(x) t$id[which(t$A==x)])

Ivan

--
Dr. Ivan Calandra
TraCEr, laboratory for Traceology and Controlled Experiments
MONREPOS Archaeological Research Centre and
Museum for Human Behavioural Evolution
Schloss Monrepos
56567 Neuwied, Germany
+49 (0) 2631 9772-243
https://www.researchgate.net/profile/Ivan_Calandra

On 07/06/2018 14:21, Massimo Bressan wrote:

sorry, but by further looking at the example I just realised that the posted 
solution it's not completely what I need because in fact I do not need to get 
back the 'indices' but instead the corrisponding values of column A

#please consider this new example

t<-data.frame(id=c(18,91,20,68,54,27,26,15,4,97),A=c(123,345,123,678,345,123,789,345,123,789))
t

# I need to get this result
r<-data.frame(unique_A=c(123, 345, 678, 
789),list_id=c('18,20,27,4','91,54,15','68','26,97'))
r

# any help for this, please?





Da: "Massimo Bressan" 
A: "r-help" 
Inviato: Giovedì, 7 giugno 2018 10:09:55
Oggetto: Re: aggregate and list elements of variables in data.frame

thanks for the help

I'm posting here the complete solution

t<-data.frame(id=1:10,A=c(123,345,123,678,345,123,789,345,123,789))
t$A <- factor(t$A)
l<-sapply(levels(t$A), function(x) which(t$A==x))
r<-data.frame(list_id=unlist(lapply(l, paste, collapse = ", ")))
r<-cbind(unique_A=row.names(r),r)
row.names(r)<-NULL
r

best



Da: "Massimo Bressan" 
A: "r-help" 
Inviato: Mercoledì, 6 giugno 2018 10:13:10
Oggetto: aggregate and list elements of variables in data.frame

#given the following reproducible and simplified example

t<-data.frame(id=1:10,A=c(123,345,123,678,345,123,789,345,123,789))
t

#I need to get the following result

r<-data.frame(unique_A=c(123, 345, 678, 
789),list_id=c('1,3,6,9','2,5,8','4','7,10'))
r

# i.e. aggregate over the variable "A" and list all elements of the variable "id" 
satisfying the criteria of having the same corrisponding value of "A"
#any help for that?

#so far I've just managed to "aggregate" and "count", like:

library(sqldf)
sqldf('select count(*) as count_id, A as unique_A from t group by A')

library(dplyr)
t%>%group_by(unique_A=A) %>% summarise(count_id = n())

# thank you




__
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] aggregate and list elements of variables in data.frame

2018-06-07 Thread Massimo Bressan
sorry, but by further looking at the example I just realised that the posted 
solution it's not completely what I need because in fact I do not need to get 
back the 'indices' but instead the corrisponding values of column A 

#please consider this new example 

t<-data.frame(id=c(18,91,20,68,54,27,26,15,4,97),A=c(123,345,123,678,345,123,789,345,123,789))
 
t 

# I need to get this result 
r<-data.frame(unique_A=c(123, 345, 678, 
789),list_id=c('18,20,27,4','91,54,15','68','26,97')) 
r 

# any help for this, please? 





Da: "Massimo Bressan"  
A: "r-help"  
Inviato: Giovedì, 7 giugno 2018 10:09:55 
Oggetto: Re: aggregate and list elements of variables in data.frame 

thanks for the help 

I'm posting here the complete solution 

t<-data.frame(id=1:10,A=c(123,345,123,678,345,123,789,345,123,789)) 
t$A <- factor(t$A) 
l<-sapply(levels(t$A), function(x) which(t$A==x)) 
r<-data.frame(list_id=unlist(lapply(l, paste, collapse = ", "))) 
r<-cbind(unique_A=row.names(r),r) 
row.names(r)<-NULL 
r 

best 



Da: "Massimo Bressan"  
A: "r-help"  
Inviato: Mercoledì, 6 giugno 2018 10:13:10 
Oggetto: aggregate and list elements of variables in data.frame 

#given the following reproducible and simplified example 

t<-data.frame(id=1:10,A=c(123,345,123,678,345,123,789,345,123,789)) 
t 

#I need to get the following result 

r<-data.frame(unique_A=c(123, 345, 678, 
789),list_id=c('1,3,6,9','2,5,8','4','7,10')) 
r 

# i.e. aggregate over the variable "A" and list all elements of the variable 
"id" satisfying the criteria of having the same corrisponding value of "A" 
#any help for that? 

#so far I've just managed to "aggregate" and "count", like: 

library(sqldf) 
sqldf('select count(*) as count_id, A as unique_A from t group by A') 

library(dplyr) 
t%>%group_by(unique_A=A) %>% summarise(count_id = n()) 

# thank you 


-- 

 
Massimo Bressan 

ARPAV 
Agenzia Regionale per la Prevenzione e 
Protezione Ambientale del Veneto 

Dipartimento Provinciale di Treviso 
Via Santa Barbara, 5/a 
31100 Treviso, Italy 

tel: +39 0422 558545 
fax: +39 0422 558516 
e-mail: massimo.bres...@arpa.veneto.it 
 


-- 

 
Massimo Bressan 

ARPAV 
Agenzia Regionale per la Prevenzione e 
Protezione Ambientale del Veneto 

Dipartimento Provinciale di Treviso 
Via Santa Barbara, 5/a 
31100 Treviso, Italy 

tel: +39 0422 558545 
fax: +39 0422 558516 
e-mail: massimo.bres...@arpa.veneto.it 
 

[[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] Histogram of character elements

2018-06-07 Thread Luigi Marongiu
Thank you Ben, this also works! I have a copy of the Sarkar but,
usually, I don't work with histograms. I'll brush it up, then. Best
regards, Luigi

On Thu, Jun 7, 2018 at 1:43 PM Ben Tupper  wrote:
>
> Hi again,
>
> I'm sort of pre-coffee still, but does this do it?  The data frame only has 
> one variable, a factor where the order of the levels is specified.
>
> library(lattice)
> group   <- c("a", "b", "c", "d", "e")
> freq<- c(1, 2, 2, 5, 3)
> x   <- rep(group, freq)
> df  <- data.frame(group = factor(x, levels = c("d", "a", "b", "c", "e")) )
> histogram(~ group, data = df)
>
> As far as super-grouping the answer is likely yes, but without details and an 
> example (and coffee) I'm at a loss.   I suggest getting a your hands on a 
> copy of https://www.r-project.org/doc/bib/R-books_bib.html#R:Sarkar:2008 It's 
> really worth it if you plan to spend time with lattice.
>
> Cheers,
> Ben
>
>
> On Jun 7, 2018, at 7:02 AM, Luigi Marongiu  wrote:
>
> also, with this approach, I need to re-arrange the data. Is it
> possible to work directly on a dataframe?
> On Thu, Jun 7, 2018 at 12:48 PM Ben Tupper  wrote:
>
>
> Hi,
>
> Is this what you are after?
>
> group <- c("a", "b", "c", "d", "e")
> freq <-c(1, 2, 2, 5, 3)
> x = rep(group, freq)
> barplot(table(x))
>
> Cheers,
> Ben
>
>
>
> On Jun 7, 2018, at 6:00 AM, Luigi Marongiu  wrote:
>
> Dear all,
> I have a dataframe with a column representing the names of the
> elements (a, b, etc) and one with their frequencies.
> How can I plot the frequencies so that each element has an associated
> frequency value?
> I have been thinking of a histogram, but I have found it difficult to
> implement. I have tried the following:
>
> group <- c("a", "b", "c", "d", "e")
> freq <-c(1, 2, 2, 5, 3)
> df <- data.frame(group, freq, stringsAsFactors = FALSE)
> hist(df$freq)
> library(lattice)
> histogram( ~ df$group)
> histogram( ~ as.factor(df$group))
> histogram(df$freq ~ as.factor(df$group))
>
> hist(df$freq) returns a histogram in which the values 1 and 2 appear 3
> times, the values 3 and 5 appear once and 4 never. This is not what I
> wanted; I want instead a graph telling me that a appears once, b twice
> etc.
>
> histogram( ~ df$group) gives the error:
> Error in hist.default(as.numeric(x), breaks = breaks, plot = FALSE,
> include.lowest = include.lowest,  :
> negative length vectors are not allowed
>
> histogram( ~ as.factor(df$group)) and histogram(df$freq ~
> as.factor(df$group)) report all groups on the x axis (that is good)
> but all at 20% level.
>
> What am I missing?
> Thank you.
>
> --
> Best regards,
> Luigi
>
> __
> 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.
>
>
> Ben Tupper
> Bigelow Laboratory for Ocean Sciences
> 60 Bigelow Drive, P.O. Box 380
> East Boothbay, Maine 04544
> http://www.bigelow.org
>
> Ecological Forecasting: https://eco.bigelow.org/
>
>
>
>
>
>
>
> --
> Best regards,
> Luigi
>
>
> Ben Tupper
> Bigelow Laboratory for Ocean Sciences
> 60 Bigelow Drive, P.O. Box 380
> East Boothbay, Maine 04544
> http://www.bigelow.org
>
> Ecological Forecasting: https://eco.bigelow.org/
>
>
>
>
>


-- 
Best regards,
Luigi

__
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] Histogram of character elements

2018-06-07 Thread Ben Tupper
Hi again,

I'm sort of pre-coffee still, but does this do it?  The data frame only has one 
variable, a factor where the order of the levels is specified.

library(lattice)
group   <- c("a", "b", "c", "d", "e")
freq<- c(1, 2, 2, 5, 3)
x   <- rep(group, freq)
df  <- data.frame(group = factor(x, levels = c("d", "a", "b", "c", "e")) )
histogram(~ group, data = df)

As far as super-grouping the answer is likely yes, but without details and an 
example (and coffee) I'm at a loss.   I suggest getting a your hands on a copy 
of https://www.r-project.org/doc/bib/R-books_bib.html#R:Sarkar:2008 
 It's really 
worth it if you plan to spend time with lattice.

Cheers,
Ben 


> On Jun 7, 2018, at 7:02 AM, Luigi Marongiu  wrote:
> 
> also, with this approach, I need to re-arrange the data. Is it
> possible to work directly on a dataframe?
> On Thu, Jun 7, 2018 at 12:48 PM Ben Tupper  wrote:
>> 
>> Hi,
>> 
>> Is this what you are after?
>> 
>> group <- c("a", "b", "c", "d", "e")
>> freq <-c(1, 2, 2, 5, 3)
>> x = rep(group, freq)
>> barplot(table(x))
>> 
>> Cheers,
>> Ben
>> 
>> 
>> 
>> On Jun 7, 2018, at 6:00 AM, Luigi Marongiu  wrote:
>> 
>> Dear all,
>> I have a dataframe with a column representing the names of the
>> elements (a, b, etc) and one with their frequencies.
>> How can I plot the frequencies so that each element has an associated
>> frequency value?
>> I have been thinking of a histogram, but I have found it difficult to
>> implement. I have tried the following:
>> 
>> group <- c("a", "b", "c", "d", "e")
>> freq <-c(1, 2, 2, 5, 3)
>> df <- data.frame(group, freq, stringsAsFactors = FALSE)
>> hist(df$freq)
>> library(lattice)
>> histogram( ~ df$group)
>> histogram( ~ as.factor(df$group))
>> histogram(df$freq ~ as.factor(df$group))
>> 
>> hist(df$freq) returns a histogram in which the values 1 and 2 appear 3
>> times, the values 3 and 5 appear once and 4 never. This is not what I
>> wanted; I want instead a graph telling me that a appears once, b twice
>> etc.
>> 
>> histogram( ~ df$group) gives the error:
>> Error in hist.default(as.numeric(x), breaks = breaks, plot = FALSE,
>> include.lowest = include.lowest,  :
>> negative length vectors are not allowed
>> 
>> histogram( ~ as.factor(df$group)) and histogram(df$freq ~
>> as.factor(df$group)) report all groups on the x axis (that is good)
>> but all at 20% level.
>> 
>> What am I missing?
>> Thank you.
>> 
>> --
>> Best regards,
>> Luigi
>> 
>> __
>> 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.
>> 
>> 
>> Ben Tupper
>> Bigelow Laboratory for Ocean Sciences
>> 60 Bigelow Drive, P.O. Box 380
>> East Boothbay, Maine 04544
>> http://www.bigelow.org
>> 
>> Ecological Forecasting: https://eco.bigelow.org/
>> 
>> 
>> 
>> 
>> 
> 
> 
> -- 
> Best regards,
> Luigi
> 

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org

Ecological Forecasting: https://eco.bigelow.org/






[[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] Histogram of character elements

2018-06-07 Thread Luigi Marongiu
also, with this approach, I need to re-arrange the data. Is it
possible to work directly on a dataframe?
On Thu, Jun 7, 2018 at 12:48 PM Ben Tupper  wrote:
>
> Hi,
>
> Is this what you are after?
>
> group <- c("a", "b", "c", "d", "e")
> freq <-c(1, 2, 2, 5, 3)
> x = rep(group, freq)
> barplot(table(x))
>
> Cheers,
> Ben
>
>
>
> On Jun 7, 2018, at 6:00 AM, Luigi Marongiu  wrote:
>
> Dear all,
> I have a dataframe with a column representing the names of the
> elements (a, b, etc) and one with their frequencies.
> How can I plot the frequencies so that each element has an associated
> frequency value?
> I have been thinking of a histogram, but I have found it difficult to
> implement. I have tried the following:
>
> group <- c("a", "b", "c", "d", "e")
> freq <-c(1, 2, 2, 5, 3)
> df <- data.frame(group, freq, stringsAsFactors = FALSE)
> hist(df$freq)
> library(lattice)
> histogram( ~ df$group)
> histogram( ~ as.factor(df$group))
> histogram(df$freq ~ as.factor(df$group))
>
> hist(df$freq) returns a histogram in which the values 1 and 2 appear 3
> times, the values 3 and 5 appear once and 4 never. This is not what I
> wanted; I want instead a graph telling me that a appears once, b twice
> etc.
>
> histogram( ~ df$group) gives the error:
> Error in hist.default(as.numeric(x), breaks = breaks, plot = FALSE,
> include.lowest = include.lowest,  :
>  negative length vectors are not allowed
>
> histogram( ~ as.factor(df$group)) and histogram(df$freq ~
> as.factor(df$group)) report all groups on the x axis (that is good)
> but all at 20% level.
>
> What am I missing?
> Thank you.
>
> --
> Best regards,
> Luigi
>
> __
> 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.
>
>
> Ben Tupper
> Bigelow Laboratory for Ocean Sciences
> 60 Bigelow Drive, P.O. Box 380
> East Boothbay, Maine 04544
> http://www.bigelow.org
>
> Ecological Forecasting: https://eco.bigelow.org/
>
>
>
>
>


-- 
Best regards,
Luigi

__
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] Histogram of character elements

2018-06-07 Thread Luigi Marongiu
exactly! Thank you!
but it is possible to do it with lattice? I might have an extra level
of information, for instance super-group, and in that case, I could
plot all the supergroup easily together.
On Thu, Jun 7, 2018 at 12:48 PM Ben Tupper  wrote:
>
> Hi,
>
> Is this what you are after?
>
> group <- c("a", "b", "c", "d", "e")
> freq <-c(1, 2, 2, 5, 3)
> x = rep(group, freq)
> barplot(table(x))
>
> Cheers,
> Ben
>
>
>
> On Jun 7, 2018, at 6:00 AM, Luigi Marongiu  wrote:
>
> Dear all,
> I have a dataframe with a column representing the names of the
> elements (a, b, etc) and one with their frequencies.
> How can I plot the frequencies so that each element has an associated
> frequency value?
> I have been thinking of a histogram, but I have found it difficult to
> implement. I have tried the following:
>
> group <- c("a", "b", "c", "d", "e")
> freq <-c(1, 2, 2, 5, 3)
> df <- data.frame(group, freq, stringsAsFactors = FALSE)
> hist(df$freq)
> library(lattice)
> histogram( ~ df$group)
> histogram( ~ as.factor(df$group))
> histogram(df$freq ~ as.factor(df$group))
>
> hist(df$freq) returns a histogram in which the values 1 and 2 appear 3
> times, the values 3 and 5 appear once and 4 never. This is not what I
> wanted; I want instead a graph telling me that a appears once, b twice
> etc.
>
> histogram( ~ df$group) gives the error:
> Error in hist.default(as.numeric(x), breaks = breaks, plot = FALSE,
> include.lowest = include.lowest,  :
>  negative length vectors are not allowed
>
> histogram( ~ as.factor(df$group)) and histogram(df$freq ~
> as.factor(df$group)) report all groups on the x axis (that is good)
> but all at 20% level.
>
> What am I missing?
> Thank you.
>
> --
> Best regards,
> Luigi
>
> __
> 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.
>
>
> Ben Tupper
> Bigelow Laboratory for Ocean Sciences
> 60 Bigelow Drive, P.O. Box 380
> East Boothbay, Maine 04544
> http://www.bigelow.org
>
> Ecological Forecasting: https://eco.bigelow.org/
>
>
>
>
>


-- 
Best regards,
Luigi

__
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] Histogram of character elements

2018-06-07 Thread Ben Tupper
Hi,

Is this what you are after? 

group <- c("a", "b", "c", "d", "e")
freq <-c(1, 2, 2, 5, 3)
x = rep(group, freq)
barplot(table(x))

Cheers,
Ben



> On Jun 7, 2018, at 6:00 AM, Luigi Marongiu  wrote:
> 
> Dear all,
> I have a dataframe with a column representing the names of the
> elements (a, b, etc) and one with their frequencies.
> How can I plot the frequencies so that each element has an associated
> frequency value?
> I have been thinking of a histogram, but I have found it difficult to
> implement. I have tried the following:
> 
> group <- c("a", "b", "c", "d", "e")
> freq <-c(1, 2, 2, 5, 3)
> df <- data.frame(group, freq, stringsAsFactors = FALSE)
> hist(df$freq)
> library(lattice)
> histogram( ~ df$group)
> histogram( ~ as.factor(df$group))
> histogram(df$freq ~ as.factor(df$group))
> 
> hist(df$freq) returns a histogram in which the values 1 and 2 appear 3
> times, the values 3 and 5 appear once and 4 never. This is not what I
> wanted; I want instead a graph telling me that a appears once, b twice
> etc.
> 
> histogram( ~ df$group) gives the error:
> Error in hist.default(as.numeric(x), breaks = breaks, plot = FALSE,
> include.lowest = include.lowest,  :
>  negative length vectors are not allowed
> 
> histogram( ~ as.factor(df$group)) and histogram(df$freq ~
> as.factor(df$group)) report all groups on the x axis (that is good)
> but all at 20% level.
> 
> What am I missing?
> Thank you.
> 
> -- 
> Best regards,
> Luigi
> 
> __
> 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.
> 

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org

Ecological Forecasting: https://eco.bigelow.org/






[[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] Histogram of character elements

2018-06-07 Thread Luigi Marongiu
Dear all,
I have a dataframe with a column representing the names of the
elements (a, b, etc) and one with their frequencies.
How can I plot the frequencies so that each element has an associated
frequency value?
I have been thinking of a histogram, but I have found it difficult to
implement. I have tried the following:

group <- c("a", "b", "c", "d", "e")
freq <-c(1, 2, 2, 5, 3)
df <- data.frame(group, freq, stringsAsFactors = FALSE)
hist(df$freq)
library(lattice)
histogram( ~ df$group)
histogram( ~ as.factor(df$group))
histogram(df$freq ~ as.factor(df$group))

hist(df$freq) returns a histogram in which the values 1 and 2 appear 3
times, the values 3 and 5 appear once and 4 never. This is not what I
wanted; I want instead a graph telling me that a appears once, b twice
etc.

histogram( ~ df$group) gives the error:
Error in hist.default(as.numeric(x), breaks = breaks, plot = FALSE,
include.lowest = include.lowest,  :
  negative length vectors are not allowed

histogram( ~ as.factor(df$group)) and histogram(df$freq ~
as.factor(df$group)) report all groups on the x axis (that is good)
but all at 20% level.

What am I missing?
Thank you.

-- 
Best regards,
Luigi

__
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] Plot in real unit (1:1)

2018-06-07 Thread Eik Vettorazzi
How about this:

in2mm<-25.4 # scale factor to convert inches to mm

pdf("test.pdf",width=8.3,height=11.7)
pin<-par("pin")
plot(c(0,pin[1]*in2mm),c(0,pin[2]*in2mm), type="n", xaxs="i", yaxs="i")
lines(c(10,10),c(0,10))
text(11,5,"1 cm", adj=0)

lines(c(0,40),c(20,20))
text(20,24,"4 cm")

polygon(c(50,50,70,70),c(50,70,70,50))
text(60,60,"2x2 cm")
dev.off()

cheers

Am 06.06.2018 um 16:00 schrieb Christian Brandstätter:
> Dear List, 
> 
> Is it possible to plot in R in "real" units? I would like to draw a
> plot on A4 paper, where 1 plot unit would be a mm in reality. Is
> something like that possible? I would also like to be able to scale the
> plot in x and y direction. 
> Background: For a project I would have to draw around 65 fast sketches
> of elevation courves. 
> 
> Copied from here, due to no answer: https://stackoverflow.com/questions
> /50606797/plot-in-real-units-mm
> 
> Thank you!
> 
> __
> 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.
> 

-- 
Eik Vettorazzi

Department of Medical Biometry and Epidemiology
University Medical Center Hamburg-Eppendorf

Martinistrasse 52
building W 34
20246 Hamburg

Phone: +49 (0) 40 7410 - 58243
Fax:   +49 (0) 40 7410 - 57790
Web: www.uke.de/imbe
--

_

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; 
Gerichtsstand: Hamburg | www.uke.de
Vorstandsmitglieder: Prof. Dr. Burkhard Göke (Vorsitzender), Prof. Dr. Dr. Uwe 
Koch-Gromus, Joachim Prölß, Martina Saurin (komm.)
_

SAVE PAPER - THINK BEFORE PRINTING
__
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] ROC within SEM

2018-06-07 Thread Eric Berger
Hi Ray,
Have you done any search at all? I did a search on "R package ROC latent
variable" and got several hits.
In particular the package randomLCA seems relevant

https://cran.r-project.org/web/packages/randomLCA/vignettes/randomLCA-package.pdf

I glanced at the documentation which mentions 2 other R packages that also
seem to address your needs - poLCA and BayesLCA.

HTH,
Eric


On Wed, Jun 6, 2018 at 11:48 PM, David Winsemius 
wrote:

>
> > On Jun 6, 2018, at 10:37 AM, Palmer, Raymond F 
> wrote:
> >
> > Dear R group.
> > Does anyone have an idea how to utilize a latent variable in an ROC
> (AUC) analysis? I want to create a latent variable, then use that latent
> construct as a continuous variable in an ROC.
> > I understand both SEM and ROC analysis can be done in R, but how to use
> the latent variable in the ROC is the issue.
> > Thanks for any insights.
> > Best, Ray
> >
> > Ray Palmer, Ph.D.
> > Professor
> > Department of Family and Community Medicine
> > University of Texas Health Science Center San Antonio
> > palm...@uthscsa.edu
> > 210-827-7681
> >
> >
> >   [[alternative HTML version deleted]]
>
> There are two sections from the Posting Guide that may be relevant:
>
> ---begin--
> Questions about statistics: The R mailing lists are primarily intended for
> questions and discussion about the R software. However, questions about
> statistical methodology are sometimes posted. If the question is well-asked
> and of interest to someone on the list, it may elicit an informative
> up-to-date answer. See also the Usenet groups sci.stat.consult (applied
> statistics and consulting) and sci.stat.math (mathematical stat and
> probability).
> ---end-
>
> Frankly I would not considered this question to be "well-asked" but anyone
> is free to dispute this or to take on the task of asking clarifying
> questions.
>
> A modern update might add to the Posting Guide in these more web-centric
> times, the CrossValidated.com site. https://stats.stackexchange.
> com/search?q=latent+variable+roc
>
> (Those newsgroups are basically defunct.)
>
> >
> ---begin--
> • No HTML posting (harder to detect spam) (note that this is the
> default in some mail clients - you may have to turn it off). Note that
> chances have become relatively high for ‘HTMLified’ e-mails to be
> completely intercepted (without notice to the sender).
> ---end-
>
> One of the effects of redistribution by a mail-server is the removal of
> the identity of your mail-client, so advice about that concern can only be
> "RTFM".
>
>
> > __
> > 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.
>
> David Winsemius
> Alameda, CA, USA
>
> 'Any technology distinguishable from magic is insufficiently advanced.'
>  -Gehm's Corollary to Clarke's Third Law
>
> __
> 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] aggregate and list elements of variables in data.frame

2018-06-07 Thread Massimo Bressan
thanks for the help 

I'm posting here the complete solution 

t<-data.frame(id=1:10,A=c(123,345,123,678,345,123,789,345,123,789)) 
t$A <- factor(t$A) 
l<-sapply(levels(t$A), function(x) which(t$A==x)) 
r<-data.frame(list_id=unlist(lapply(l, paste, collapse = ", "))) 
r<-cbind(unique_A=row.names(r),r) 
row.names(r)<-NULL 
r 

best 



Da: "Massimo Bressan"  
A: "r-help"  
Inviato: Mercoledì, 6 giugno 2018 10:13:10 
Oggetto: aggregate and list elements of variables in data.frame 

#given the following reproducible and simplified example 

t<-data.frame(id=1:10,A=c(123,345,123,678,345,123,789,345,123,789)) 
t 

#I need to get the following result 

r<-data.frame(unique_A=c(123, 345, 678, 
789),list_id=c('1,3,6,9','2,5,8','4','7,10')) 
r 

# i.e. aggregate over the variable "A" and list all elements of the variable 
"id" satisfying the criteria of having the same corrisponding value of "A" 
#any help for that? 

#so far I've just managed to "aggregate" and "count", like: 

library(sqldf) 
sqldf('select count(*) as count_id, A as unique_A from t group by A') 

library(dplyr) 
t%>%group_by(unique_A=A) %>% summarise(count_id = n()) 

# thank you 


-- 

 
Massimo Bressan 

ARPAV 
Agenzia Regionale per la Prevenzione e 
Protezione Ambientale del Veneto 

Dipartimento Provinciale di Treviso 
Via Santa Barbara, 5/a 
31100 Treviso, Italy 

tel: +39 0422 558545 
fax: +39 0422 558516 
e-mail: massimo.bres...@arpa.veneto.it 
 

[[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] using myfunction in stat_function

2018-06-07 Thread Jeff Newmiller
Your example is not reproducible.

Perhaps read [1]

[1] 
http://rstudio-pubs-static.s3.amazonaws.com/3365_9573f6d661b99365fe1841ee65d3.html

On June 6, 2018 8:04:44 PM PDT, Veerappa Chetty  wrote:
>HI,
>
>I use solve(A,b) inside my function, myfun2; it works fine when I
>return
>one value or a list.
>
> I want use the return values in ggplot as below: ggplot(data.frame(
>x=c(0.1,0.8)),aes(x=x))+stat_function(fun=myfun.2,geom="line")
>I get a blank graph. Would greatly appreciate help! Thanks.

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

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


Re: [R] verInd= and HorInd= arguments to pairs() function

2018-06-07 Thread Gerrit Eichner

Hi, Chris,

had the same problem (and first thought it was my fault), but there
seems to be a typo in the code of pairs.default. Below is a workaround.
Look for two comments (starting with #) in the code to see what I
have changed to make it work at least the way I'd expect it in one of
your examples.

 Hth --  Gerrit


mypairs <- function (x, labels, panel = points, ...,
horInd = 1:nc, verInd = 1:nc,
lower.panel = panel, upper.panel = panel, diag.panel = NULL,
text.panel = textPanel, label.pos = 0.5 + has.diag/3, line.main = 3,
cex.labels = NULL, font.labels = 1, row1attop = TRUE, gap = 1,
log = "") {
if (doText <- missing(text.panel) || is.function(text.panel))
textPanel <- function(x = 0.5, y = 0.5, txt, cex, font) text(x,
y, txt, cex = cex, font = font)
localAxis <- function(side, x, y, xpd, bg, col = NULL, main,
oma, ...) {
xpd <- NA
if (side%%2L == 1L && xl[j])
xpd <- FALSE
if (side%%2L == 0L && yl[i])
xpd <- FALSE
if (side%%2L == 1L)
Axis(x, side = side, xpd = xpd, ...)
else Axis(y, side = side, xpd = xpd, ...)
}
localPlot <- function(..., main, oma, font.main, cex.main) plot(...)
localLowerPanel <- function(..., main, oma, font.main, cex.main) 
lower.panel(...)
localUpperPanel <- function(..., main, oma, font.main, cex.main) 
upper.panel(...)
localDiagPanel <- function(..., main, oma, font.main, cex.main) 
diag.panel(...)

dots <- list(...)
nmdots <- names(dots)
if (!is.matrix(x)) {
x <- as.data.frame(x)
for (i in seq_along(names(x))) {
if (is.factor(x[[i]]) || is.logical(x[[i]]))
x[[i]] <- as.numeric(x[[i]])
if (!is.numeric(unclass(x[[i]])))
stop("non-numeric argument to 'pairs'")
}
}
else if (!is.numeric(x))
stop("non-numeric argument to 'pairs'")
panel <- match.fun(panel)
if ((has.lower <- !is.null(lower.panel)) && !missing(lower.panel))
lower.panel <- match.fun(lower.panel)
if ((has.upper <- !is.null(upper.panel)) && !missing(upper.panel))
upper.panel <- match.fun(upper.panel)
if ((has.diag <- !is.null(diag.panel)) && !missing(diag.panel))
diag.panel <- match.fun(diag.panel)
if (row1attop) {
tmp <- lower.panel
lower.panel <- upper.panel
upper.panel <- tmp
tmp <- has.lower
has.lower <- has.upper
has.upper <- tmp
}
nc <- ncol(x)
if (nc < 2L)
stop("only one column in the argument to 'pairs'")
if (!all(horInd >= 1L && horInd <= nc))
stop("invalid argument 'horInd'")
if (!all(verInd >= 1L && verInd <= nc))
stop("invalid argument 'verInd'")
if (doText) {
if (missing(labels)) {
labels <- colnames(x)
if (is.null(labels))
labels <- paste("var", 1L:nc)
}
else if (is.null(labels))
doText <- FALSE
}
oma <- if ("oma" %in% nmdots)
dots$oma
main <- if ("main" %in% nmdots)
dots$main
if (is.null(oma))
oma <- c(4, 4, if (!is.null(main)) 6 else 4, 4)
opar <- par(mfcol = c(length(horInd), length(verInd)),
# Changed from mfrow to mfcol
mar = rep.int(gap/2, 4), oma = oma)
on.exit(par(opar))
dev.hold()
on.exit(dev.flush(), add = TRUE)
xl <- yl <- logical(nc)
if (is.numeric(log))
xl[log] <- yl[log] <- TRUE
else {
xl[] <- grepl("x", log)
yl[] <- grepl("y", log)
}
for (j in if (row1attop) verInd else rev(verInd))
 for (i in horInd) {
# Exchanged i and j. (i used to be in
# the outer and j in the inner loop!)
l <- paste0(ifelse(xl[j], "x", ""), ifelse(yl[i], "y", ""))
localPlot(x[, j], x[, i], xlab = "", ylab = "", axes = FALSE,
type = "n", ..., log = l)
if (i == j || (i < j && has.lower) || (i > j && has.upper)) {
box()
if (i == 1 && (!(j%%2L) || !has.upper || !has.lower))
localAxis(1L + 2L * row1attop, x[, j], x[, i],
  ...)
if (i == nc && (j%%2L || !has.upper || !has.lower))
localAxis(3L - 2L * row1attop, x[, j], x[, i],
  ...)
if (j == 1 && (!(i%%2L) || !has.upper || !has.lower))
localAxis(2L, x[, j], x[, i], ...)
if (j == nc && (i%%2L || !has.upper || !has.lower))
localAxis(4L, x[, j], x[, i], ...)
mfg <- par("mfg")
if (i == j) {
if (has.diag)
  localDiagPanel(as.vector(x[, i]), ...)
if (doText) {
  par(usr = c(0, 1, 0, 1))
  if (is.null(cex.labels)) {
l.wid <- strwidth(labels, "user")
cex.labels <- max(0.8, min(2, 0.9/max(l.wid)))
  }
  xlp <- if (xl[i]