Re: [R] lattice plot: points and lines for different variables in same plotlattice plot

2015-10-04 Thread Duncan Mackay
Hi Naresh

Try

xyplot(y + y.fit ~ x | name, data = my.df,
 type = c("p","l"),
 distribute.type = TRUE,
 panel = panel.superpose
)

Your code seems to be a direct copy from the command line; sometimes it
makes it clearer to arguments and functions within the panel function on
their own line

Regards
Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mac...@northnet.com.au

-Original Message-
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Naresh
Gurbuxani
Sent: Monday, 5 October 2015 05:30
To: r-help@r-project.org
Subject: [R] lattice plot: points and lines for different variables in same
plotlattice plot

I want to draw scatter plot and a fitted line in the same lattice plot.  My
problem is that either both can be plotted as points or both as lines.  The
code that attempts to plot points for data and lines for fitted line does
not work.  

Thanks,
Naresh

library(lattice)
library(plyr)

my.df <- data.frame(x = rnorm(100), y = rnorm(100), name = "A")

temp.df <- data.frame(x = rnorm(100), name = "B")
temp.df <- within(temp.df, {y <- x + 0.2 * rnorm(100)})
my.df <- rbind(my.df, temp.df)

temp.df <- data.frame(x = rnorm(100), name = "C")
temp.df <- within(temp.df, {y <- x + 0.5 * x^2 + 0.2 * rnorm(100)})
my.df <- rbind(my.df, temp.df)

my.df <- ddply(my.df, c("name"), mutate, y.fit = lm(y ~ x +
I(x^2))$fitted.values)

my.df <- my.df[order(my.df$name, my.df$x),]

# This works
xyplot(y + y.fit ~ x | name, data = my.df, type = c("l"))

# This does not work.  Line plot seems wrong.
xyplot(y + y.fit ~ x | name, data = my.df, type = c("l"), y.prime =
my.df$y.fit, panel = function(x, y, y.prime, ...){panel.xyplot(x, y);
panel.lines(x, y.prime, type = "l")})


  
[[alternative HTML version deleted]]

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

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


Re: [R] Barplot - Beginners Question

2015-10-04 Thread Boris Steipe
On Oct 4, 2015, at 7:39 PM, Duncan Murdoch  wrote:

> You'll get what you want if you use as.matrix(datentabelle)[,1]
> instead of datentabelle[,1].

This happens to work in this particular case, but fails if the data frame 
contains text columns.
Example:
   datentabelle <- cbind(datentabelle, text = sample(letters, 
nrow(datentabelle)))
   barplot(as.matrix(datentabelle)[,1])
   R>  Error in -0.01 * height : non-numeric argument to binary operator

Therefore I would get the row names from your data frame with 
rownames(datentabelle), and use them as the names.arg argument of barplot():

  barplot(datentabelle[,1], names.arg=rownames(datentabelle))

Cheers,
Boris

__
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] Calling external file

2015-10-04 Thread Michael Dewey

In line

On 03/10/2015 23:56, Steven Yen wrote:

Thanks Bill. Simplified content of max.calls.R (with repeated calls to
maxLik removed) are shown below in the message. No, fn does not exist in
the environment.


Which explains why R cannot find it.

 I call a routine (say probit.R compiled into a library) to

use maxLik. Inside this routine,
1. In probit.R. likelihood function is defined yet in another nested
routine;


If I understand you correctly it is that function which you need to pass 
to max.calls. So alter max.calls as below



2. Function "max.calls" is also nested in that  probit.R;
Then, a call to max.calls works.

What I am trying to accomplish is, instead of inserting the identical
function (or set of lines) in every routine like probit.R, I like to either
compile max.calls.R or source it from inside probit.R. Thanks.


On Sat, Oct 3, 2015 at 4:47 AM, Steven Yen  wrote:


Hi
I collect a list of calls to a package in a function (routine) so that I
do not need to repeat the same sets of codes from program to program. In
the following, inserting the function into each program works. Then, I
place the function elsewhere in a PC folder, and include in with a 'source'
command. This does not work; it complains about a function (fn below) not
defined.
Compiling the function into a library file does not work either (with all
sorts of error messages saying this and that not defined).
Steven Yen

fn <- function(beta){
   f<-... (define f in this routine)
return(f)
}

max.calls<-function(method){


max.calls <- function(method, fn = NULL) {
   if(is.null(fn)) warning("You forgot to supply fn)

# ***
# Call maxLike with alternative algorithms
# ***
   many<-c("NR","BFGS","BFGSR","BHHH","SANN","CG","NM")
   if (method %in% many){
 ML<-maxLik(logLik=fn,grad=NULL,hess=NULL,start,
method,print.level,constraints=NULL, ...)}
return(ML)
}
# This works:
ML<-max.calls(method)


Now in probit.R replace the call to max.calls(method) with
max.calls(method, whatever_function_you 
defined_in_probit.R_for_the_likelihood)


Of course I may have completely misunderstood what you are driving at.



# Does not work:
source("z:\\R\\yenlib\\lib\\max.calls.R")
ML<-max.calls(method)

Error: object 'fn' not found




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



--
Michael
http://www.dewey.myzen.co.uk/home.html

__
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] denstrip package: densregion when density is not provided

2015-10-04 Thread Giorgio Garziano
It is likely you have some list structure you should not.
Check the class of the elements of your matrixes, to see if any list class 
shows up.

Not clear from your code what is "y" passed to densregion(..).

Anyway, one way to reproduce your error is the following:

# this does not work
> a <- list(a=1:10, b=30:40)
> a
$a
[1]  1  2  3  4  5  6  7  8  9 10

$b
[1] 30 31 32 33 34 35 36 37 38 39 40

rank(a, ties.method = "min", na.last = "keep")

Error in rank(a, ties.method = "min", na.last = "keep") :
  unimplemented type 'list' in 'greater'

# this works
b <- sample(1:10)
> b
[1]  8  9  5  2  4  1  7 10  3  6
> rank(b)
[1]  8  9  5  2  4  1  7 10  3  6
>

You may also try to debug the densregion() function.
Using RStudio it pretty straightforward.
Call this before running your code.

debug(densregion)

when executing densregion() the RStudio source-viewer will show up the 
densregion.default code and
step by step (F10) you can go through the code lines see what is going wrong.

To stop debugging, click red Stop button on console pane and then if you do not 
need to re-run the debugging,
call:

undebug(densregion)


Good luck.

--
GG

[[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] denstrip package: densregion when density is not provided

2015-10-04 Thread Matteo Richiardi
I need to estimate the density from the other time series, similarly to what
denstrip does (however, I need a whole region, and not just a strip).

This is my updated example, where I tried to estimate the density from my
bootstrapped projections using density() - as in denstrip:

require(denstrip)
x <- 2013:2015
y0.df <- read.delim("clipboard")
y0.num <- data.matrix(y0.df, rownames.force = NA)
y.df <- read.delim("clipboard")
y.num <- data.matrix(y.df, rownames.force = NA)
z <- density(y.num)
densregion(x, y, z)

where y0.num contains my "central" projection:
> head(y0.num)
   default
[1,] 0.7086428
[2,] 0.7111570
[3,] 0.7117583
[4,] 0.7124711
[5,] 0.7166852
[6,] 0.7167453

and y.num contains the bootstrapped projections:
  run1  run2  run3  run4  run5  run6
[1,] 0.6932324 0.7732179 0.6201226 0.6712345 0.6974636 0.7399447
[2,] 0.6773456 0.7971804 0.5810890 0.6775887 0.7134176 0.7480797
[3,] 0.6704025 0.8122599 0.5655451 0.6836657 0.7438639 0.7540193
[4,] 0.6671754 0.8235685 0.5663680 0.6828320 0.7650042 0.7605239
[5,] 0.6644370 0.8287378 0.5643008 0.6846030 0.7921467 0.7655395
[6,] 0.6630486 0.8333708 0.5612197 0.6882252 0.8183746 0.7687452

This is the error I get:

Error in rank(x, ties.method = "min", na.last = "keep") :
  unimplemented type 'list' in 'greater'

Matteo

On 3 October 2015 at 09:16, Matteo Richiardi 
wrote:

> I have several estimated time series, running from 2013 to 2050. 'y'
> values are constrained between 0 and 1. I would like to plot them using
> shaded colours of decreasing intensity, depending on an estimated density
> at each point x in 2013-2050.
>
> This is what I have done:
>
> require(denstrip)
> x <- 2013:2015
> y <- seq(0, 1, length=100)
> z <- read.delim("clipboard")
> densregion(x, y, z)
>
> where I imported 'z' from MS Excel. 'z' looks like (I copied only the
> first 2 columns, l but I have 100 of them):
>
> run1  run2
> 1  0.6932324 0.7732179
> 2  0.6773456 0.7971804
> ...
> 37 0.7260790 0.8724961
> 38 0.7290335 0.8755433
>
> I get the following error message:
> Error in `[.data.frame`(x, order(x, na.last = na.last, decreasing =
> decreasing)) :
>   undefined columns selected
>
> Could anybody please help me with fixing this? Thanks in advance
>

[[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-es] error

2015-10-04 Thread juan(uned)
Hola Jos� si importas el archivo csv ver�s que hay columnas que tienes 
valores num�ricos y caracteres, por ejemplo Costa Rica. Adem�s debes 
modificar la importaci�n para que te tome los valores num�ricos dec="," 
. En una palabra abre el fichero csv con excel y encolumna adecuadamente 
las variables.

Saludos,

Juan


El 04/10/2015 a las 13:16, josebetancourt.cmw escribi�:
> rm(list = ls())
> setwd("D:/Public/Documents/biblio a revisar/ analisis 
> detalles/r_epidemiolog�a/DATA/")
> data<- read.csv('maternalR.csv',header=TRUE, sep=";", dec=";")
> attach(data)
> library(sjPlot)
> sjp.pca(data[,2:4], numberOfFactors = NULL, factorLoadingTolerance = 0.1,
> plotEigenvalues = FALSE, digits = 2, title = NULL,
> axisLabels.y = NULL, type = "b", geom.size = 0.6,
> geom.colors = "RdBu", breakTitleAt = 50, breakLabelsAt = 30,
> showValueLabels = TRUE, showCronbachsAlpha = TRUE, printPlot = TRUE)
> prcomp(data[,2:4])

-- 
Juan Antonio Gil Pascual
Profesor de Metodolog�a de la Investigaci�n Cuantitativa
correo: j...@edu.uned.es
web: www.uned.es/personal/jgil

Dpto. MIDE
Facultad de Educaci�n
c/Juan del Rosal, 14 desp. 2.72
28040 Madrid
Tel�f. 91 3987279
Fax. 91 3987288


[[alternative HTML version deleted]]

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


Re: [R] Calling external file

2015-10-04 Thread William Dunlap
Does the following pattern resemble what you have?

Here are a couple of functions that use the same code for finding the
location of the minimum of a function fn:
   f1 <- function(x) {
   fn <- function(beta) {
   sum((x-beta)^2)
   }
   argMinFn <- function() {
   beta <- seq(0, 1, len=129)
   beta[which.min(vapply(beta, fn, 0))]
   }
   argMinFn()
   }
   f2 <- function(x) {
   fn <- function(beta) {
   sum(abs(x-beta))
   }
   argMinFn <- function() {
   beta <- seq(0, 1, len=129)
   beta[which.min(vapply(beta, fn, 0))]
   }
   argMinFn()
   }
used as
   > f1(1/(1:10)) # approx. mean of 1/(1:10)
   [1] 0.2890625
   > f2(1/(1:10)) # approx median of 1/(1:10)
   [1] 0.171875

I think you are trying to avoid copying that argMinFn into every
function of this sort
so you move it out of the f1 and f2 functions:
   argMinFn.bad <- function() {
   beta <- seq(0, 1, len=129)
   beta[which.min(vapply(beta, fn, 0))]
   }
and omit it from f1 and f2
 f1a.bad <- function(x) {
   fn <- function(beta) {
   sum((x-beta)^2)
   }
   argMinFn.bad()
   }
Then you get the sort of error you describe
  > f1a.bad(1/(1:10))
  Error in match.fun(FUN) : object 'fn' not found
The problem is that a function first looks for objects defined in its own
environment, then in the environment in which the function was created,
then the parent of that environment, etc.  The stand-along argMinFn
was defined in the global environment so it does not look in the environment
of f1a.bad for fn.

The fix is to make fn an argument to the stand-along argMinFn and have
f1a.good pass fn into the environment of argMinFn via the argument list.
 argMinFn.good <- function(fn) {
 beta <- seq(0, 1, len=129)
 beta[which.min(vapply(beta, fn, 0))]
 }
 f1a.good <- function(x)
 {
fn <- function(beta) {
  sum((x-beta)^2)
}
argMinFn.good(fn)
 }
e.g
> f1a.good(1/(1:10))
[1] 0.2890625

How functions look up variables is called 'scoping' and R's method is
called 'lexical scoping'.  You can find lots more information on this with
google.
Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Sat, Oct 3, 2015 at 3:56 PM, Steven Yen  wrote:
> Thanks Bill. Simplified content of max.calls.R (with repeated calls to
> maxLik removed) are shown below in the message. No, fn does not exist in
> the environment. I call a routine (say probit.R compiled into a library) to
> use maxLik. Inside this routine,
> 1. In probit.R. likelihood function is defined yet in another nested
> routine;
> 2. Function "max.calls" is also nested in that  probit.R;
> Then, a call to max.calls works.
>
> What I am trying to accomplish is, instead of inserting the identical
> function (or set of lines) in every routine like probit.R, I like to either
> compile max.calls.R or source it from inside probit.R. Thanks.
>
>
> On Sat, Oct 3, 2015 at 4:47 AM, Steven Yen  wrote:
>
>> Hi
>> I collect a list of calls to a package in a function (routine) so that I
>> do not need to repeat the same sets of codes from program to program. In
>> the following, inserting the function into each program works. Then, I
>> place the function elsewhere in a PC folder, and include in with a 'source'
>> command. This does not work; it complains about a function (fn below) not
>> defined.
>> Compiling the function into a library file does not work either (with all
>> sorts of error messages saying this and that not defined).
>> Steven Yen
>>
>> fn <- function(beta){
>>   f<-... (define f in this routine)
>> return(f)
>> }
>>
>> max.calls<-function(method){
>> # ***
>> # Call maxLike with alternative algorithms
>> # ***
>>   many<-c("NR","BFGS","BFGSR","BHHH","SANN","CG","NM")
>>   if (method %in% many){
>> ML<-maxLik(logLik=fn,grad=NULL,hess=NULL,start,
>>method,print.level,constraints=NULL, ...)}
>> return(ML)
>> }
>> # This works:
>> ML<-max.calls(method)
>>
>> # Does not work:
>> source("z:\\R\\yenlib\\lib\\max.calls.R")
>> ML<-max.calls(method)
>>
>> Error: object 'fn' not found
>>
>>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

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


[R] Johansen Test of Cointegration:How to access rows in R output

2015-10-04 Thread Preetam Pal
Hi guys,

I ran ca.jo(data,type="trace", ecdet="none",k=2) i.e. Johansen's Trace test
on R-Studio (package: "urca")and got the output below:

I have 3 questions about this:

A> How do I programmatically access the columns("test", "10pct" etc) in any
row corresponding to, say, r < = 1 in the output?  I mean, I shall only
provide the r-value as an input and all the (column name, column value)
pairs will be outputted to me.

B> How do I write a code that will check if the null hypotheses for r =0,
<= 1, <= 2 and so on  (in this order) are rejected or not; and in case one
of them is rejected, it checks the next higher value of r and gives the
final inference , something like "Null not rejected for r <= *appropriate
r-value from this table* " or "All nulls rejected" or "No null rejected'.

C> Also, I need to extract the eigen vectors from the Cointegration Matrix
below to get the cointegrated transfoms.

I have attached the data for your perusal. If I need to provide anything
more, please let me know.

Regards,
Preetam

##
# Johansen-Procedure #
##

Test type: trace statistic , with linear trend in cointegration

Eigenvalues (lambda):
[1] 7.935953e-01 5.444372e-01 4.985327e-01 2.562245e-01 5.551115e-16

Values of teststatistic and critical values of test:

  test 10pct  5pct  1pct
r <= 3 |  6.81 10.49 12.25 16.26
r <= 2 | 22.68 22.76 25.32 30.45
r <= 1 | 40.77 39.06 42.44 48.45
r = 0  | 77.06 59.14 62.99 70.05

Eigenvectors, normalised to first column:
(These are the cointegration relations)

   GDP.l2 HPA.l2   FX.l2Y.l2   trend.l2
GDP.l21.0  1.000  1.  1.  1.000
HPA.l22.52550  0.1569079  0.08077351 -0.22777550 -0.9178250
FX.l2-8.643729121 -2.5815150  0.17158404 -0.47053012 -4.8528875
Y.l2  0.805229998 -1.4241546  0.07767540  0.02303305  0.5213294
trend.l2  0.006283314  0.0385276 -0.01512016  0.01986813 -0.9516072

Weights W:
(This is the loading matrix)

   GDP.l2  HPA.l2  FX.l2Y.l2  trend.l2
GDP.d  0.03055313 -0.04681978 -0.8376985 -0.04220534 -1.271960e-17
HPA.d -0.22649596 -0.24287691 -1.6358880  2.03813569 -8.002467e-17
FX.d   0.10327579  0.15150469 -0.1649066  0.37449910 -2.570250e-18
Y.d   -0.35200485  0.56808024 -5.7829738  0.01000965  1.730461e-16
GDP HPA FX  Y
0.514662421 0.635997077 1.37802145  1.773342598
0.9367223.127683176 1.391916535 3.709809052
0.101482324 1.270555421 0.831157511 0.226267793
0.017548634 2.456061547 1.003945759 9.510258161
0.236462416 0.988324147 0.223682679 5.026671536
0.372005149 2.177631629 0.904226065 4.219235789
0.153915709 4.620341653 0.033410743 3.17396006
0.524887329 1.050861084 0.518201484 7.950098612
0.776616937 0.503349512 0.666089868 3.320938471
0.760074361 3.635853456 0.470220952 6.380945175
0.802986662 1.260738545 0.452674872 1.036040804
0.375145127 0.20035625  1.837306306 6.486871565
0.002568896 3.532359526 0.556752154 8.536594244
0.754309276 3.952381767 0.247402168 8.559081716
0.585966577 4.01463047  1.184382133 0.148121669
0.39767356  1.553753452 0.983129422 5.378373676
0.859898623 4.73191381  0.828795696 3.367809329
0.741376169 4.993350692 1.758051281 5.516460988
0.329240391 3.465836416 1.701655508 1.249497907
0.078661064 3.298298811 0.04575857  5.132921426
0.270971873 0.46627043  1.739487411 4.94697541
0.731072625 0.940642982 0.728747166 7.583041122
0.385038046 3.51048946  0.021866584 7.361148458
0.530760376 1.204422978 0.415530715 1.163503483
0.555323667 4.12592 1.844184811 8.596644394
__
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] (no subject)

2015-10-04 Thread FERNANDO MANSITO CABALLERO
Dear Madam/Sir,

I  am   trying to understand  R and I have come to a stumbling block. i
have written:

>Empl <- list(employee="Anna",family=list(spouse="Fred",children=3,
+child.ages=c(4,7,9)),employee="John",family=list(spouse="Mary",children=2,
+child.ages=c(14,17)))
>Empl[c(2,4)]$family$spouse
[1] "Fred"
>#instead of [1] "Fred" "Mary"

Where am I wrong?

Thank you very much for your patience
Yours truly,
Fernando Mansito

[[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] (no subject)

2015-10-04 Thread Barry Rowlingson
lists in R can have multiple elements with the same name but if you
try and access elements by name you only get the first.

For example:

 > a = list(x=99, x=23, x=456)
 > a$x
 [1] 99

Its just the way it is.

Note you might find the `str` function useful to see the structure of R objects:

 > str(Empl)
List of 4
 $ employee: chr "Anna"
 $ family  :List of 3
  ..$ spouse: chr "Fred"
  ..$ children  : num 3
  ..$ child.ages: num [1:3] 4 7 9
 $ employee: chr "John"
 $ family  :List of 3
  ..$ spouse: chr "Mary"
  ..$ children  : num 2
  ..$ child.ages: num [1:2] 14 17
 > str(Empl[c(2,4)])
List of 2
 $ family:List of 3
  ..$ spouse: chr "Fred"
  ..$ children  : num 3
  ..$ child.ages: num [1:3] 4 7 9
 $ family:List of 3
  ..$ spouse: chr "Mary"
  ..$ children  : num 2
  ..$ child.ages: num [1:2] 14 17
 > str(Empl[c(2,4)]$family)
List of 3
 $ spouse: chr "Fred"
 $ children  : num 3
 $ child.ages: num [1:3] 4 7 9

With your current data structure you might need to use the
list-processing functions like `sapply` and `lapply` to get out the
spouse names from your list:

 > sapply(Empl[c(2,4)], function(x){x$spouse})
 family family
 "Fred" "Mary"

Keep at it!

On Sun, Oct 4, 2015 at 7:31 PM, FERNANDO MANSITO CABALLERO
 wrote:
> Dear Madam/Sir,
>
> I  am   trying to understand  R and I have come to a stumbling block. i
> have written:
>
>>Empl <- list(employee="Anna",family=list(spouse="Fred",children=3,
> +child.ages=c(4,7,9)),employee="John",family=list(spouse="Mary",children=2,
> +child.ages=c(14,17)))
>>Empl[c(2,4)]$family$spouse
> [1] "Fred"
>>#instead of [1] "Fred" "Mary"
>
> Where am I wrong?
>
> Thank you very much for your patience
> Yours truly,
> Fernando Mansito
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

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


Re: [R] (no subject)

2015-10-04 Thread jim holtman
You need to break down your expression into parts and see what the data is:

> Empl[c(2,4)]
$family
$family$spouse
[1] "Fred"

$family$children
[1] 3

$family$child.ages
[1] 4 7 9


$family
$family$spouse
[1] "Mary"

$family$children
[1] 2

$family$child.ages
[1] 14 17


> Empl[c(2,4)]$family  #>> notice only picks the first object
$spouse
[1] "Fred"

$children
[1] 3

$child.ages
[1] 4 7 9


To get multiple objects, then you need to use some of the 'apply'
functions; in this case 'sapply':

 > sapply(Empl[c(2,4)], '[[', 'spouse')
family family
"Fred" "Mary"



Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

On Sun, Oct 4, 2015 at 2:31 PM, FERNANDO MANSITO CABALLERO <
fernando.mans...@gmail.com> wrote:

> Dear Madam/Sir,
>
> I  am   trying to understand  R and I have come to a stumbling block. i
> have written:
>
> >Empl <- list(employee="Anna",family=list(spouse="Fred",children=3,
> +child.ages=c(4,7,9)),employee="John",family=list(spouse="Mary",children=2,
> +child.ages=c(14,17)))
> >Empl[c(2,4)]$family$spouse
> [1] "Fred"
> >#instead of [1] "Fred" "Mary"
>
> Where am I wrong?
>
> Thank you very much for your patience
> Yours truly,
> Fernando Mansito
>
> [[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] (no subject)

2015-10-04 Thread David Winsemius

On Oct 4, 2015, at 11:31 AM, FERNANDO MANSITO CABALLERO wrote:

> Dear Madam/Sir,
> 
> I  am   trying to understand  R and I have come to a stumbling block. i
> have written:
> 
>> Empl <- list(employee="Anna",family=list(spouse="Fred",children=3,
> +child.ages=c(4,7,9)),employee="John",family=list(spouse="Mary",children=2,
> +child.ages=c(14,17)))
>> $family$spouse
> [1] "Fred"
>> #instead of [1] "Fred" "Mary"
> 
> Where am I wrong?

The $ function is short-hand for "[[" (with an unevaluated argument). The "[[" 
function is not able to deliver multiple values. You might think you needed to 
use:

sapply( Empl[c(2,4)], function(x){ x$family$spouse )


And you cannot use that construction or its equivalent, because sapply and 
lapply do not pass the names of their arguments:

> sapply( Empl[c(2,4)], function(x){ x[['family']]['spouse']} )
$family
NULL

$family
NULL

#---


This succeeds:

> sapply( Empl[grepl('family', names(Empl)) ], function(x){x$spouse})
family family 
"Fred" "Mary" 


-- 

David Winsemius
Alameda, CA, USA

__
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] (no subject)

2015-10-04 Thread Giorgio Garziano
Good question.

> str(Empl[c(2,4)])
List of 2
$ family:List of 3
  ..$ spouse: chr "Fred"
  ..$ children  : num 3
  ..$ child.ages: num [1:3] 4 7 9
$ family:List of 3
  ..$ spouse: chr "Mary"
  ..$ children  : num 2
  ..$ child.ages: num [1:2] 14 17
>

> Empl[c(2,4)][1]
$family
$family$spouse
[1] "Fred"

$family$children
[1] 3

$family$child.ages
[1] 4 7 9


> Empl[c(2,4)][2]
$family
$family$spouse
[1] "Mary"

$family$children
[1] 2

$family$child.ages
[1] 14 17


> Empl[c(2,4)][[1]][1]
$spouse
[1] "Fred"

> Empl[c(2,4)][[2]][1]
$spouse
[1] "Mary"


> Empl[c(2,4)][[1]]$spouse
[1] "Fred"


> Empl[c(2,4)][[2]]$spouse
[1] "Mary"


[[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] Barplot - Beginners Question

2015-10-04 Thread Duncan Murdoch
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA512

On 04/10/2015 3:31 PM, Jutta Wrage wrote:
> Hi!
> 
> According to this I tried to create a barplot:
> 
> https://de.wikibooks.org/wiki/GNU_R:_barplot
> 
> Input Data (Tab delimeted)
> 
> Range   Anzahl  Prozent 36-40   12  1.92 41-45   21  3.36 
> 46-50   48  7.68 51-55   87  13.92 56-60   92  14.72 
> 61-65   131 20.96 66-70   67  10.72
> 
> I read the table using this command:
>> datentabelle <- read.table( "stats-test.txt", header = TRUE, sep
>> = "\t", dec = ".", row.names=1) datentabelle
> Anzahl Prozent 36-40 121.92 41-45 213.36 46-50
> 487.68 51-55 87   13.92 56-60 92   14.72 61-65131
> 20.96 66-70 67   10.72 71-75 528.32 76-80 25
> 4.00
>> is.data.frame(datentabelle)
> [1] TRUE
>> 
> 
> Looking ate the table it looks like VADeaths
> 
> Then I try to plot as described in wikibooks
> 
>> barplot(datentabelle[,1], main= "Altersverteilung")
> 
> The plot I get looks lie that using VADeaths with one difference: I
> do not ret the rownames in my table as labels for the bars
> 
> So what am I missing?

Your data is a dataframe, VADeaths is a matrix.  For some dim
historical reason, pulling a column from a matrix keeps the row names,
but pulling a column from a dataframe doesn't.

You'll get what you want if you use as.matrix(datentabelle)[,1]
instead of datentabelle[,1].

Duncan Murdoch

> 
> I expect something like I get from
>> barplot(VADeaths[,1], main= "Altersverteilung")
> 
> Jutta
> 
> 
> 
> -- http://www.witch.westfalen.de
> 
> 
> 
> __ 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.
> 

-BEGIN PGP SIGNATURE-
Version: GnuPG/MacGPG2 v2
Comment: GPGTools - https://gpgtools.org

iQEcBAEBCgAGBQJWEbjKAAoJEHE2Kz23YMZyx8sH/iX62bx7uRmXHFSbJbYMBUxf
+WTDZyO0nQFq8fLwti2NWuTchWvu/hBoIWheIGPCe7xF9ApoJydHmAc4DIf6xoqT
nngQS2F9mGyjgfMagnH68pvMI/W8bwOW3VIJ2nb5nhpSPF/yQZsKLewcyI+L1uiZ
uDPeol7Ig6ij7amGnz4haa1g7wRhUp/mAcFn8gBuDOh5Y4vFpqdba1NcRUQP5Kj8
4x+wN4eKqX0uT2IV0GkGfNT1R4sYR6g+Ardml+vQZ5E+fRrZxi6SoOSRWgptiBAM
cqad7Mfll6RdPWTVCwTlctTYw0arIzNsMaUoG7E+QlcbKviMCNx8IRZUScJP/5c=
=B1u9
-END PGP SIGNATURE-

__
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] Barplot - Beginners Question

2015-10-04 Thread Jutta Wrage
Hi!

According to this I tried to create a barplot:

https://de.wikibooks.org/wiki/GNU_R:_barplot

Input Data (Tab delimeted)

Range   Anzahl  Prozent
36-40   12  1.92
41-45   21  3.36
46-50   48  7.68
51-55   87  13.92
56-60   92  14.72
61-65   131 20.96
66-70   67  10.72

I read the table using this command:
> datentabelle <- read.table( "stats-test.txt", header = TRUE, sep = "\t", dec 
> = ".", row.names=1)
> datentabelle
  Anzahl Prozent
36-40 121.92
41-45 213.36
46-50 487.68
51-55 87   13.92
56-60 92   14.72
61-65131   20.96
66-70 67   10.72
71-75 528.32
76-80 254.00
> is.data.frame(datentabelle)
[1] TRUE
>

Looking ate the table it looks like VADeaths

Then I try to plot as described in wikibooks

> barplot(datentabelle[,1], main= "Altersverteilung")

The plot I get looks lie that using VADeaths with one difference:
I do not ret the rownames in my table as labels for the bars

So what am I missing?

I expect something like I get from
> barplot(VADeaths[,1], main= "Altersverteilung")

Jutta



--
http://www.witch.westfalen.de



signature.asc
Description: Message signed with OpenPGP using GPGMail
__
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.