Re: [R] cbind question, please

2015-04-24 Thread Kehl Dániel
Hello,

I am not sure what you mean by a matrix. If you want to have a matrix, use the 
function matrix, (matrix(c(dog,cat,tree),3))
but I have the feeling you really want a data frame as you are talking about 
variables.
In that case simply use 

mydataframe - data.frame(dog,cat,tree)

If you are not sure what you want, please read the intro to R pdf which is 
included in your installed R library.

Best regards,
daniel

Feladó: R-help [r-help-boun...@r-project.org] ; meghatalmaz#243;: Erin Hodgess 
[erinm.hodg...@gmail.com]
Küldve: 2015. április 24. 0:41
To: R help
Tárgy: [R]  cbind question, please

Hello!

I have a cbind type question, please:  Suppose I have the following:

dog - 1:3
cat - 2:4
tree - 5:7

and a character vector
big.char - c(dog,cat,tree)

I want to end up with a matrix that is a cbind of dog, cat, and tree.
This is a toy example.  There will be a bunch of variables.

I experimented with do.call, but all I got was
1
2
3

Any suggestions would be much appreciated.  I still think that do.call
might be the key, but I'm not sure.

R Version 3-1.3, Windows 7.

Thanks,
Erin


--
Erin Hodgess
Associate Professor
Department of Mathematical and Statistics
University of Houston - Downtown
mailto: erinm.hodg...@gmail.com

[[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] cbind question, please

2015-04-24 Thread Rolf Turner

On 24/04/15 10:41, Erin Hodgess wrote:

Hello!

I have a cbind type question, please:  Suppose I have the following:

dog - 1:3
cat - 2:4
tree - 5:7

and a character vector
big.char - c(dog,cat,tree)

I want to end up with a matrix that is a cbind of dog, cat, and tree.
This is a toy example.  There will be a bunch of variables.

I experimented with do.call, but all I got was
1
2
3


I don't understand how you managed to get *that*.  When I did the 
obvious thing --- do.call(cbind,as.list(big.char)) --- I got

(as expected :-) )

 [,1]  [,2]  [,3]
[1,] dog cat tree



Any suggestions would be much appreciated.  I still think that do.call
might be the key, but I'm not sure.

R Version 3-1.3, Windows 7.


do.call(cbind,lapply(big.char,get,envir=.GlobalEnv))

Note:  I had to throw in the specification of envir otherwise get() 
got the cat() function from base rather than your vector cat.


Probably not a problem for your real application; shouldn't hurt, but.

Another salutary example of why it's not a good idea to give data sets 
names that are names of R built-ins.


cheers,

Rolf

--
Rolf Turner
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276
Home phone: +64-9-480-4619

__
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] cbind question, please

2015-04-24 Thread John Kane
What are you expecting?

dog - 1:3
cat - 2:4
tree - 5:7
big.char - c(dog,cat,tree)

xx -  cbind(dog, cat, tree, big.char)

gives me 
xx1  -  structure(c(1, 2, 3, 2, 3, 4, 5, 6, 7, dog, 
cat, tree), .Dim = 3:4, .Dimnames = list(NULL, c(dog, cat, 
tree, big.char)))



John Kane
Kingston ON Canada


 -Original Message-
 From: erinm.hodg...@gmail.com
 Sent: Thu, 23 Apr 2015 18:41:05 -0400
 To: r-h...@stat.math.ethz.ch
 Subject: [R] cbind question, please
 
 Hello!
 
 I have a cbind type question, please:  Suppose I have the following:
 
 dog - 1:3
 cat - 2:4
 tree - 5:7
 
 and a character vector
 big.char - c(dog,cat,tree)
 
 I want to end up with a matrix that is a cbind of dog, cat, and tree.
 This is a toy example.  There will be a bunch of variables.
 
 I experimented with do.call, but all I got was
 1
 2
 3
 
 Any suggestions would be much appreciated.  I still think that do.call
 might be the key, but I'm not sure.
 
 R Version 3-1.3, Windows 7.
 
 Thanks,
 Erin
 
 
 --
 Erin Hodgess
 Associate Professor
 Department of Mathematical and Statistics
 University of Houston - Downtown
 mailto: erinm.hodg...@gmail.com
 
   [[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.


FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks  orcas on your 
desktop!

__
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] cbind question, please

2015-04-24 Thread Martin Maechler
 Steve Taylor steve.tay...@aut.ac.nz
 on Thu, 23 Apr 2015 23:32:00 + writes:

 This works for me...
 get0 = function(x) get(x,pos=1)
 sapply(big.char, get0)

Note that  get0() is a _ somewhat important for efficient code _
new function since R 3.2.0
so you'd rather call your functions differently...

 The extra step seems necessary because without it, get() gets base::cat() 
instead of cat.

 cheers,
 Steve

 -Original Message-
 From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Erin 
Hodgess
 Sent: Friday, 24 April 2015 10:41a
 To: R help
 Subject: [R] cbind question, please

 Hello!

 I have a cbind type question, please:  Suppose I have the following:

 dog - 1:3
 cat - 2:4
 tree - 5:7

 and a character vector
 big.char - c(dog,cat,tree)

 I want to end up with a matrix that is a cbind of dog, cat, and tree.
 This is a toy example.  There will be a bunch of variables.

 I experimented with do.call, but all I got was
 1
 2
 3

 Any suggestions would be much appreciated.  I still think that do.call
 might be the key, but I'm not sure.

 R Version 3-1.3, Windows 7.

 Thanks,
 Erin


 -- 
 Erin Hodgess
 Associate Professor
 Department of Mathematical and Statistics
 University of Houston - Downtown
 mailto: erinm.hodg...@gmail.com

 [[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-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] cbind question, please

2015-04-24 Thread Erin Hodgess
Here is the big picture.  I have a character vector with all of the names
of the variables in it.

I want to cbind all of the variables to create a matrix.

Doing 3 is straightforward, but many, not so much.

Hence my question.

Thanks so much for your answers!

Sincerely,
Erin


On Thu, Apr 23, 2015 at 7:44 PM, John Kane jrkrid...@inbox.com wrote:

 What are you expecting?

 dog - 1:3
 cat - 2:4
 tree - 5:7
 big.char - c(dog,cat,tree)

 xx -  cbind(dog, cat, tree, big.char)

 gives me
 xx1  -  structure(c(1, 2, 3, 2, 3, 4, 5, 6, 7, dog,
 cat, tree), .Dim = 3:4, .Dimnames = list(NULL, c(dog, cat,
 tree, big.char)))



 John Kane
 Kingston ON Canada


  -Original Message-
  From: erinm.hodg...@gmail.com
  Sent: Thu, 23 Apr 2015 18:41:05 -0400
  To: r-h...@stat.math.ethz.ch
  Subject: [R] cbind question, please
 
  Hello!
 
  I have a cbind type question, please:  Suppose I have the following:
 
  dog - 1:3
  cat - 2:4
  tree - 5:7
 
  and a character vector
  big.char - c(dog,cat,tree)
 
  I want to end up with a matrix that is a cbind of dog, cat, and tree.
  This is a toy example.  There will be a bunch of variables.
 
  I experimented with do.call, but all I got was
  1
  2
  3
 
  Any suggestions would be much appreciated.  I still think that do.call
  might be the key, but I'm not sure.
 
  R Version 3-1.3, Windows 7.
 
  Thanks,
  Erin
 
 
  --
  Erin Hodgess
  Associate Professor
  Department of Mathematical and Statistics
  University of Houston - Downtown
  mailto: erinm.hodg...@gmail.com
 
[[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.

 
 FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks  orcas on
 your desktop!
 Check it out at http://www.inbox.com/marineaquarium





-- 
Erin Hodgess
Associate Professor
Department of Mathematical and Statistics
University of Houston - Downtown
mailto: erinm.hodg...@gmail.com

[[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] cbind question, please

2015-04-24 Thread Michael Hannon
Is this what you're looking for?

 dog - 1:3

 bat - 2:4

 tree - 5:7

 big.char - c(dog,bat,tree)

 do.call(cbind,lapply(big.char, get))
 [,1] [,2] [,3]
[1,]125
[2,]236
[3,]347



On Thu, Apr 23, 2015 at 3:41 PM, Erin Hodgess erinm.hodg...@gmail.com wrote:
 Hello!

 I have a cbind type question, please:  Suppose I have the following:

 dog - 1:3
 cat - 2:4
 tree - 5:7

 and a character vector
 big.char - c(dog,cat,tree)

 I want to end up with a matrix that is a cbind of dog, cat, and tree.
 This is a toy example.  There will be a bunch of variables.

 I experimented with do.call, but all I got was
 1
 2
 3

 Any suggestions would be much appreciated.  I still think that do.call
 might be the key, but I'm not sure.

 R Version 3-1.3, Windows 7.

 Thanks,
 Erin


 --
 Erin Hodgess
 Associate Professor
 Department of Mathematical and Statistics
 University of Houston - Downtown
 mailto: erinm.hodg...@gmail.com

 [[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] cbind question, please

2015-04-24 Thread Berwin A Turlach
G'day Erin,

On Thu, 23 Apr 2015 20:51:18 -0400
Erin Hodgess erinm.hodg...@gmail.com wrote:

 Here is the big picture.  I have a character vector with all of the
 names of the variables in it.
 
 I want to cbind all of the variables to create a matrix.
 
 Doing 3 is straightforward, but many, not so much.

So I guess you want something like:

R do.call(cbind, sapply(big.char, as.name))
 dog cat tree
[1,]   1   25
[2,]   2   36
[3,]   3   47

Which does not seem to have the problem of confusing the numeric vector
`cat' with the function 'cat'.

HTH.

Cheers,

Berwin

__
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] cbind question, please

2015-04-24 Thread Rolf Turner


I am amazed at the number of rather obtuse misunderstandings of the 
actual nature of Erin's question.


The suggestion that Erin should read the intro to R made me smile.  Erin 
is a long time and highly sophisticated user of R; she has no need to 
read the intro.  The person who made that suggestion should have read 
her question more thoughtfully.


Also I liked Steve Taylor's and Marc Swartz's nifty solutions that use 
sapply(); much sexier than my rather kludgy effort using do.call(). 
Berwin Turlach's combination of do.call() and sapply() is pretty sexy too.


cheers,

Rolf Turner

--
Rolf Turner
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276
Home phone: +64-9-480-4619

__
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] cbind question, please

2015-04-24 Thread David Kienle

Hello Erin,

I think you have explain your goal more detailed. Maybe I am completely 
lost but as far as I understand now you only need the command cbind:


m1 - cbind(dog, dat, tree)

 dog cat tree
[1,]   1   25
[2,]   2   36
[3,]   3   47

But I can't imagine that is the solution you are looking for.

Cheers
David


On 24.04.2015 00:41, Erin Hodgess wrote:

Hello!

I have a cbind type question, please:  Suppose I have the following:

dog - 1:3
cat - 2:4
tree - 5:7

and a character vector
big.char - c(dog,cat,tree)

I want to end up with a matrix that is a cbind of dog, cat, and tree.
This is a toy example.  There will be a bunch of variables.

I experimented with do.call, but all I got was
1
2
3

Any suggestions would be much appreciated.  I still think that do.call
might be the key, but I'm not sure.

R Version 3-1.3, Windows 7.

Thanks,
Erin




--
David Kienle

Department of Biogeography
University of Bayreuth

GEO II, Rm 003

__
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] cbind question, please

2015-04-24 Thread William Dunlap
You could do something tricky like
   do.call(cbind, lapply(big.char, as.name))
   dog cat tree
  [1,]   1   25
  [2,]   2   36
  [3,]   3   47
but you are usually better off creating these things as part of a list
and passing that to do.call(cbind, list).

There is a slight danger of using do.call with cbind.  If your
list has a component with the unlikely name 'deparse.level',
then that will be taken as cbind's deparse.level argument,
not as a column of the matrix to be made.


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, Apr 23, 2015 at 3:41 PM, Erin Hodgess erinm.hodg...@gmail.com
wrote:

 Hello!

 I have a cbind type question, please:  Suppose I have the following:

 dog - 1:3
 cat - 2:4
 tree - 5:7

 and a character vector
 big.char - c(dog,cat,tree)

 I want to end up with a matrix that is a cbind of dog, cat, and tree.
 This is a toy example.  There will be a bunch of variables.

 I experimented with do.call, but all I got was
 1
 2
 3

 Any suggestions would be much appreciated.  I still think that do.call
 might be the key, but I'm not sure.

 R Version 3-1.3, Windows 7.

 Thanks,
 Erin


 --
 Erin Hodgess
 Associate Professor
 Department of Mathematical and Statistics
 University of Houston - Downtown
 mailto: erinm.hodg...@gmail.com

 [[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] cbind question, please

2015-04-24 Thread Clint Bowman

Perhaps:


dog - 1:3
cat - 2:4
tree - 5:7
big.char - cbind(dog,cat,tree)
big.char

 dog cat tree
[1,]   1   25
[2,]   2   36
[3,]   3   47

colnames(big.char)-c(dog,cat,tree)
big.char

 dog cat tree
[1,]   1   25
[2,]   2   36
[3,]   3   47



Clint BowmanINTERNET:   cl...@ecy.wa.gov
Air Quality Modeler INTERNET:   cl...@math.utah.edu
Department of Ecology   VOICE:  (360) 407-6815
PO Box 47600FAX:(360) 407-7534
Olympia, WA 98504-7600

USPS:   PO Box 47600, Olympia, WA 98504-7600
Parcels:300 Desmond Drive, Lacey, WA 98503-1274

On Thu, 23 Apr 2015, Erin Hodgess wrote:


Hello!

I have a cbind type question, please:  Suppose I have the following:

dog - 1:3
cat - 2:4
tree - 5:7

and a character vector
big.char - c(dog,cat,tree)

I want to end up with a matrix that is a cbind of dog, cat, and tree.
This is a toy example.  There will be a bunch of variables.

I experimented with do.call, but all I got was
1
2
3

Any suggestions would be much appreciated.  I still think that do.call
might be the key, but I'm not sure.

R Version 3-1.3, Windows 7.

Thanks,
Erin


--
Erin Hodgess
Associate Professor
Department of Mathematical and Statistics
University of Houston - Downtown
mailto: erinm.hodg...@gmail.com

[[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] cbind question, please

2015-04-24 Thread Jim Lemon
Hi Erin,
Well, if I do this:

dog - 1:3
cat - 2:4
tree - 5:7
dct-cbind(dog,cat,tree)

I get this:

dct
 dog cat tree
[1,]   1   25
[2,]   2   36
[3,]   3   47

If I assume that you want to include the character vector as well:

rownames(dct)-big.char
dct

Jim

On Fri, Apr 24, 2015 at 8:41 AM, Erin Hodgess erinm.hodg...@gmail.com wrote:
 Hello!

 I have a cbind type question, please:  Suppose I have the following:

 dog - 1:3
 cat - 2:4
 tree - 5:7

 and a character vector
 big.char - c(dog,cat,tree)

 I want to end up with a matrix that is a cbind of dog, cat, and tree.
 This is a toy example.  There will be a bunch of variables.

 I experimented with do.call, but all I got was
 1
 2
 3

 Any suggestions would be much appreciated.  I still think that do.call
 might be the key, but I'm not sure.

 R Version 3-1.3, Windows 7.

 Thanks,
 Erin


 --
 Erin Hodgess
 Associate Professor
 Department of Mathematical and Statistics
 University of Houston - Downtown
 mailto: erinm.hodg...@gmail.com

 [[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] cbind question, please

2015-04-24 Thread Marc Schwartz
On Apr 23, 2015, at 5:41 PM, Erin Hodgess erinm.hodg...@gmail.com wrote:
 
 Hello!
 
 I have a cbind type question, please:  Suppose I have the following:
 
 dog - 1:3
 cat - 2:4
 tree - 5:7
 
 and a character vector
 big.char - c(dog,cat,tree)
 
 I want to end up with a matrix that is a cbind of dog, cat, and tree.
 This is a toy example.  There will be a bunch of variables.
 
 I experimented with do.call, but all I got was
 1
 2
 3
 
 Any suggestions would be much appreciated.  I still think that do.call
 might be the key, but I'm not sure.
 
 R Version 3-1.3, Windows 7.
 
 Thanks,
 Erin
 


Hi Erin,

One approach could be:

 sapply(big.char, get, mode = integer)
 dog cat tree
[1,]   1   25
[2,]   2   36
[3,]   3   47

or

 sapply(big.char, get, mode = numeric)
 dog cat tree
[1,]   1   25
[2,]   2   36
[3,]   3   47


Note that I used the ‘mode' argument to get(). You used ‘cat’ as the name of 
one of the objects and of course, there is an R function cat(). By default for 
get(), mode = “any”, which would otherwise result in:

 sapply(big.char, get)
$dog
[1] 1 2 3

$cat
function (..., file = , sep =  , fill = FALSE, labels = NULL, 
append = FALSE) 
{
if (is.character(file)) 
if (file == ) 
file - stdout()
else if (substring(file, 1L, 1L) == |) {
file - pipe(substring(file, 2L), w)
on.exit(close(file))
}
else {
file - file(file, ifelse(append, a, w))
on.exit(close(file))
}
.Internal(cat(list(...), file, sep, fill, labels, append))
}
bytecode: 0x7fe942d78f78
environment: namespace:base

$tree
[1] 5 6 7


In the above, the cat() function body is returned, instead of the vector cat. 
So just need to be cautious.

An alternative approach, depending upon where your vectors are stored, might be:

 sapply(big.char, get, pos = 1)
 dog cat tree
[1,]   1   25
[2,]   2   36
[3,]   3   47


which specifies which environment to search for the named objects and the cat() 
function is not returned since it is in namespace:base.

See ?get

Regards,

Marc Schwartz

__
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] cbind question, please

2015-04-24 Thread Steve Taylor
This works for me...

get0 = function(x) get(x,pos=1)
sapply(big.char, get0)

The extra step seems necessary because without it, get() gets base::cat() 
instead of cat.

cheers,
Steve

-Original Message-
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Erin Hodgess
Sent: Friday, 24 April 2015 10:41a
To: R help
Subject: [R] cbind question, please

Hello!

I have a cbind type question, please:  Suppose I have the following:

dog - 1:3
cat - 2:4
tree - 5:7

and a character vector
big.char - c(dog,cat,tree)

I want to end up with a matrix that is a cbind of dog, cat, and tree.
This is a toy example.  There will be a bunch of variables.

I experimented with do.call, but all I got was
1
2
3

Any suggestions would be much appreciated.  I still think that do.call
might be the key, but I'm not sure.

R Version 3-1.3, Windows 7.

Thanks,
Erin


-- 
Erin Hodgess
Associate Professor
Department of Mathematical and Statistics
University of Houston - Downtown
mailto: erinm.hodg...@gmail.com

[[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] cbind question, please

2015-04-24 Thread Erin Hodgess
These are great! Thank you!



On Thu, Apr 23, 2015 at 7:14 PM, William Dunlap wdun...@tibco.com wrote:

 You could do something tricky like
do.call(cbind, lapply(big.char, as.name))
dog cat tree
   [1,]   1   25
   [2,]   2   36
   [3,]   3   47
 but you are usually better off creating these things as part of a list
 and passing that to do.call(cbind, list).

 There is a slight danger of using do.call with cbind.  If your
 list has a component with the unlikely name 'deparse.level',
 then that will be taken as cbind's deparse.level argument,
 not as a column of the matrix to be made.


 Bill Dunlap
 TIBCO Software
 wdunlap tibco.com

 On Thu, Apr 23, 2015 at 3:41 PM, Erin Hodgess erinm.hodg...@gmail.com
 wrote:

 Hello!

 I have a cbind type question, please:  Suppose I have the following:

 dog - 1:3
 cat - 2:4
 tree - 5:7

 and a character vector
 big.char - c(dog,cat,tree)

 I want to end up with a matrix that is a cbind of dog, cat, and tree.
 This is a toy example.  There will be a bunch of variables.

 I experimented with do.call, but all I got was
 1
 2
 3

 Any suggestions would be much appreciated.  I still think that do.call
 might be the key, but I'm not sure.

 R Version 3-1.3, Windows 7.

 Thanks,
 Erin


 --
 Erin Hodgess
 Associate Professor
 Department of Mathematical and Statistics
 University of Houston - Downtown
 mailto: erinm.hodg...@gmail.com

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





-- 
Erin Hodgess
Associate Professor
Department of Mathematical and Statistics
University of Houston - Downtown
mailto: erinm.hodg...@gmail.com

[[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] cbind question, please

2015-04-23 Thread Erin Hodgess
Hello!

I have a cbind type question, please:  Suppose I have the following:

dog - 1:3
cat - 2:4
tree - 5:7

and a character vector
big.char - c(dog,cat,tree)

I want to end up with a matrix that is a cbind of dog, cat, and tree.
This is a toy example.  There will be a bunch of variables.

I experimented with do.call, but all I got was
1
2
3

Any suggestions would be much appreciated.  I still think that do.call
might be the key, but I'm not sure.

R Version 3-1.3, Windows 7.

Thanks,
Erin


-- 
Erin Hodgess
Associate Professor
Department of Mathematical and Statistics
University of Houston - Downtown
mailto: erinm.hodg...@gmail.com

[[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] cbind in a loop...better way? | summary

2014-10-09 Thread Evan Cooch
Two solutions proposed -- not entirely orthogonal, but both do the 
trick. Instead of nesting cbin in a loop (as I did originally -- OP, 
below),


1\   do.call(cbind, lapply(mat_list, as.vector))

or

2\   sapply(mat_list,function(x) as.vector(x))


Both work fine. Thanks to Jeff Laake (2) + David Carlson (1) for their 
suggestions.



On 10/8/2014 3:12 PM, Evan Cooch wrote:
...or some such. I'm trying to work up a function wherein the user 
passes a list of matrices to the function, which then (1) takes each 
matrix, (2) performs an operation to 'vectorize' the matrix (i.e., 
given an (m x n) matrix x, this produces the vector Y of length  m*n 
that contains the columns of the matrix x, stacked below each other), 
and then (3) cbinds them together.


Here is an example using the case where I know how many matrices I 
need to cbind together. For this example, 2 square (3x3) matrices:


 a - matrix(c,0,20,50,0.05,0,0,0,0.1,0),3,3,byrow=T)
 b - matrix(c(0,15,45,0.15,0,0,0,0.2,0),3,3,byrow=T)

I want to vec them, and then cbind them together. So,

result  - cbind(matrix(a,nr=9), matrix(b,nr=9))

which yields the following:

  [,1]  [,2]
 [1,]  0.00  0.00
 [2,]  0.05  0.15
 [3,]  0.00  0.00
 [4,] 20.00 15.00
 [5,]  0.00  0.00
 [6,]  0.10  0.20
 [7,] 50.00 45.00
 [8,]  0.00  0.00
 [9,]  0.00  0.00

Easy enough. But, I want to put it in a function, where the number and 
dimensions  of the matrices is not specified. Something like


Using matrices (a) and (b) from above, let

  env - list(a,b).

Now, a function (or attempt at same) to perform the desired operations:

  vec=function(matlist) {

  n_mat=length(matlist);
  size_mat=dim(matlist[[1]])[1];

  result=cbind()

   for (i in 1:n_mat) {
 result=cbind(result,matrix(matlist[[i]],nr=size_mat^2))
  }

 return(result)

   }


When I run vec(env), I get the *right answer*, but I am wondering if 
there is a *better* way to get there from here than the approach I use 
(above). I'm not so much interested in 'computational efficiency' as I 
am in stability, and flexibility.


Thanks...

.



__
R-help@r-project.org mailing list
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] cbind in a loop...better way? | summary

2014-10-09 Thread David L Carlson
Actually Jeff Laake's can be made even shorter with

sapply(mat_list, as.vector)

David C

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Evan Cooch
Sent: Thursday, October 9, 2014 7:37 AM
To: Evan Cooch; r-help@r-project.org
Subject: Re: [R] cbind in a loop...better way? | summary

Two solutions proposed -- not entirely orthogonal, but both do the 
trick. Instead of nesting cbin in a loop (as I did originally -- OP, 
below),

1\   do.call(cbind, lapply(mat_list, as.vector))

or

2\   sapply(mat_list,function(x) as.vector(x))


Both work fine. Thanks to Jeff Laake (2) + David Carlson (1) for their 
suggestions.


On 10/8/2014 3:12 PM, Evan Cooch wrote:
 ...or some such. I'm trying to work up a function wherein the user 
 passes a list of matrices to the function, which then (1) takes each 
 matrix, (2) performs an operation to 'vectorize' the matrix (i.e., 
 given an (m x n) matrix x, this produces the vector Y of length  m*n 
 that contains the columns of the matrix x, stacked below each other), 
 and then (3) cbinds them together.

 Here is an example using the case where I know how many matrices I 
 need to cbind together. For this example, 2 square (3x3) matrices:

  a - matrix(c,0,20,50,0.05,0,0,0,0.1,0),3,3,byrow=T)
  b - matrix(c(0,15,45,0.15,0,0,0,0.2,0),3,3,byrow=T)

 I want to vec them, and then cbind them together. So,

 result  - cbind(matrix(a,nr=9), matrix(b,nr=9))

 which yields the following:

   [,1]  [,2]
  [1,]  0.00  0.00
  [2,]  0.05  0.15
  [3,]  0.00  0.00
  [4,] 20.00 15.00
  [5,]  0.00  0.00
  [6,]  0.10  0.20
  [7,] 50.00 45.00
  [8,]  0.00  0.00
  [9,]  0.00  0.00

 Easy enough. But, I want to put it in a function, where the number and 
 dimensions  of the matrices is not specified. Something like

 Using matrices (a) and (b) from above, let

   env - list(a,b).

 Now, a function (or attempt at same) to perform the desired operations:

   vec=function(matlist) {

   n_mat=length(matlist);
   size_mat=dim(matlist[[1]])[1];

   result=cbind()

for (i in 1:n_mat) {
  result=cbind(result,matrix(matlist[[i]],nr=size_mat^2))
   }

  return(result)

}


 When I run vec(env), I get the *right answer*, but I am wondering if 
 there is a *better* way to get there from here than the approach I use 
 (above). I'm not so much interested in 'computational efficiency' as I 
 am in stability, and flexibility.

 Thanks...

 .


__
R-help@r-project.org mailing list
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
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] cbind in a loop...better way? | summary

2014-10-09 Thread Wensui Liu
How about foreach() run in parallel?
On Oct 9, 2014 1:54 PM, David L Carlson dcarl...@tamu.edu wrote:

 Actually Jeff Laake's can be made even shorter with

 sapply(mat_list, as.vector)

 David C

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
 On Behalf Of Evan Cooch
 Sent: Thursday, October 9, 2014 7:37 AM
 To: Evan Cooch; r-help@r-project.org
 Subject: Re: [R] cbind in a loop...better way? | summary

 Two solutions proposed -- not entirely orthogonal, but both do the
 trick. Instead of nesting cbin in a loop (as I did originally -- OP,
 below),

 1\   do.call(cbind, lapply(mat_list, as.vector))

 or

 2\   sapply(mat_list,function(x) as.vector(x))


 Both work fine. Thanks to Jeff Laake (2) + David Carlson (1) for their
 suggestions.


 On 10/8/2014 3:12 PM, Evan Cooch wrote:
  ...or some such. I'm trying to work up a function wherein the user
  passes a list of matrices to the function, which then (1) takes each
  matrix, (2) performs an operation to 'vectorize' the matrix (i.e.,
  given an (m x n) matrix x, this produces the vector Y of length  m*n
  that contains the columns of the matrix x, stacked below each other),
  and then (3) cbinds them together.
 
  Here is an example using the case where I know how many matrices I
  need to cbind together. For this example, 2 square (3x3) matrices:
 
   a - matrix(c,0,20,50,0.05,0,0,0,0.1,0),3,3,byrow=T)
   b - matrix(c(0,15,45,0.15,0,0,0,0.2,0),3,3,byrow=T)
 
  I want to vec them, and then cbind them together. So,
 
  result  - cbind(matrix(a,nr=9), matrix(b,nr=9))
 
  which yields the following:
 
[,1]  [,2]
   [1,]  0.00  0.00
   [2,]  0.05  0.15
   [3,]  0.00  0.00
   [4,] 20.00 15.00
   [5,]  0.00  0.00
   [6,]  0.10  0.20
   [7,] 50.00 45.00
   [8,]  0.00  0.00
   [9,]  0.00  0.00
 
  Easy enough. But, I want to put it in a function, where the number and
  dimensions  of the matrices is not specified. Something like
 
  Using matrices (a) and (b) from above, let
 
env - list(a,b).
 
  Now, a function (or attempt at same) to perform the desired operations:
 
vec=function(matlist) {
 
n_mat=length(matlist);
size_mat=dim(matlist[[1]])[1];
 
result=cbind()
 
 for (i in 1:n_mat) {
   result=cbind(result,matrix(matlist[[i]],nr=size_mat^2))
}
 
   return(result)
 
 }
 
 
  When I run vec(env), I get the *right answer*, but I am wondering if
  there is a *better* way to get there from here than the approach I use
  (above). I'm not so much interested in 'computational efficiency' as I
  am in stability, and flexibility.
 
  Thanks...
 
  .
 

 __
 R-help@r-project.org mailing list
 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
 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
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] cbind in a loop...better way? | summary

2014-10-09 Thread Evan Cooch

Thanks!

On 10/9/2014 1:52 PM, David L Carlson wrote:

Actually Jeff Laake's can be made even shorter with

sapply(mat_list, as.vector)

David C

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Evan Cooch
Sent: Thursday, October 9, 2014 7:37 AM
To: Evan Cooch; r-help@r-project.org
Subject: Re: [R] cbind in a loop...better way? | summary

Two solutions proposed -- not entirely orthogonal, but both do the
trick. Instead of nesting cbin in a loop (as I did originally -- OP,
below),

1\   do.call(cbind, lapply(mat_list, as.vector))

or

2\   sapply(mat_list,function(x) as.vector(x))


Both work fine. Thanks to Jeff Laake (2) + David Carlson (1) for their
suggestions.


On 10/8/2014 3:12 PM, Evan Cooch wrote:

...or some such. I'm trying to work up a function wherein the user
passes a list of matrices to the function, which then (1) takes each
matrix, (2) performs an operation to 'vectorize' the matrix (i.e.,
given an (m x n) matrix x, this produces the vector Y of length  m*n
that contains the columns of the matrix x, stacked below each other),
and then (3) cbinds them together.

Here is an example using the case where I know how many matrices I
need to cbind together. For this example, 2 square (3x3) matrices:

  a - matrix(c,0,20,50,0.05,0,0,0,0.1,0),3,3,byrow=T)
  b - matrix(c(0,15,45,0.15,0,0,0,0.2,0),3,3,byrow=T)

I want to vec them, and then cbind them together. So,

result  - cbind(matrix(a,nr=9), matrix(b,nr=9))

which yields the following:

   [,1]  [,2]
  [1,]  0.00  0.00
  [2,]  0.05  0.15
  [3,]  0.00  0.00
  [4,] 20.00 15.00
  [5,]  0.00  0.00
  [6,]  0.10  0.20
  [7,] 50.00 45.00
  [8,]  0.00  0.00
  [9,]  0.00  0.00

Easy enough. But, I want to put it in a function, where the number and
dimensions  of the matrices is not specified. Something like

Using matrices (a) and (b) from above, let

   env - list(a,b).

Now, a function (or attempt at same) to perform the desired operations:

   vec=function(matlist) {

   n_mat=length(matlist);
   size_mat=dim(matlist[[1]])[1];

   result=cbind()

for (i in 1:n_mat) {
  result=cbind(result,matrix(matlist[[i]],nr=size_mat^2))
   }

  return(result)

}


When I run vec(env), I get the *right answer*, but I am wondering if
there is a *better* way to get there from here than the approach I use
(above). I'm not so much interested in 'computational efficiency' as I
am in stability, and flexibility.

Thanks...

.


__
R-help@r-project.org mailing list
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
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] cbind in a loop...better way?

2014-10-08 Thread Evan Cooch
...or some such. I'm trying to work up a function wherein the user 
passes a list of matrices to the function, which then (1) takes each 
matrix, (2) performs an operation to 'vectorize' the matrix (i.e., given 
an (m x n) matrix x, this produces the vector Y of length  m*n that 
contains the columns of the matrix x, stacked below each other), and 
then (3) cbinds them together.


Here is an example using the case where I know how many matrices I need 
to cbind together. For this example, 2 square (3x3) matrices:


 a - matrix(c,0,20,50,0.05,0,0,0,0.1,0),3,3,byrow=T)
 b - matrix(c(0,15,45,0.15,0,0,0,0.2,0),3,3,byrow=T)

I want to vec them, and then cbind them together. So,

result  - cbind(matrix(a,nr=9), matrix(b,nr=9))

which yields the following:

  [,1]  [,2]
 [1,]  0.00  0.00
 [2,]  0.05  0.15
 [3,]  0.00  0.00
 [4,] 20.00 15.00
 [5,]  0.00  0.00
 [6,]  0.10  0.20
 [7,] 50.00 45.00
 [8,]  0.00  0.00
 [9,]  0.00  0.00

Easy enough. But, I want to put it in a function, where the number and 
dimensions  of the matrices is not specified. Something like


Using matrices (a) and (b) from above, let

  env - list(a,b).

Now, a function (or attempt at same) to perform the desired operations:

  vec=function(matlist) {

  n_mat=length(matlist);
  size_mat=dim(matlist[[1]])[1];

  result=cbind()

   for (i in 1:n_mat) {
 result=cbind(result,matrix(matlist[[i]],nr=size_mat^2))
  }

 return(result)

   }


When I run vec(env), I get the *right answer*, but I am wondering if 
there is a *better* way to get there from here than the approach I use 
(above). I'm not so much interested in 'computational efficiency' as I 
am in stability, and flexibility.


Thanks...

__
R-help@r-project.org mailing list
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] cbind in a loop...better way?

2014-10-08 Thread David L Carlson
How about

 do.call(cbind, lapply(env, as.vector))
   [,1]  [,2]
 [1,]  0.00  0.00
 [2,]  0.05  0.15
 [3,]  0.00  0.00
 [4,] 20.00 15.00
 [5,]  0.00  0.00
 [6,]  0.10  0.20
 [7,] 50.00 45.00
 [8,]  0.00  0.00
 [9,]  0.00  0.00

-
David L Carlson
Department of Anthropology
Texas AM University
College Station, TX 77840-4352

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Evan Cooch
Sent: Wednesday, October 8, 2014 2:13 PM
To: r-help@r-project.org
Subject: [R] cbind in a loop...better way?

...or some such. I'm trying to work up a function wherein the user 
passes a list of matrices to the function, which then (1) takes each 
matrix, (2) performs an operation to 'vectorize' the matrix (i.e., given 
an (m x n) matrix x, this produces the vector Y of length  m*n that 
contains the columns of the matrix x, stacked below each other), and 
then (3) cbinds them together.

Here is an example using the case where I know how many matrices I need 
to cbind together. For this example, 2 square (3x3) matrices:

  a - matrix(c,0,20,50,0.05,0,0,0,0.1,0),3,3,byrow=T)
  b - matrix(c(0,15,45,0.15,0,0,0,0.2,0),3,3,byrow=T)

I want to vec them, and then cbind them together. So,

result  - cbind(matrix(a,nr=9), matrix(b,nr=9))

which yields the following:

   [,1]  [,2]
  [1,]  0.00  0.00
  [2,]  0.05  0.15
  [3,]  0.00  0.00
  [4,] 20.00 15.00
  [5,]  0.00  0.00
  [6,]  0.10  0.20
  [7,] 50.00 45.00
  [8,]  0.00  0.00
  [9,]  0.00  0.00

Easy enough. But, I want to put it in a function, where the number and 
dimensions  of the matrices is not specified. Something like

Using matrices (a) and (b) from above, let

   env - list(a,b).

Now, a function (or attempt at same) to perform the desired operations:

   vec=function(matlist) {

   n_mat=length(matlist);
   size_mat=dim(matlist[[1]])[1];

   result=cbind()

for (i in 1:n_mat) {
  result=cbind(result,matrix(matlist[[i]],nr=size_mat^2))
   }

  return(result)

}


When I run vec(env), I get the *right answer*, but I am wondering if 
there is a *better* way to get there from here than the approach I use 
(above). I'm not so much interested in 'computational efficiency' as I 
am in stability, and flexibility.

Thanks...

__
R-help@r-project.org mailing list
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
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] cbind in a loop...better way?

2014-10-08 Thread Evan Cooch
That works as well. I'll collate your response and a couple of others, 
and post tomorrow.

On 10/8/2014 4:17 PM, David L Carlson wrote:
 How about

 do.call(cbind, lapply(env, as.vector))
 [,1]  [,2]
   [1,]  0.00  0.00
   [2,]  0.05  0.15
   [3,]  0.00  0.00
   [4,] 20.00 15.00
   [5,]  0.00  0.00
   [6,]  0.10  0.20
   [7,] 50.00 45.00
   [8,]  0.00  0.00
   [9,]  0.00  0.00

 -
 David L Carlson
 Department of Anthropology
 Texas AM University
 College Station, TX 77840-4352

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
 Behalf Of Evan Cooch
 Sent: Wednesday, October 8, 2014 2:13 PM
 To: r-help@r-project.org
 Subject: [R] cbind in a loop...better way?

 ...or some such. I'm trying to work up a function wherein the user
 passes a list of matrices to the function, which then (1) takes each
 matrix, (2) performs an operation to 'vectorize' the matrix (i.e., given
 an (m x n) matrix x, this produces the vector Y of length  m*n that
 contains the columns of the matrix x, stacked below each other), and
 then (3) cbinds them together.

 Here is an example using the case where I know how many matrices I need
 to cbind together. For this example, 2 square (3x3) matrices:

a - matrix(c,0,20,50,0.05,0,0,0,0.1,0),3,3,byrow=T)
b - matrix(c(0,15,45,0.15,0,0,0,0.2,0),3,3,byrow=T)

 I want to vec them, and then cbind them together. So,

 result  - cbind(matrix(a,nr=9), matrix(b,nr=9))

 which yields the following:

 [,1]  [,2]
[1,]  0.00  0.00
[2,]  0.05  0.15
[3,]  0.00  0.00
[4,] 20.00 15.00
[5,]  0.00  0.00
[6,]  0.10  0.20
[7,] 50.00 45.00
[8,]  0.00  0.00
[9,]  0.00  0.00

 Easy enough. But, I want to put it in a function, where the number and
 dimensions  of the matrices is not specified. Something like

 Using matrices (a) and (b) from above, let

 env - list(a,b).

 Now, a function (or attempt at same) to perform the desired operations:

 vec=function(matlist) {

 n_mat=length(matlist);
 size_mat=dim(matlist[[1]])[1];

 result=cbind()

  for (i in 1:n_mat) {
result=cbind(result,matrix(matlist[[i]],nr=size_mat^2))
 }

return(result)

  }


 When I run vec(env), I get the *right answer*, but I am wondering if
 there is a *better* way to get there from here than the approach I use
 (above). I'm not so much interested in 'computational efficiency' as I
 am in stability, and flexibility.

 Thanks...

 __
 R-help@r-project.org mailing list
 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
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] cbind with row names to serveral files in R

2014-04-19 Thread arun
Hi,
The rownames part is not clear as your expected output and input files didn't 
show them as rownames. 


##Suppose you have all the files in a folder
##here I am creating the names of those files

files1 - paste0(sample, rep(1:777, each=29),chr,1:29,.txt)
length(files1)
#[1] 22533
lst1 -  split(files1,as.numeric(gsub(.*chr(\\d+)\\..*,\\1,files1)))

##use list.files() 

##in your case, ##not tested

lst1 - split(list.files(pattern=sample\\d+), 
as.numeric(gsub(.*chr(\\d+)\\..*,\\1,list.files(pattern=sample\\d+ 
##in case other files are also in the folder

library(plyr)

lst2 - lapply(lst1,function(x) {x1 -join_all(lapply(x,function(y) 
read.table(y,header=TRUE,stringsAsFactors=FALSE,sep=)),c(Name,Chr,Position))
 })


lapply(seq_along(lst2),function(i) 
write.table(lst2[[i]],paste0(LRRrawallchr,i,.txt),row.names=FALSE,quote=FALSE))


###if you need to create rownames using the first three columns:
lapply(seq_along(lst2),function(i) {x1 - lst2[[i]]; row.names(x1) - 
as.character(interaction(x1[,1:3],sep=_)); x2 - x1[,-(1:3)]; write.table(x2, 
paste0(LRRrawallchr,i,.txt), quote=FALSE)})



A.K.


I would like to use the `cbind` in a list of files. However each file are 
splited in a specific chromosome (chr) `(k in 1:29)`, and specific sample `(i 
in 1:777)`. The files are like:


sample1chr1.txt, sample1chr2.txt ... sample1chr29.txt, sample2chr1.txt ... 
sample777chr29.txt

All files have exactly the same rows names (3 first collumns represent my row 
names). I would like to get a final file to each chr merging to all sample 
files, with and do not repeat the row names in the final file (the first 3 
collumns representing my rows names).

I tried this:

`#Creating file with row names (3 first collumns) to each Chr
{
{for(k in 1:29){
  infile - paste0(sample1chr,k,.txt)
  outfile - paste0(LRRrawallchr,k,.txt)
  rows - read.table(infile, header=TRUE, sep=\t)
  rows - rows[, -grep(Log.R.Ratio, colnames(rows))]
  write.table(rows, outfile, sep=;)}}`

`#Cbind in one file per Chr
{  for(i in 1:777)
  for(k in 1:29){
    base - paste0(LRRrawallchr,k,.txt)
    chr - read.table(base, header=TRUE, sep=;)
    infile - paste0(sample,i,chr,k,.txt)
    chr2 - read.table(infile, header=TRUE, sep=\t)
    outfile - paste0(LRRrawallchr,k,.txt)
    chr2 - chr2[, -grep(Name, colnames(chr2))]
    chr2 - chr2[, -grep(Chr, colnames(chr2))]
    chr2 - chr2[, -grep(Position, colnames(chr2))]
    chr - cbind(chr, chr2)
    write.table(chr, outfile, sep=;, row.names=FALSE, col.names=FALSE)}
}`

Input example (sample1chr1.txt):

   

 Name   Chr Position sample1value
    BAC-11034 1 128   0.302
    BAC-11044 1 129   -0.56
    BAC-11057 1 134   0.0840

Input example (sample2chr1.txt):

    Name   Chr Position  sample2value
    BAC-11034 1 128   0.25
    BAC-11044 1 129   0.41
    BAC-11057 1 134  -0.14

Expected output (LRRrawallchr1):

    Name   Chr Position    sample1value   sample2value
    BAC-11034 1 128    0.302  0.25
    BAC-11044 1 129    -0.56  0.41
    BAC-11057 1 134    0.0840 -0.14

I have 22553 diferent .txt files (29 files (one per chr) to each of 777 
samples). All 22553 files (sample1chr1.txt, sample1chr2.txt ... 
sample1chr29.txt, sample2chr1.txt ... sample777chr29.txt) are like above 
example.  

I wanna 29 files like (LRRrawallchr1), one per Chr. The LRRrawallchr,k, files 
have to be with 777+3 (800 collumns). The 3 row names and one collumn per 
sample.

Cheers! 


__
R-help@r-project.org mailing list
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] cbind() function : Not able to display columns

2013-10-22 Thread Vivek Singh
Hi All,


I have create a matrix using cbind() function as follows:


 a=c(1,2,3)

 b=c('a','b','c')

 c=c(ee,tt,rr)


 k=cbind(a,b,c)


Problem: when we print the matrix k,

 k

a   b   c

[1,] 1 a ee

[2,] 2 b tt

[3,] 3 c rr

we can see that rows are represented by [1,] , [2,] and [3,]. Similarly,
the columns are denoted by [a], [b] and [c]. When we try to print the
corresponding columns, we are able to print for k[a], i.e., the first
column but not able to correctly print the second and third columns.

 k[a]

[1] 1 2 3

 k[b]

[1] NA NA NA

 k[c]

[1] NA NA NA

Please let me know what am I doing wrong.

-- 
Thanks and Regards,

Vivek Kumar Singh

Research Assistant,
School of Computing,
National University of Singapore
Mobile:(0065) 82721535

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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] cbind() function : Not able to display columns

2013-10-22 Thread arun
Hi,

Try:

  k[,a]
#[1] 1 2 3
 k[,b]
#[1] a b c
 k[,c]
#[1] ee tt rr
A.K.






On Tuesday, October 22, 2013 11:37 PM, Vivek Singh vksingh.ii...@gmail.com 
wrote:
Hi All,


I have create a matrix using cbind() function as follows:


 a=c(1,2,3)

 b=c('a','b','c')

 c=c(ee,tt,rr)


 k=cbind(a,b,c)


Problem: when we print the matrix k,

 k

    a   b   c

[1,] 1 a ee

[2,] 2 b tt

[3,] 3 c rr

we can see that rows are represented by [1,] , [2,] and [3,]. Similarly,
the columns are denoted by [a], [b] and [c]. When we try to print the
corresponding columns, we are able to print for k[a], i.e., the first
column but not able to correctly print the second and third columns.

 k[a]

[1] 1 2 3

 k[b]

[1] NA NA NA

 k[c]

[1] NA NA NA

Please let me know what am I doing wrong.

-- 
Thanks and Regards,

Vivek Kumar Singh

Research Assistant,
School of Computing,
National University of Singapore
Mobile:(0065) 82721535

    [[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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
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] cbind() function : Not able to display columns

2013-10-22 Thread Jeff Newmiller
Hard to say, not sure what you want to do. But the columns are not denoted by 
[a], [b] or [c]. You should learn to use the str function to understand what 
various expressions really are, and return to the Introduction to R document 
that comes with the software. There is a distinct difference between a and a 
in R, and square brackets are not at all like quotes. See help([) and the 
ItoR section on indexing.

You might get what you want by k[,b] for example.
---
Jeff NewmillerThe .   .  Go Live...
DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

Vivek Singh vksingh.ii...@gmail.com wrote:
Hi All,


I have create a matrix using cbind() function as follows:


 a=c(1,2,3)

 b=c('a','b','c')

 c=c(ee,tt,rr)


 k=cbind(a,b,c)


Problem: when we print the matrix k,

 k

a   b   c

[1,] 1 a ee

[2,] 2 b tt

[3,] 3 c rr

we can see that rows are represented by [1,] , [2,] and [3,].
Similarly,
the columns are denoted by [a], [b] and [c]. When we try to print the
corresponding columns, we are able to print for k[a], i.e., the first
column but not able to correctly print the second and third columns.

 k[a]

[1] 1 2 3

 k[b]

[1] NA NA NA

 k[c]

[1] NA NA NA

Please let me know what am I doing wrong.

__
R-help@r-project.org mailing list
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] cbind() function : Not able to display columns

2013-10-22 Thread Richard M. Heiberger
In addition to what the others have told you, it looks like you might
be confusing
matrices with data.frames.  Please see
?data.frame

I think what you are looking for is

 b - c('a','b','c')
 c - c(ee,tt,rr)
 k - cbind(a,b,c)
 K - data.frame(a, b, c)
 K
  a b  c
1 1 a ee
2 2 b tt
3 3 c rr

I recommend using  -  for assignment (not the spaces on both sides), not =.

On Wed, Oct 23, 2013 at 12:01 AM, Jeff Newmiller
jdnew...@dcn.davis.ca.us wrote:
 Hard to say, not sure what you want to do. But the columns are not denoted by 
 [a], [b] or [c]. You should learn to use the str function to understand what 
 various expressions really are, and return to the Introduction to R 
 document that comes with the software. There is a distinct difference between 
 a and a in R, and square brackets are not at all like quotes. See help([) 
 and the ItoR section on indexing.

 You might get what you want by k[,b] for example.
 ---
 Jeff NewmillerThe .   .  Go Live...
 DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live Go...
   Live:   OO#.. Dead: OO#..  Playing
 Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
 /Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
 ---
 Sent from my phone. Please excuse my brevity.

 Vivek Singh vksingh.ii...@gmail.com wrote:
Hi All,


I have create a matrix using cbind() function as follows:


 a=c(1,2,3)

 b=c('a','b','c')

 c=c(ee,tt,rr)


 k=cbind(a,b,c)


Problem: when we print the matrix k,

 k

a   b   c

[1,] 1 a ee

[2,] 2 b tt

[3,] 3 c rr

we can see that rows are represented by [1,] , [2,] and [3,].
Similarly,
the columns are denoted by [a], [b] and [c]. When we try to print the
corresponding columns, we are able to print for k[a], i.e., the first
column but not able to correctly print the second and third columns.

 k[a]

[1] 1 2 3

 k[b]

[1] NA NA NA

 k[c]

[1] NA NA NA

Please let me know what am I doing wrong.

 __
 R-help@r-project.org mailing list
 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
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] cbind with headers

2013-08-08 Thread arun
Hi,

You can save it in file.  I copy and paste:

Subtype,Gender,Expression
A,m,-0.54
A,f,-0.8
B,f,-1.03
C,m,-0.41


on the gedit and save it as data1.csv.  You might be able to do the same 
with notepad.

x - read.csv(data1.csv,header=T,sep=,)
x2 - read.csv(data2N.csv,header=T,sep=,)
x3 - cbind(x,x2)

 x3
#  Subtype Gender Expression Age City
#1   A  m  -0.54  32 New York
#2   A  f  -0.80  21  Houston
#3   B  f  -1.03  34  Seattle
#4   C  m  -0.41  67  Houston



#or if the dataset is small as in the example

x- read.table(text=
Subtype,Gender,Expression
A,m,-0.54
A,f,-0.8
B,f,-1.03
C,m,-0.41
,sep=,,header=TRUE,stringsAsFactors=FALSE)

x2- read.table(text=
Age,City
32,New York
21,Houston
34,Seattle
67,Houston
,sep=,,header=TRUE,stringsAsFactors=FALSE)
cbind(x,x2)
#  Subtype Gender Expression Age City
#1   A  m  -0.54  32 New York
#2   A  f  -0.80  21  Houston
#3   B  f  -1.03  34  Seattle
#4   C  m  -0.41  67  Houston
A.K.




Hi, 

I can't seem to get this to work: 
http://www.endmemo.com/program/R/cbind.php

Do
 I save the data as data1.csv in note pad and pull in the file? Do I 
type 
data1.csv-Subtype,Gender,Expression,A,m,-0.54,A,f,-0.8,B,f,-1.03,C,m,-0.41?? 

I can do a simple matrix. But, I want to have headers and data to combine. 

Simple Matrix I combined. 
 m-as.data.frame(matrix(c(1:6),ncol=2)) 
 n-as.data.frame(matrix(c(7:12),ncol=2)) 
 m 
  V1 V2 
1  1  4 
2  2  5 
3  3  6 
 n 
  V1 V2 
1  7 10 
2  8 11 
3  9 12 
 Mary-cbind(m,n) 
 Mary 
  V1 V2 V1 V2 
1  1  4  7 10 
2  2  5  8 11 
3  3  6  9 12

__
R-help@r-project.org mailing list
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] cbind with headers

2013-08-08 Thread Docbanks84
Hi,

I can't seem to get this to work:
http://www.endmemo.com/program/R/cbind.php

Do I save the data as data1.csv in note pad and pull in the file? Do I type
data1.csv-Subtype,Gender,Expression,A,m,-0.54,A,f,-0.8,B,f,-1.03,C,m,-0.41??

I can do a simple matrix. But, I want to have headers and data to combine.

Simple Matrix I combined.
 m-as.data.frame(matrix(c(1:6),ncol=2))
 n-as.data.frame(matrix(c(7:12),ncol=2))
 m
  V1 V2
1  1  4
2  2  5
3  3  6
 n
  V1 V2
1  7 10
2  8 11
3  9 12
 Mary-cbind(m,n)
 Mary
  V1 V2 V1 V2
1  1  4  7 10
2  2  5  8 11
3  3  6  9 12




--
View this message in context: 
http://r.789695.n4.nabble.com/cbind-with-headers-tp4673354.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
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] cbind error with check.names

2013-07-23 Thread Fg Nu


Hello all,

I posted a question about cbind and an error when specifying the check.names 
argument here:
http://stackoverflow.com/questions/17810470/cbind-error-with-check-names


I was advised to take this to R-devel, but before that, I'd like to get the 
opinions of people on this list.

Thanks

__
R-help@r-project.org mailing list
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] cbind for list of zoo objects

2013-04-09 Thread Harry Mamaysky
Thanks for the explanations. 

Wouldn't the following bit of checking in do.call() make it easier to figure 
such things out in the future?


my.call - function(what,args,...) {
 
  ##  Get the name of the function to call.
  if (!is.character(what))
  whatStr - deparse(substitute(what))
  else
  whatStr - what
 
  callFctn - paste0(gsub(paste0('.',class(args[[1]])),'',whatStr), '.', 
class(args[[1]]))
 
  ##  Check whether list names are arguments to the function.
  if ( any( names(args) %in% names(formals(callFctn)) ) )
  warning( 'Element of args is also an argument for ', callFctn, '.' )
 
  ##  Do the actual call.
  do.call( what, args, ... )
}

Sent from my iPhone

On Apr 8, 2013, at 7:23 PM, Gabor Grothendieck ggrothendi...@gmail.com wrote:

On Mon, Apr 8, 2013 at 3:54 PM, Harry Mamaysky h.mamay...@gmail.com wrote:
 Can someone explain why this happens when one of the list elements is named 
 'all'?
 
 zz - list( zoo(1:10,1:10), zoo(101:110,1:10), zoo(201:210,1:10) )
 names(zz)-c('test','bar','foo')
 do.call(cbind,zz)
   test bar foo
 1 1 101 201
 2 2 102 202
 3 3 103 203
 4 4 104 204
 5 5 105 205
 6 6 106 206
 7 7 107 207
 8 8 108 208
 9 9 109 209
 10   10 110 210
 names(zz)-c('test','all','foo')
 do.call(cbind,zz)
   test foo
 1 1 201
 2 2 202
 3 3 203
 4 4 204
 5 5 205
 6 6 206
 7 7 207
 8 8 208
 9 9 209
 10   10 210

all= is an argument to cbind.zoo so it cannot be used as a column name.

 args(zoo:::cbind.zoo)
function (..., all = TRUE, fill = NA, suffixes = NULL, drop = FALSE)
NULL

--
Statistics  Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
R-help@r-project.org mailing list
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] cbind for list of zoo objects

2013-04-09 Thread Joshua Ulrich
On Tue, Apr 9, 2013 at 7:31 AM, Harry Mamaysky h.mamay...@gmail.com wrote:
 Thanks for the explanations.

 Wouldn't the following bit of checking in do.call() make it easier to figure 
 such things out in the future?

Sure, it would have helped you figure out your issue, but you don't
want a *warning* when you purposefully try to set named function
arguments.  I.e., what if you want to set a function argument to
something other than its default value?  Do you really want a warning
when you do that?


 my.call - function(what,args,...) {

   ##  Get the name of the function to call.
   if (!is.character(what))
   whatStr - deparse(substitute(what))
   else
   whatStr - what

   callFctn - paste0(gsub(paste0('.',class(args[[1]])),'',whatStr), '.', 
 class(args[[1]]))

   ##  Check whether list names are arguments to the function.
   if ( any( names(args) %in% names(formals(callFctn)) ) )
   warning( 'Element of args is also an argument for ', callFctn, '.' )

   ##  Do the actual call.
   do.call( what, args, ... )
 }

 Sent from my iPhone

 On Apr 8, 2013, at 7:23 PM, Gabor Grothendieck ggrothendi...@gmail.com 
 wrote:

 On Mon, Apr 8, 2013 at 3:54 PM, Harry Mamaysky h.mamay...@gmail.com wrote:
 Can someone explain why this happens when one of the list elements is named 
 'all'?

 zz - list( zoo(1:10,1:10), zoo(101:110,1:10), zoo(201:210,1:10) )
 names(zz)-c('test','bar','foo')
 do.call(cbind,zz)
   test bar foo
 1 1 101 201
 2 2 102 202
 3 3 103 203
 4 4 104 204
 5 5 105 205
 6 6 106 206
 7 7 107 207
 8 8 108 208
 9 9 109 209
 10   10 110 210
 names(zz)-c('test','all','foo')
 do.call(cbind,zz)
   test foo
 1 1 201
 2 2 202
 3 3 203
 4 4 204
 5 5 205
 6 6 206
 7 7 207
 8 8 208
 9 9 209
 10   10 210

 all= is an argument to cbind.zoo so it cannot be used as a column name.

 args(zoo:::cbind.zoo)
 function (..., all = TRUE, fill = NA, suffixes = NULL, drop = FALSE)
 NULL

 --
 Statistics  Software Consulting
 GKX Group, GKX Associates Inc.
 tel: 1-877-GKX-GROUP
 email: ggrothendieck at gmail.com


--
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com

R/Finance 2013: Applied Finance with R  | www.RinFinance.com

__
R-help@r-project.org mailing list
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] cbind for list of zoo objects

2013-04-09 Thread Harry Mamaysky
That's true. So perhaps there should be a flag that turns on this error 
checking. Often args is just a list that gets generated automatically and you 
don't know what all of its elements are. It just leads to a bit of 
non-deterministic behavior. It would be useful to have the option of flagging 
when one of those list elements (inadvertently) has the same name as an 
argument of what. 

Sent from my iPhone

On Apr 9, 2013, at 9:07 AM, Joshua Ulrich josh.m.ulr...@gmail.com wrote:

On Tue, Apr 9, 2013 at 7:31 AM, Harry Mamaysky h.mamay...@gmail.com wrote:
 Thanks for the explanations.
 
 Wouldn't the following bit of checking in do.call() make it easier to figure 
 such things out in the future?
Sure, it would have helped you figure out your issue, but you don't
want a *warning* when you purposefully try to set named function
arguments.  I.e., what if you want to set a function argument to
something other than its default value?  Do you really want a warning
when you do that?

 
 my.call - function(what,args,...) {
 
  ##  Get the name of the function to call.
  if (!is.character(what))
  whatStr - deparse(substitute(what))
  else
  whatStr - what
 
  callFctn - paste0(gsub(paste0('.',class(args[[1]])),'',whatStr), '.', 
 class(args[[1]]))
 
  ##  Check whether list names are arguments to the function.
  if ( any( names(args) %in% names(formals(callFctn)) ) )
  warning( 'Element of args is also an argument for ', callFctn, '.' )
 
  ##  Do the actual call.
  do.call( what, args, ... )
 }
 
 Sent from my iPhone
 
 On Apr 8, 2013, at 7:23 PM, Gabor Grothendieck ggrothendi...@gmail.com 
 wrote:
 
 On Mon, Apr 8, 2013 at 3:54 PM, Harry Mamaysky h.mamay...@gmail.com wrote:
 Can someone explain why this happens when one of the list elements is named 
 'all'?
 
 zz - list( zoo(1:10,1:10), zoo(101:110,1:10), zoo(201:210,1:10) )
 names(zz)-c('test','bar','foo')
 do.call(cbind,zz)
  test bar foo
 1 1 101 201
 2 2 102 202
 3 3 103 203
 4 4 104 204
 5 5 105 205
 6 6 106 206
 7 7 107 207
 8 8 108 208
 9 9 109 209
 10   10 110 210
 names(zz)-c('test','all','foo')
 do.call(cbind,zz)
  test foo
 1 1 201
 2 2 202
 3 3 203
 4 4 204
 5 5 205
 6 6 206
 7 7 207
 8 8 208
 9 9 209
 10   10 210
 
 all= is an argument to cbind.zoo so it cannot be used as a column name.
 
 args(zoo:::cbind.zoo)
 function (..., all = TRUE, fill = NA, suffixes = NULL, drop = FALSE)
 NULL
 
 --
 Statistics  Software Consulting
 GKX Group, GKX Associates Inc.
 tel: 1-877-GKX-GROUP
 email: ggrothendieck at gmail.com

--
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com

R/Finance 2013: Applied Finance with R  | www.RinFinance.com

__
R-help@r-project.org mailing list
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] cbind for list of zoo objects

2013-04-09 Thread Gabor Grothendieck
On Tue, Apr 9, 2013 at 9:15 AM, Harry Mamaysky h.mamay...@gmail.com wrote:
 That's true. So perhaps there should be a flag that turns on this error 
 checking. Often args is just a list that gets generated automatically and 
 you don't know what all of its elements are. It just leads to a bit of 
 non-deterministic behavior. It would be useful to have the option of flagging 
 when one of those list elements (inadvertently) has the same name as an 
 argument of what.


You can force an error by specifying all the default arguments.  Using
the version of zz with an all component it will encounter all= twice:

 do.call(cbind, c(zz, all = TRUE, fill = NA, suffixes = list(NULL), drop = 
 FALSE))
Error in cbind(deparse.level, ...) :
  formal argument all matched by multiple actual arguments

or more compactly:

 do.call(cbind, c(zz, formals(zoo:::cbind.zoo)[-1]))
Error in cbind(deparse.level, ...) :
  formal argument all matched by multiple actual arguments


--
Statistics  Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
R-help@r-project.org mailing list
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] cbind for list of zoo objects

2013-04-09 Thread Harry Mamaysky
That's a nice solution. Thanks. 

Sent from my iPhone

On Apr 9, 2013, at 10:00 AM, Gabor Grothendieck ggrothendi...@gmail.com wrote:

On Tue, Apr 9, 2013 at 9:15 AM, Harry Mamaysky h.mamay...@gmail.com wrote:
 That's true. So perhaps there should be a flag that turns on this error 
 checking. Often args is just a list that gets generated automatically and 
 you don't know what all of its elements are. It just leads to a bit of 
 non-deterministic behavior. It would be useful to have the option of flagging 
 when one of those list elements (inadvertently) has the same name as an 
 argument of what.
 

You can force an error by specifying all the default arguments.  Using
the version of zz with an all component it will encounter all= twice:

 do.call(cbind, c(zz, all = TRUE, fill = NA, suffixes = list(NULL), drop = 
 FALSE))
Error in cbind(deparse.level, ...) :
 formal argument all matched by multiple actual arguments

or more compactly:

 do.call(cbind, c(zz, formals(zoo:::cbind.zoo)[-1]))
Error in cbind(deparse.level, ...) :
 formal argument all matched by multiple actual arguments


--
Statistics  Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
R-help@r-project.org mailing list
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] cbind for list of zoo objects

2013-04-08 Thread Harry Mamaysky
 Can someone explain why this happens when one of the list elements is named 
'all'?

 zz - list( zoo(1:10,1:10), zoo(101:110,1:10), zoo(201:210,1:10) )
 names(zz)-c('test','bar','foo')
 do.call(cbind,zz)
   test bar foo
1 1 101 201
2 2 102 202
3 3 103 203
4 4 104 204
5 5 105 205
6 6 106 206
7 7 107 207
8 8 108 208
9 9 109 209
10   10 110 210
 names(zz)-c('test','all','foo')
 do.call(cbind,zz)
   test foo
1 1 201
2 2 202
3 3 203
4 4 204
5 5 205
6 6 206
7 7 207
8 8 208
9 9 209
10   10 210
 
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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] cbind for list of zoo objects

2013-04-08 Thread Joshua Ulrich
Because 'all' is the name of one of the arguments to cbind.zoo:

R args(cbind.zoo)
function (..., all = TRUE, fill = NA, suffixes = NULL, drop = FALSE)
NULL

do.call constructs a call somewhat like:
R cbind(test=zz$test, all=zz$all, foo=zz$foo)

The same thing would happen for list elements named 'fill',
'suffixes', or 'drop'.

Best,
--
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com

R/Finance 2013: Applied Finance with R  | www.RinFinance.com


On Mon, Apr 8, 2013 at 2:54 PM, Harry Mamaysky h.mamay...@gmail.com wrote:
  Can someone explain why this happens when one of the list elements is named 
 'all'?

 zz - list( zoo(1:10,1:10), zoo(101:110,1:10), zoo(201:210,1:10) )
 names(zz)-c('test','bar','foo')
 do.call(cbind,zz)
test bar foo
 1 1 101 201
 2 2 102 202
 3 3 103 203
 4 4 104 204
 5 5 105 205
 6 6 106 206
 7 7 107 207
 8 8 108 208
 9 9 109 209
 10   10 110 210
 names(zz)-c('test','all','foo')
 do.call(cbind,zz)
test foo
 1 1 201
 2 2 202
 3 3 203
 4 4 204
 5 5 205
 6 6 206
 7 7 207
 8 8 208
 9 9 209
 10   10 210

 [[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list
 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
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] cbind for list of zoo objects

2013-04-08 Thread Gabor Grothendieck
On Mon, Apr 8, 2013 at 3:54 PM, Harry Mamaysky h.mamay...@gmail.com wrote:
  Can someone explain why this happens when one of the list elements is named 
 'all'?

 zz - list( zoo(1:10,1:10), zoo(101:110,1:10), zoo(201:210,1:10) )
 names(zz)-c('test','bar','foo')
 do.call(cbind,zz)
test bar foo
 1 1 101 201
 2 2 102 202
 3 3 103 203
 4 4 104 204
 5 5 105 205
 6 6 106 206
 7 7 107 207
 8 8 108 208
 9 9 109 209
 10   10 110 210
 names(zz)-c('test','all','foo')
 do.call(cbind,zz)
test foo
 1 1 201
 2 2 202
 3 3 203
 4 4 204
 5 5 205
 6 6 206
 7 7 207
 8 8 208
 9 9 209
 10   10 210

all= is an argument to cbind.zoo so it cannot be used as a column name.

 args(zoo:::cbind.zoo)
function (..., all = TRUE, fill = NA, suffixes = NULL, drop = FALSE)
NULL

--
Statistics  Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
R-help@r-project.org mailing list
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] cbind and cbind.data.frame

2012-07-09 Thread James Long
## Hi, I'm having trouble understanding how the cbind function decides what
method to apply to its arguments. Easy cut and paste code below.







 ## create two columns

 c1 - c(A,A,B,B)

 c2 - 1:4



 ## cbind outputs a matrix with elements that are characters, seems
reasonable

 out - cbind(c1,c2)

 out

 c1  c2

[1,] A 1

[2,] A 2

[3,] B 3

[4,] B 4

 class(out)

[1] matrix

 mode(out)

[1] character





 ## there are two methods associated with cbind. so i assume that

 ## cbind is calling the data.frame method, because neither of my

 ## arguments are of ts class

 methods(cbind)

[1] cbind.data.frame cbind.ts*


   Non-visible functions are asterisked





 ## now i explicitly tell cbind to use the data.frame method

 ## (which is what i assumed it was doing automatically in the last
example)

 ## but this produces something very different from before, a data.frame

 out2 - cbind.data.frame(c1,c2)

 out2

  c1 c2

1  A  1

2  A  2

3  B  3

4  B  4

 class(out2)

[1] data.frame

 mode(out2)

[1] list





 ## can someone explain why these outputs are different. Thanks, James.









c1 - c(A,A,B,B)

c2 - 1:4

out - cbind(c1,c2)

out

class(out)

mode(out)

methods(cbind)

out2 - cbind.data.frame(c1,c2)

out2

class(out2)

mode(out2)

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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] cbind and cbind.data.frame

2012-07-09 Thread Michael Weylandt
Short answer: there is a default method used here which returns a matrix. It's 
defined at the C-level for speed so you don't see it with methods()

Longer: cbind() isn't a regular S3 generic since it has no UseMethod(). Look at 
WRE and R-Internals (internal generics) for more info. 

Best (and good question!),
Michael

On Jul 9, 2012, at 10:21 PM, James Long jpl2...@gmail.com wrote:

 ## Hi, I'm having trouble understanding how the cbind function decides what
 method to apply to its arguments. Easy cut and paste code below.
 
 
 
 
 
 
 
 ## create two columns
 
 c1 - c(A,A,B,B)
 
 c2 - 1:4
 
 
 
 ## cbind outputs a matrix with elements that are characters, seems
 reasonable
 
 out - cbind(c1,c2)
 
 out
 
 c1  c2
 
 [1,] A 1
 
 [2,] A 2
 
 [3,] B 3
 
 [4,] B 4
 
 class(out)
 
 [1] matrix
 
 mode(out)
 
 [1] character
 
 
 
 
 
 ## there are two methods associated with cbind. so i assume that
 
 ## cbind is calling the data.frame method, because neither of my
 
 ## arguments are of ts class
 
 methods(cbind)
 
 [1] cbind.data.frame cbind.ts*
 
 
   Non-visible functions are asterisked
 
 
 
 
 
 ## now i explicitly tell cbind to use the data.frame method
 
 ## (which is what i assumed it was doing automatically in the last
 example)
 
 ## but this produces something very different from before, a data.frame
 
 out2 - cbind.data.frame(c1,c2)
 
 out2
 
  c1 c2
 
 1  A  1
 
 2  A  2
 
 3  B  3
 
 4  B  4
 
 class(out2)
 
 [1] data.frame
 
 mode(out2)
 
 [1] list
 
 
 
 
 
 ## can someone explain why these outputs are different. Thanks, James.
 
 
 
 
 
 
 
 
 
 c1 - c(A,A,B,B)
 
 c2 - 1:4
 
 out - cbind(c1,c2)
 
 out
 
 class(out)
 
 mode(out)
 
 methods(cbind)
 
 out2 - cbind.data.frame(c1,c2)
 
 out2
 
 class(out2)
 
 mode(out2)
 
[[alternative HTML version deleted]]
 
 __
 R-help@r-project.org mailing list
 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
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] Cbind help

2012-04-16 Thread Osvald Wiklander
Hi, we have a problem concerning the use and forecasting on the GARCH (1, 1) on 
financial data.

We would like to make predictions on future returns in a loop which moves the 
window of estimation forward one unit for each iteration. Our code is as 
follows:

 BJORN - read.table(C:/Users/Osvald/Desktop/DATATILLUPPSATS.txt,
+ header=TRUE, sep=\t, na.strings=NA, dec=,, strip.white=TRUE)
 t - as.timeSeries(BJORN)
 start=length(t[2:1834])
 end=length(t)
 fore = array(0, dim=(end-start))
 for (i in start:end) {
  +fit - garchFit(formula =garch(1,1), data = t[1:i])
  +pred - predict(fit, n.ahead=1)
  +fore[i-start+1] = pred[,1]
+ }



[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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] cbind, data.frame | numeric to string?

2012-04-10 Thread Anser Chen
Complete newbie to R -- struggling with something which should be pretty
basic. Trying to create a simple data set (which I gather R refers to as a
data.frame). So

  a - c(1,2,3,4,5);
  b - c(0.3,0.4,0.5,0,6,0.7);

Stick the two together into a data frame (call test) using cbind

 test - data.frame(cbind(a,b))

Seems to do the trick:

 test
  a   b
1 1 0.3
2 2 0.4
3 3 0.5
4 4 0.6
5 5 0.7


Confirm that each variable is numeric:

 is.numeric(test$a)
[1] TRUE
 is.numeric(test$b)
[1] TRUE


OK, so far so good. But, now I want to merge in a vector of characters:

 c - c('y1,y2,y3,y4,y5)

Confirm that this is string:

 is.numeric(c);
[1] FALSE

cbind c into the data frame:

  test - data.frame(cbind(a,b,c))

Looks like everything is in place:

 test
  a   b  c
1 1 0.3 y1
2 2 0.4 y2
3 3 0.5 y3
4 4 0.6 y4
5 5 0.7 y5

Except that it seems as if the moment I cbind in a character vector, it
changes numeric data to string:

 is.numeric(test$a)
[1] FALSE
 is.numeric(test$b)
[1] FALSE

which would explain why the operations I'm trying to perform on elements of
a and b columns are failing. If I look at the structure of the data.frame,
I see that in fact *all* the variables are being entered as factors.

 str(test)
'data.frame':   5 obs. of  3 variables:
 $ a: Factor w/ 5 levels 1,2,3,4,..: 1 2 3 4 5
 $ b: Factor w/ 5 levels 0.3,0.4,0.5,..: 1 2 3 4 5
 $ c: Factor w/ 5 levels y1,y2,y3,..: 1 2 3 4 5

But, if I try

 test - data.frame(cbind(a,b))
 str(test)
'data.frame':   5 obs. of  2 variables:
 $ a: num  1 2 3 4 5
 $ b: num  0.3 0.4 0.5 0.6 0.7

a and b are coming back as numeric. So, why does cbind'ing a column of
character variables change everything else? And, more to the point, what do
I need to do to 'correct' the problem (i.e., stop this from happening).

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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] cbind, data.frame | numeric to string?

2012-04-10 Thread R. Michael Weylandt
Don't use cbind() -- it forces everything into a single type, here
string, which in turn becomes factor.

Simply,

data.frame(a, b, c)

Like David mentioned a few days ago, I have no idea who is promoting
this data.frame(cbind(...)) idiom, but it's a terrible idea (albeit
one that seems to be very frequent over the last few weeks)

Michael

On Tue, Apr 10, 2012 at 10:33 AM, Anser Chen anser.c...@gmail.com wrote:
 Complete newbie to R -- struggling with something which should be pretty
 basic. Trying to create a simple data set (which I gather R refers to as a
 data.frame). So

  a - c(1,2,3,4,5);
  b - c(0.3,0.4,0.5,0,6,0.7);

 Stick the two together into a data frame (call test) using cbind

 test - data.frame(cbind(a,b))

 Seems to do the trick:

 test
  a   b
 1 1 0.3
 2 2 0.4
 3 3 0.5
 4 4 0.6
 5 5 0.7


 Confirm that each variable is numeric:

 is.numeric(test$a)
 [1] TRUE
 is.numeric(test$b)
 [1] TRUE


 OK, so far so good. But, now I want to merge in a vector of characters:

 c - c('y1,y2,y3,y4,y5)

 Confirm that this is string:

 is.numeric(c);
 [1] FALSE

 cbind c into the data frame:

  test - data.frame(cbind(a,b,c))

 Looks like everything is in place:

 test
  a   b  c
 1 1 0.3 y1
 2 2 0.4 y2
 3 3 0.5 y3
 4 4 0.6 y4
 5 5 0.7 y5

 Except that it seems as if the moment I cbind in a character vector, it
 changes numeric data to string:

 is.numeric(test$a)
 [1] FALSE
 is.numeric(test$b)
 [1] FALSE

 which would explain why the operations I'm trying to perform on elements of
 a and b columns are failing. If I look at the structure of the data.frame,
 I see that in fact *all* the variables are being entered as factors.

 str(test)
 'data.frame':   5 obs. of  3 variables:
  $ a: Factor w/ 5 levels 1,2,3,4,..: 1 2 3 4 5
  $ b: Factor w/ 5 levels 0.3,0.4,0.5,..: 1 2 3 4 5
  $ c: Factor w/ 5 levels y1,y2,y3,..: 1 2 3 4 5

 But, if I try

  test - data.frame(cbind(a,b))
 str(test)
 'data.frame':   5 obs. of  2 variables:
  $ a: num  1 2 3 4 5
  $ b: num  0.3 0.4 0.5 0.6 0.7

 a and b are coming back as numeric. So, why does cbind'ing a column of
 character variables change everything else? And, more to the point, what do
 I need to do to 'correct' the problem (i.e., stop this from happening).

        [[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list
 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
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] cbind, data.frame | numeric to string?

2012-04-10 Thread Jessica Streicher
Still didn't work for me without cbind , although you really don't need it ;)

worked after i set options(stringsAsFactors=F).

 options(stringsAsFactors=F)
 df-data.frame(intVec,chaVec)
 df
  intVec chaVec
1  1  a
2  2  b
3  3  c

 df$chaVec
[1] a b c

documentation of data.frame says the option is true by default.


Am 10.04.2012 um 17:38 schrieb R. Michael Weylandt:

 Don't use cbind() -- it forces everything into a single type, here
 string, which in turn becomes factor.
 
 Simply,
 
 data.frame(a, b, c)
 
 Like David mentioned a few days ago, I have no idea who is promoting
 this data.frame(cbind(...)) idiom, but it's a terrible idea (albeit
 one that seems to be very frequent over the last few weeks)
 
 Michael
 
 On Tue, Apr 10, 2012 at 10:33 AM, Anser Chen anser.c...@gmail.com wrote:
 Complete newbie to R -- struggling with something which should be pretty
 basic. Trying to create a simple data set (which I gather R refers to as a
 data.frame). So
 
  a - c(1,2,3,4,5);
  b - c(0.3,0.4,0.5,0,6,0.7);
 
 Stick the two together into a data frame (call test) using cbind
 
 test - data.frame(cbind(a,b))
 
 Seems to do the trick:
 
 test
  a   b
 1 1 0.3
 2 2 0.4
 3 3 0.5
 4 4 0.6
 5 5 0.7
 
 
 Confirm that each variable is numeric:
 
 is.numeric(test$a)
 [1] TRUE
 is.numeric(test$b)
 [1] TRUE
 
 
 OK, so far so good. But, now I want to merge in a vector of characters:
 
 c - c('y1,y2,y3,y4,y5)
 
 Confirm that this is string:
 
 is.numeric(c);
 [1] FALSE
 
 cbind c into the data frame:
 
  test - data.frame(cbind(a,b,c))
 
 Looks like everything is in place:
 
 test
  a   b  c
 1 1 0.3 y1
 2 2 0.4 y2
 3 3 0.5 y3
 4 4 0.6 y4
 5 5 0.7 y5
 
 Except that it seems as if the moment I cbind in a character vector, it
 changes numeric data to string:
 
 is.numeric(test$a)
 [1] FALSE
 is.numeric(test$b)
 [1] FALSE
 
 which would explain why the operations I'm trying to perform on elements of
 a and b columns are failing. If I look at the structure of the data.frame,
 I see that in fact *all* the variables are being entered as factors.
 
 str(test)
 'data.frame':   5 obs. of  3 variables:
  $ a: Factor w/ 5 levels 1,2,3,4,..: 1 2 3 4 5
  $ b: Factor w/ 5 levels 0.3,0.4,0.5,..: 1 2 3 4 5
  $ c: Factor w/ 5 levels y1,y2,y3,..: 1 2 3 4 5
 
 But, if I try
 
  test - data.frame(cbind(a,b))
 str(test)
 'data.frame':   5 obs. of  2 variables:
  $ a: num  1 2 3 4 5
  $ b: num  0.3 0.4 0.5 0.6 0.7
 
 a and b are coming back as numeric. So, why does cbind'ing a column of
 character variables change everything else? And, more to the point, what do
 I need to do to 'correct' the problem (i.e., stop this from happening).
 
[[alternative HTML version deleted]]
 
 __
 R-help@r-project.org mailing list
 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
 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
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] cbind, data.frame | numeric to string?

2012-04-10 Thread Rainer Schuermann
cbind() works as well, but only if c is attached to the existing test variable:

 tst - cbind( test, c )   

 tst   

   ab   c   

1  1  0.3  y1   

2  2  0.4  y2   

3  3  0.5  y3   

4  4  0.6  y4   

5  5  0.7  y5   

 str( tst )
   
'data.frame':   5 obs. of  3 variables: 
 
 $ a: num  1 2 3 4 5
 
 $ b: num  0.3 0.4 0.5 0.6 0.7  
 
 $ c: Factor w/ 5 levels y1,y2,y3,..: 1 2 3 4 5

Not saying it is a good idea, though...

Rainer


On Tuesday 10 April 2012 11:38:51 R. Michael Weylandt wrote:
 Don't use cbind() -- it forces everything into a single type, here
 string, which in turn becomes factor.
 
 Simply,
 
 data.frame(a, b, c)
 
 Like David mentioned a few days ago, I have no idea who is promoting
 this data.frame(cbind(...)) idiom, but it's a terrible idea (albeit
 one that seems to be very frequent over the last few weeks)
 
 Michael
 
 On Tue, Apr 10, 2012 at 10:33 AM, Anser Chen anser.c...@gmail.com wrote:
  Complete newbie to R -- struggling with something which should be pretty
  basic. Trying to create a simple data set (which I gather R refers to as a
  data.frame). So
 
   a - c(1,2,3,4,5);
   b - c(0.3,0.4,0.5,0,6,0.7);
 
  Stick the two together into a data frame (call test) using cbind
 
  test - data.frame(cbind(a,b))
 
  Seems to do the trick:
 
  test
   a   b
  1 1 0.3
  2 2 0.4
  3 3 0.5
  4 4 0.6
  5 5 0.7
 
 
  Confirm that each variable is numeric:
 
  is.numeric(test$a)
  [1] TRUE
  is.numeric(test$b)
  [1] TRUE
 
 
  OK, so far so good. But, now I want to merge in a vector of characters:
 
  c - c('y1,y2,y3,y4,y5)
 
  Confirm that this is string:
 
  is.numeric(c);
  [1] FALSE
 
  cbind c into the data frame:
 
   test - data.frame(cbind(a,b,c))
 
  Looks like everything is in place:
 
  test
   a   b  c
  1 1 0.3 y1
  2 2 0.4 y2
  3 3 0.5 y3
  4 4 0.6 y4
  5 5 0.7 y5
 
  Except that it seems as if the moment I cbind in a character vector, it
  changes numeric data to string:
 
  is.numeric(test$a)
  [1] FALSE
  is.numeric(test$b)
  [1] FALSE
 
  which would explain why the operations I'm trying to perform on elements of
  a and b columns are failing. If I look at the structure of the data.frame,
  I see that in fact *all* the variables are being entered as factors.
 
  str(test)
  'data.frame':   5 obs. of  3 variables:
   $ a: Factor w/ 5 levels 1,2,3,4,..: 1 2 3 4 5
   $ b: Factor w/ 5 levels 0.3,0.4,0.5,..: 1 2 3 4 5
   $ c: Factor w/ 5 levels y1,y2,y3,..: 1 2 3 4 5
 
  But, if I try
 
   test - data.frame(cbind(a,b))
  str(test)
  'data.frame':   5 obs. of  2 variables:
   $ a: num  1 2 3 4 5
   $ b: num  0.3 0.4 0.5 0.6 0.7
 
  a and b are coming back as numeric. So, why does cbind'ing a column of
  character variables change everything else? And, more to the point, what do
  I need to do to 'correct' the problem (i.e., stop this from happening).
 
 [[alternative HTML version deleted]]
 
  __
  R-help@r-project.org mailing list
  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
 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
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] cbind, data.frame | numeric to string?

2012-04-10 Thread R. Michael Weylandt
Sorry, I missed that the OP's real question was in character/factor,
not in the why are these all factors bit...good catch.

Rant about cbind() still stands though. :-)   [Your way with cbind()
would give him all characters, not some characters and some numerics
since cbind() gives a matrix by default -- note that in Rainer's
construction, it doesn't coerce because it's giving a data.frame (as
cbind.data.frame does but cdbind.default doesn't) so no coercion is
necessary ]

Best,

Michael

On Tue, Apr 10, 2012 at 11:55 AM, Jessica Streicher
j.streic...@micromata.de wrote:
 Still didn't work for me without cbind , although you really don't need it ;)

 worked after i set options(stringsAsFactors=F).

 options(stringsAsFactors=F)
 df-data.frame(intVec,chaVec)
 df
  intVec chaVec
 1      1      a
 2      2      b
 3      3      c

 df$chaVec
 [1] a b c

 documentation of data.frame says the option is true by default.


 Am 10.04.2012 um 17:38 schrieb R. Michael Weylandt:

 Don't use cbind() -- it forces everything into a single type, here
 string, which in turn becomes factor.

 Simply,

 data.frame(a, b, c)

 Like David mentioned a few days ago, I have no idea who is promoting
 this data.frame(cbind(...)) idiom, but it's a terrible idea (albeit
 one that seems to be very frequent over the last few weeks)

 Michael

 On Tue, Apr 10, 2012 at 10:33 AM, Anser Chen anser.c...@gmail.com wrote:
 Complete newbie to R -- struggling with something which should be pretty
 basic. Trying to create a simple data set (which I gather R refers to as a
 data.frame). So

  a - c(1,2,3,4,5);
  b - c(0.3,0.4,0.5,0,6,0.7);

 Stick the two together into a data frame (call test) using cbind

 test - data.frame(cbind(a,b))

 Seems to do the trick:

 test
  a   b
 1 1 0.3
 2 2 0.4
 3 3 0.5
 4 4 0.6
 5 5 0.7


 Confirm that each variable is numeric:

 is.numeric(test$a)
 [1] TRUE
 is.numeric(test$b)
 [1] TRUE


 OK, so far so good. But, now I want to merge in a vector of characters:

 c - c('y1,y2,y3,y4,y5)

 Confirm that this is string:

 is.numeric(c);
 [1] FALSE

 cbind c into the data frame:

  test - data.frame(cbind(a,b,c))

 Looks like everything is in place:

 test
  a   b  c
 1 1 0.3 y1
 2 2 0.4 y2
 3 3 0.5 y3
 4 4 0.6 y4
 5 5 0.7 y5

 Except that it seems as if the moment I cbind in a character vector, it
 changes numeric data to string:

 is.numeric(test$a)
 [1] FALSE
 is.numeric(test$b)
 [1] FALSE

 which would explain why the operations I'm trying to perform on elements of
 a and b columns are failing. If I look at the structure of the data.frame,
 I see that in fact *all* the variables are being entered as factors.

 str(test)
 'data.frame':   5 obs. of  3 variables:
  $ a: Factor w/ 5 levels 1,2,3,4,..: 1 2 3 4 5
  $ b: Factor w/ 5 levels 0.3,0.4,0.5,..: 1 2 3 4 5
  $ c: Factor w/ 5 levels y1,y2,y3,..: 1 2 3 4 5

 But, if I try

  test - data.frame(cbind(a,b))
 str(test)
 'data.frame':   5 obs. of  2 variables:
  $ a: num  1 2 3 4 5
  $ b: num  0.3 0.4 0.5 0.6 0.7

 a and b are coming back as numeric. So, why does cbind'ing a column of
 character variables change everything else? And, more to the point, what do
 I need to do to 'correct' the problem (i.e., stop this from happening).

        [[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list
 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
 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
 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
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] cbind, data.frame | numeric to string?

2012-04-10 Thread David Winsemius


On Apr 10, 2012, at 11:58 AM, Rainer Schuermann wrote:

cbind() works as well, but only if c is attached to the existing  
test variable:



tst - cbind( test, c )
tst

  ab   c
1  1  0.3  y1
2  2  0.4  y2
3  3  0.5  y3
4  4  0.6  y4
5  5  0.7  y5

str( tst )

'data.frame':   5 obs. of  3 variables:
$ a: num  1 2 3 4 5
$ b: num  0.3 0.4 0.5 0.6 0.7
$ c: Factor w/ 5 levels y1,y2,y3,..: 1 2 3 4 5

Not saying it is a good idea, though...


To be somewhat more expansive ... 'cbind' is not just one function,  
but rather a set of functions, since it is generic. The one that is  
chosen by the interpreter will depend on whether the first argument  
has a class. If it does have a class as in the example above having a  
class of data.frame, then the cbind.data.frame function will be  
dispatched to process the list of arguments. If the first argument  
doesn't have a class as in the OP's second example below, then the  
internal cbind function will be used and returns a matrics which  
strips off all but a few attributes and forces a lowest common  
denominator mode. If only one of the arguments were logical, then  
cbind would return a a matrix of all TRUEs and FALSEs.


(This all assumes that the typos in the OP's original example that  
created 'c' as an incomplete expression and a and b with unequal  
lengths were fixed.)


 a - c(1,2,3,4,5);
 b - c(0.3,0.4,0.5,0,6,0.7);
 test - data.frame(cbind(a,b))
Warning message:
In cbind(a, b) :
  number of rows of result is not a multiple of vector length (arg 1)
 c - c(y1,y2,y3,y4,y5)
 cbind(c, test)
Error in data.frame(..., check.names = FALSE) :
  arguments imply differing number of rows: 5, 6
--
David.




Rainer


On Tuesday 10 April 2012 11:38:51 R. Michael Weylandt wrote:

Don't use cbind() -- it forces everything into a single type, here
string, which in turn becomes factor.

Simply,

data.frame(a, b, c)

Like David mentioned a few days ago, I have no idea who is promoting
this data.frame(cbind(...)) idiom, but it's a terrible idea (albeit
one that seems to be very frequent over the last few weeks)

Michael

On Tue, Apr 10, 2012 at 10:33 AM, Anser Chen anser.c...@gmail.com  
wrote:
Complete newbie to R -- struggling with something which should be  
pretty
basic. Trying to create a simple data set (which I gather R refers  
to as a

data.frame). So


a - c(1,2,3,4,5);
b - c(0.3,0.4,0.5,0,6,0.7);


Stick the two together into a data frame (call test) using cbind


test - data.frame(cbind(a,b))


Seems to do the trick:


test

a   b
1 1 0.3
2 2 0.4
3 3 0.5
4 4 0.6
5 5 0.7




Confirm that each variable is numeric:


is.numeric(test$a)

[1] TRUE

is.numeric(test$b)

[1] TRUE


OK, so far so good. But, now I want to merge in a vector of  
characters:



c - c('y1,y2,y3,y4,y5)


Confirm that this is string:


is.numeric(c);

[1] FALSE

cbind c into the data frame:


test - data.frame(cbind(a,b,c))


Looks like everything is in place:


test

a   b  c
1 1 0.3 y1
2 2 0.4 y2
3 3 0.5 y3
4 4 0.6 y4
5 5 0.7 y5

Except that it seems as if the moment I cbind in a character  
vector, it

changes numeric data to string:


is.numeric(test$a)

[1] FALSE

is.numeric(test$b)

[1] FALSE

which would explain why the operations I'm trying to perform on  
elements of
a and b columns are failing. If I look at the structure of the  
data.frame,
I see that in fact *all* the variables are being entered as  
factors.



str(test)

'data.frame':   5 obs. of  3 variables:
$ a: Factor w/ 5 levels 1,2,3,4,..: 1 2 3 4 5
$ b: Factor w/ 5 levels 0.3,0.4,0.5,..: 1 2 3 4 5
$ c: Factor w/ 5 levels y1,y2,y3,..: 1 2 3 4 5

But, if I try

test - data.frame(cbind(a,b))

str(test)

'data.frame':   5 obs. of  2 variables:
$ a: num  1 2 3 4 5
$ b: num  0.3 0.4 0.5 0.6 0.7

a and b are coming back as numeric. So, why does cbind'ing a  
column of
character variables change everything else? And, more to the  
point, what do
I need to do to 'correct' the problem (i.e., stop this from  
happening).


  [[alternative HTML version deleted]]



David Winsemius, MD
West Hartford, CT

__
R-help@r-project.org mailing list
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] cbind, data.frame | numeric to string?

2012-04-10 Thread David Winsemius


On Apr 10, 2012, at 12:19 PM, David Winsemius wrote:



On Apr 10, 2012, at 11:58 AM, Rainer Schuermann wrote:

cbind() works as well, but only if c is attached to the existing  
test variable:



tst - cbind( test, c )
tst

 ab   c
1  1  0.3  y1
2  2  0.4  y2
3  3  0.5  y3
4  4  0.6  y4
5  5  0.7  y5

str( tst )

'data.frame':   5 obs. of  3 variables:
$ a: num  1 2 3 4 5
$ b: num  0.3 0.4 0.5 0.6 0.7
$ c: Factor w/ 5 levels y1,y2,y3,..: 1 2 3 4 5

Not saying it is a good idea, though...


To be somewhat more expansive ... 'cbind' is not just one function,  
but rather a set of functions, since it is generic. The one that  
is chosen by the interpreter will depend on whether the first  
argument has a class.


That was just my erroneous impression. If _any_ of the objects in the  
argument list is a data.frame then cbind.data.frame appears to get  
used. There is a Dispatch section on the help page for cbind that  
appears to cover this adequately.


If it does have a class as in the example above having a class of  
data.frame, then the cbind.data.frame function will be dispatched  
to process the list of arguments. If the first argument doesn't have  
a class as in the OP's second example below, then the internal cbind  
function will be used and returns a matrics which strips off all but  
a few attributes and forces a lowest common denominator mode. If  
only one of the arguments were logical, then cbind would return a a  
matrix of all TRUEs and FALSEs.


(This all assumes that the typos in the OP's original example that  
created 'c' as an incomplete expression and a and b with unequal  
lengths were fixed.)


 a - c(1,2,3,4,5);
 b - c(0.3,0.4,0.5,0,6,0.7);
 test - data.frame(cbind(a,b))
Warning message:
In cbind(a, b) :
 number of rows of result is not a multiple of vector length (arg 1)
 c - c(y1,y2,y3,y4,y5)
 cbind(c, test)
Error in data.frame(..., check.names = FALSE) :
 arguments imply differing number of rows: 5, 6
--
David.




Rainer


On Tuesday 10 April 2012 11:38:51 R. Michael Weylandt wrote:

Don't use cbind() -- it forces everything into a single type, here
string, which in turn becomes factor.

Simply,

data.frame(a, b, c)

Like David mentioned a few days ago, I have no idea who is promoting
this data.frame(cbind(...)) idiom, but it's a terrible idea (albeit
one that seems to be very frequent over the last few weeks)

Michael

On Tue, Apr 10, 2012 at 10:33 AM, Anser Chen  
anser.c...@gmail.com wrote:
Complete newbie to R -- struggling with something which should be  
pretty
basic. Trying to create a simple data set (which I gather R  
refers to as a

data.frame). So


a - c(1,2,3,4,5);
b - c(0.3,0.4,0.5,0,6,0.7);


Stick the two together into a data frame (call test) using cbind


test - data.frame(cbind(a,b))


Seems to do the trick:


test

a   b
1 1 0.3
2 2 0.4
3 3 0.5
4 4 0.6
5 5 0.7




Confirm that each variable is numeric:


is.numeric(test$a)

[1] TRUE

is.numeric(test$b)

[1] TRUE


OK, so far so good. But, now I want to merge in a vector of  
characters:



c - c('y1,y2,y3,y4,y5)


Confirm that this is string:


is.numeric(c);

[1] FALSE

cbind c into the data frame:


test - data.frame(cbind(a,b,c))


Looks like everything is in place:


test

a   b  c
1 1 0.3 y1
2 2 0.4 y2
3 3 0.5 y3
4 4 0.6 y4
5 5 0.7 y5

Except that it seems as if the moment I cbind in a character  
vector, it

changes numeric data to string:


is.numeric(test$a)

[1] FALSE

is.numeric(test$b)

[1] FALSE

which would explain why the operations I'm trying to perform on  
elements of
a and b columns are failing. If I look at the structure of the  
data.frame,
I see that in fact *all* the variables are being entered as  
factors.



str(test)

'data.frame':   5 obs. of  3 variables:
$ a: Factor w/ 5 levels 1,2,3,4,..: 1 2 3 4 5
$ b: Factor w/ 5 levels 0.3,0.4,0.5,..: 1 2 3 4 5
$ c: Factor w/ 5 levels y1,y2,y3,..: 1 2 3 4 5

But, if I try

test - data.frame(cbind(a,b))

str(test)

'data.frame':   5 obs. of  2 variables:
$ a: num  1 2 3 4 5
$ b: num  0.3 0.4 0.5 0.6 0.7

a and b are coming back as numeric. So, why does cbind'ing a  
column of
character variables change everything else? And, more to the  
point, what do
I need to do to 'correct' the problem (i.e., stop this from  
happening).


 [[alternative HTML version deleted]]



David Winsemius, MD
West Hartford, CT

__
R-help@r-project.org mailing list
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, MD
West Hartford, CT

__
R-help@r-project.org mailing list
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] cbind alternate

2012-01-06 Thread Mary Kindall
I have two one dimensional list of elements and want to perform cbind and
then write into a file. The number of entries are more than a million in
both lists. R is taking a lot of time performing this operation.

Is there any alternate way to perform cbind?

x = table1[1:100,1]
y = table2[1:100,5]

z = cbind(x,y)   //hanging the machine

write.table(z,'out.txt)



-- 
-
Mary Kindall
Yorktown Heights, NY
USA

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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] cbind alternate

2012-01-06 Thread Bos, Roger
You could break the data into chunks, so you cbind and save 50,000
observations at a time.  That should be less taxing on your machine and
memory.

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
On Behalf Of Mary Kindall
Sent: Friday, January 06, 2012 12:43 PM
To: r-help@r-project.org
Subject: [R] cbind alternate

I have two one dimensional list of elements and want to perform cbind
and then write into a file. The number of entries are more than a
million in both lists. R is taking a lot of time performing this
operation.

Is there any alternate way to perform cbind?

x = table1[1:100,1]
y = table2[1:100,5]

z = cbind(x,y)   //hanging the machine

write.table(z,'out.txt)



--
-
Mary Kindall
Yorktown Heights, NY
USA

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.

***
This message is for the named person's use only. It may\...{{dropped:11}}

__
R-help@r-project.org mailing list
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] cbind alternate

2012-01-06 Thread Marc Schwartz
On Jan 6, 2012, at 11:43 AM, Mary Kindall wrote:

 I have two one dimensional list of elements and want to perform cbind and
 then write into a file. The number of entries are more than a million in
 both lists. R is taking a lot of time performing this operation.
 
 Is there any alternate way to perform cbind?
 
 x = table1[1:100,1]
 y = table2[1:100,5]
 
 z = cbind(x,y)   //hanging the machine
 
 write.table(z,'out.txt)



The issue is not the use of cbind(), but that write.table() can be slow with 
data frames, where each column may be a different class (data type) and 
requires separate formatting for output. This is referenced in the Note section 
of ?write.table:

write.table can be slow for data frames with large numbers (hundreds or more) 
of columns: this is inevitable as each column could be of a different class and 
so must be handled separately. If they are all of the same class, consider 
using a matrix instead.


I suspect in this case, while you don't have a large number of columns, you do 
have a large number of rows, so that there is a tradeoff.

If all of the columns in your source tables are of the same type (eg. all 
numeric), coerce 'z' to a matrix and then try using write.table().

z - matrix(rnorm(100 * 6), ncol = 6)

 str(z)
 num [1:100, 1:6] -0.713 0.79 -0.538 0.945 1.621 ...

 system.time(write.table(z, file = test.txt))
   user  system elapsed 
 12.664   0.292  13.029 


The resultant file is about 118 Mb on my system.

HTH,

Marc Schwartz

__
R-help@r-project.org mailing list
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] cbind alternate

2012-01-06 Thread David Winsemius


On Jan 6, 2012, at 12:43 PM, Mary Kindall wrote:

I have two one dimensional list of elements and want to perform  
cbind and
then write into a file. The number of entries are more than a  
million in

both lists. R is taking a lot of time performing this operation.

Is there any alternate way to perform cbind?

x =
y = table2[1:100,5]

z = cbind(x,y)   //hanging the machine


You should have been able to bypass the intermediate steps with just:

z = cbind( table1[1:100,1] table2[1:100,5])

Whether you will have sufficient contiguous memory for that object at  
the moment or even after rm(x), rm(y) is in doubt, but had you not  
created the unneeded x and y, you _might_ have succeeded in your  
limited environment. (Real answer: Buy more RAM.)


I speculate that you are on Windows and so refer your to the R-Win FAQ  
for further reading about memory limits.





write.table(z,'out.txt)


I do not know of a way to bypass the requirement of a named object to  
pass to write.table, but testing suggests that you could try:


write( t(cbind( table1[1:100,1] table2[1:100,5])).   
test.txt, 2)


write()  does not require a named object but is less inquisitive than  
write table and will give you a transposed matrix with 5 columns by  
default which will really mess up things, so you need to transpose and  
specify the number of columns. (And that may not save any space over  
creating a z object.)


So there is another thread today to which master R programmer Bill  
Dunlap has offered this strategy (with minor modifications to your  
situation by me):


###
f1 - function (n, fileName) {
 unlink(fileName)
 system.time({
 fileConn - file(fileName, wt)
 on.exit(close(fileConn))
 for (i in seq_len(n)) cat( table1[i, 1],  ,
table2[i, 5],
  \n, file = fileConn)
 })
 }

f1(100, 'out.txt')

#
--

David Winsemius, MD
West Hartford, CT

__
R-help@r-project.org mailing list
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] cbind alternate

2012-01-06 Thread Marc Schwartz

On Jan 6, 2012, at 12:39 PM, Marc Schwartz wrote:

 On Jan 6, 2012, at 11:43 AM, Mary Kindall wrote:
 
 I have two one dimensional list of elements and want to perform cbind and
 then write into a file. The number of entries are more than a million in
 both lists. R is taking a lot of time performing this operation.
 
 Is there any alternate way to perform cbind?
 
 x = table1[1:100,1]
 y = table2[1:100,5]
 
 z = cbind(x,y)   //hanging the machine
 
 write.table(z,'out.txt)
 

Apologies, I mis-read where the hang up was. It is in the use of cbind() prior 
to calling write.table(), not in write.table() itself.

Not sure why that part is taking a long time, unless as already mentioned, you 
are short on memory available. This runs quickly for me:

x - matrix(rnorm(100 * 3), ncol = 3)
y - matrix(rnorm(100 * 3), ncol = 3)
 
 system.time(z - cbind(x, y))
   user  system elapsed 
  0.039   0.025   0.065 

 str(z)
 num [1:100, 1:6] -0.5102 1.8776 2.4635 0.2982 0.0901 ...


To give an example with two data frames containing differing data types, let's 
use the built-in 'iris' data set, which has 5 columns and 150 rows by default. 
Let's create a new version with over a million rows:

iris.new - iris[rep(seq(nrow(iris)), 7000), ]

 str(iris.new)
'data.frame':   105 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species : Factor w/ 3 levels setosa,versicolor,..: 1 1 1 1 1 1 1 1 1 
1 ...


 system.time(iris.new2 - cbind(iris.new, iris.new))
   user  system elapsed 
  5.289   0.282   5.658 


 str(iris.new2)
'data.frame':   105 obs. of  10 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species : Factor w/ 3 levels setosa,versicolor,..: 1 1 1 1 1 1 1 1 1 
1 ...
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species : Factor w/ 3 levels setosa,versicolor,..: 1 1 1 1 1 1 1 1 1 
1 ...


You might verify the structures of your 'x' and 'y' to be sure that there is 
not something amiss with either one.

HTH,

Marc Schwartz

__
R-help@r-project.org mailing list
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] cbind alternate

2012-01-06 Thread Rui Barradas
Hello,

I believe this function can handle a problem of that size, or bigger.

It does NOT create the full matrix, just writes it to a file, a certain
number of lines at a time.


write.big.matrix - function(x, y, outfile, nmax=1000){

if(file.exists(outfile)) unlink(outfile)
testf - file(outfile, at)   # or wt - write text
on.exit(close(testf))

step - nmax # how many at a time
inx  - seq(1, length(x), by=step)   # index into 'x' and 'y'
mat  - matrix(0, nrow=step, ncol=2) # create a work matrix

# do it 'nmax' rows per iteration
for(i in inx){
mat - cbind(x[i:(i+step-1)], y[i:(i+step-1)])
write.table(mat, file=testf, quote=FALSE, row.names=FALSE,
col.names=FALSE)
}

# and now the remainder
mat - NULL
mat - cbind(x[(i+1):length(x)], y[(i+1):length(y)])
write.table(mat, file=testf, quote=FALSE, row.names=FALSE, 
col.names=FALSE)

# return the output filename
outfile
}

x - 1:1e6  # a numeric vector
y - sample(letters, 1e6, replace=TRUE) # and a character vector
length(x);length(y) # of the same length
fl - test.txt# output file

system.time(write.big.matrix(x, y, outfile=fl))


On my system it takes (sample output)

   user  system elapsed 
   1.590.041.65

and can handle different types of data. In the example, numeric and
character.

If you also need the matrix, try to use 'cbind' first, without writing to a
file.
If it's still slow, adapt the code above to keep inserting chunks in an
output matrix.

Rui Barradas




--
View this message in context: 
http://r.789695.n4.nabble.com/cbind-alternate-tp4270188p4270444.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
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] cbind alternate

2012-01-06 Thread Rui Barradas
Sorry Mary,

My function would write the remainder twice, I had only tested it
with multiples of the chunk size.
(And without looking at the lenghty output correctly.)

Now checked:

write.big.matrix - function(x, y, outfile, nmax=1000){

if(file.exists(outfile)) unlink(outfile)
testf - file(outfile, at)   # or wt - write text
on.exit(close(testf))

step - nmax  # how many at a time
inx  - seq(1, length(x)-step, by=step)   # index into 'x' and 'y'

mat  - matrix(0, nrow=step, ncol=2) # create a work matrix

# do it 'nmax' rows per iteration
for(i in inx){
mat - cbind(x[i:(i+step-1)], y[i:(i+step-1)])
write.table(mat, file=testf, quote=FALSE, row.names=FALSE,
col.names=FALSE)
}

# and now the remainder
if(i+step  length(x)){
mat - NULL
mat - cbind(x[(i+step):length(x)], y[(i+step):length(y)])
write.table(mat, file=testf, quote=FALSE, row.names=FALSE,
col.names=FALSE)
}
# return the output filename
outfile
}

x - 1:(1e6 + 1234) # a numeric vector
y - sample(letters, 1e6 + 1234, replace=TRUE)  # and a character vector
length(x);length(y) # of the same length
fl - test.txt# output file

system.time(write.big.matrix(x, y, outfile=fl, nmax=100))

   user  system elapsed 
   3.040.063.09

system.time(write.big.matrix(x, y, outfile=fl))

   user  system elapsed 
   1.640.121.76

Rui Barradas


--
View this message in context: 
http://r.789695.n4.nabble.com/cbind-alternate-tp4270188p4270687.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
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] cbind alternate

2012-01-06 Thread jim holtman
What is it you want to do with the data after you save it?  Are you
just going to read it back into R?  If so, consider using save/load.

On Fri, Jan 6, 2012 at 12:43 PM, Mary Kindall mary.kind...@gmail.com wrote:
 I have two one dimensional list of elements and want to perform cbind and
 then write into a file. The number of entries are more than a million in
 both lists. R is taking a lot of time performing this operation.

 Is there any alternate way to perform cbind?

 x = table1[1:100,1]
 y = table2[1:100,5]

 z = cbind(x,y)   //hanging the machine

 write.table(z,'out.txt)



 --
 -
 Mary Kindall
 Yorktown Heights, NY
 USA

        [[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list
 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.



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

__
R-help@r-project.org mailing list
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] cbind giving NA's?

2011-10-24 Thread Joshua Ulrich
Note that this issue was raised on StackOverflow recently.
http://stackoverflow.com/questions/7678090/xts-merge-odd-behaviour

Here's the solution:

index(a) - index(a)
index(b) - index(b)
merge(a,b)
   ZWD.UGX SCHB.Close
2010-03-31  NA  28.02
2010-04-01  7.6343 NA
2010-04-02  7.6343 NA
2010-04-03  7.5458 NA
2010-04-04  7.4532  28.30
2010-04-05  7.4040  28.38
2010-04-06  7.3317  28.21
2010-04-07  NA  28.31
2010-04-08  NA  28.47

Jeff's answer on StackOverflow explains why this works.

Best,
--
Joshua Ulrich  |  FOSS Trading: www.fosstrading.com



On Fri, Aug 26, 2011 at 8:09 AM, R. Michael Weylandt
michael.weyla...@gmail.com wrote:
 This seems to be the easiest way to handle the problem:

 a = xts(coredata(a), time(a))
 b = xts(coredata(b), time(b))
 merge(a,b)


           ZWD.UGX SCHB.Close
 2010-03-31      NA      28.02
 2010-04-01  7.6343         NA
 2010-04-02  7.6343         NA
 2010-04-03  7.5458         NA
 2010-04-04  7.4532      28.30
 2010-04-05  7.4040      28.38
 2010-04-06  7.3317      28.21
 2010-04-07      NA      28.31
 2010-04-08      NA      28.47

 I can't be sure where the problem was coming from, but if you look
 closely into the mumbo jumbo of the dput() code you'll see that the
 time indices don't actually match anywhere as POSIXct/POSIXt objects.
 Even though they map to the same day, something or other is keeping
 them as internally different and it's probably inherited from the data
 sources -- one set maps to times captured at 2300 EDT on my machine
 and the other to 0800 EDT. If you want, it might be interesting to
 track down the difference, but the fix I gave above, which basically
 pulls the time data out through the xts class and only keeps the date
 in creating a new xts that's exactly the same should handle it.

 In short, it's all black magic derived from R's various time/date
 mechanisms. If you don't


 really want to see whats under the hood, this will also work in this case.


 a = as.xts(a)
 b = as.xts(b)
 merge(a, b)

 But this can lead to some strange code if you don't take a moment to think
 about it

 is.xts(a)
 TRUE

 identical(a, as.xts(a))
 FALSE

 Hope this helps and thanks for the fun problem!

 Michael Weylandt




 On Fri, Aug 26, 2011 at 8:08 AM, Petr PIKAL petr.pi...@precheza.cz wrote:

 I was rather too quick

 It has probably something to do with versions of zoo and xts

 after updating to zoo 1.7.4 and xts 0.8.2 I got with your examples

  merge(a,b)
           ZWD.UGX SCHB.Close
 2010-04-01      NA      28.02
 2010-04-01  7.6343         NA
 2010-04-02  7.6343         NA
 2010-04-03  7.5458         NA
 2010-04-04  7.4532         NA
 2010-04-05      NA      28.30
 2010-04-05  7.4040         NA
 2010-04-06      NA      28.38
 2010-04-06  7.3317         NA
 2010-04-07      NA      28.21
 2010-04-08      NA      28.31
 2010-04-09      NA      28.47

 but when I did

  a-as.zoo(a)
  b-as.zoo(b)

 I got

  merge(a,b)
           ZWD.UGX SCHB.Close
 2010-04-01  7.6343      28.02
 2010-04-02  7.6343         NA
 2010-04-03  7.5458         NA
 2010-04-04  7.4532         NA
 2010-04-05  7.4040      28.30
 2010-04-06  7.3317      28.38
 2010-04-07      NA      28.21
 2010-04-08      NA      28.31
 2010-04-09      NA      28.47

 which is probably what you want.

 Regards
 Petr

 

  Hi
 
  
   On 26 August 2011 03:37, R. Michael Weylandt
 michael.weyla...@gmail.com wrote:
If you could, dput() them so we can see everything about them. You
 also
might see if merge() gives you more expected behavior
  
   Ok...
dput(a)
   structure(c(7.6343, 7.6343, 7.5458, 7.4532, 7.404, 7.3317), class =
 c(xts,
   zoo), .indexCLASS = Date, .indexTZ = , index =
 structure(c(1270105200,
   1270191600, 1270278000, 1270364400, 1270450800, 1270537200), tzone =
   , tclass = Date), .Dim = c(6L,
   1L), .Dimnames = list(NULL, ZWD.UGX))
dput(b)
   structure(c(28.02, 28.3, 28.38, 28.21, 28.31, 28.47), .indexCLASS =
   Date, .indexTZ = , src = yahoo, updated =
   structure(1314356091.21457, class = c(POSIXct,
   POSIXt)), class = c(xts, zoo), index = structure(c(1270072800,
   1270418400, 1270504800, 1270591200, 1270677600, 1270764000), tzone =
   , tclass = Date), .Dim = c(6L,
   1L), .Dimnames = list(NULL, SCHB.Close))
merge(a,b)
merge(a,b)
              ZWD.UGX SCHB.Close
   2010-04-01      NA      28.02
   2010-04-01  7.6343         NA
   2010-04-02  7.6343         NA
   2010-04-03  7.5458         NA
   2010-04-04  7.4532         NA
   2010-04-05      NA      28.30
   2010-04-05  7.4040         NA
   2010-04-06      NA      28.38
   2010-04-06  7.3317         NA
   2010-04-07      NA      28.21
   2010-04-08      NA      28.31
   2010-04-09      NA      28.47
q()

  I get slightly different result
 
   xx-(merge(a,b))
   xx
             ZWD.UGX SCHB.Close
  1270072800      NA      28.02
  1270105200  7.6343         NA
  1270191600  7.6343         NA
  1270278000  7.5458         NA
  

[R] cbind giving NA's?

2011-08-26 Thread Hasan Diwan
I have two xts objects, call them a and b, and am trying to merge them...
 class(a)
[1] xts zoo
 class(b)
[1] xts zoo
 head(a)
2010-04-01  7.6343
2010-04-02  7.6343
2010-04-03  7.5458
2010-04-04  7.4532
2010-04-05  7.4040
2010-04-06  7.3317
 head(b)
2010-04-01 568.80
2010-04-05 571.01
2010-04-06 568.22
2010-04-07 563.54
2010-04-08 567.49
2010-04-09 566.22
 head(cbind(a,b))
2010-04-01 568.80  NA
2010-04-01 NA  0.3998
2010-04-02 NA  0.3998
2010-04-03 NA  0.3999
2010-04-04 NA  0.3994
2010-04-05 571.01  NA
 q()
I'd like the cbind'ed version to look like:
2010-04-01 568.800.3998
2010-04-02 NA  0.3998
2010-04-03 NA  0.3999
2010-04-04 NA  0.3994
2010-04-05 571.01  0.3991

What gives? Many thanks, in advance. -- H

-- 
Sent from my mobile device
Envoyait de mon telephone mobil

__
R-help@r-project.org mailing list
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] cbind giving NA's?

2011-08-26 Thread R. Michael Weylandt
Hmm, that's quite puzzling. I don't know but I'd willing to guess the
time/date stamps on a,b are more different than the output is leading us to
believe. My experience is that there's always more to a time/date object to
trick us up than one would expect.

If you could, dput() them so we can see everything about them. You also
might see if merge() gives you more expected behavior

Michael Weylandt


On Fri, Aug 26, 2011 at 6:05 AM, Hasan Diwan hasan.di...@gmail.com wrote:

 I have two xts objects, call them a and b, and am trying to merge
 them...
  class(a)
 [1] xts zoo
  class(b)
 [1] xts zoo
  head(a)
 2010-04-01  7.6343
 2010-04-02  7.6343
 2010-04-03  7.5458
 2010-04-04  7.4532
 2010-04-05  7.4040
 2010-04-06  7.3317
  head(b)
 2010-04-01 568.80
 2010-04-05 571.01
 2010-04-06 568.22
 2010-04-07 563.54
 2010-04-08 567.49
 2010-04-09 566.22
  head(cbind(a,b))
 2010-04-01 568.80  NA
 2010-04-01 NA  0.3998
 2010-04-02 NA  0.3998
 2010-04-03 NA  0.3999
 2010-04-04 NA  0.3994
 2010-04-05 571.01  NA
  q()
 I'd like the cbind'ed version to look like:
 2010-04-01 568.800.3998
 2010-04-02 NA  0.3998
 2010-04-03 NA  0.3999
 2010-04-04 NA  0.3994
 2010-04-05 571.01  0.3991

 What gives? Many thanks, in advance. -- H

 --
 Sent from my mobile device
 Envoyait de mon telephone mobil

 __
 R-help@r-project.org mailing list
 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
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] cbind giving NA's?

2011-08-26 Thread Hasan Diwan
On 26 August 2011 03:37, R. Michael Weylandt michael.weyla...@gmail.com wrote:
 If you could, dput() them so we can see everything about them. You also
 might see if merge() gives you more expected behavior

Ok...
 dput(a)
structure(c(7.6343, 7.6343, 7.5458, 7.4532, 7.404, 7.3317), class = c(xts,
zoo), .indexCLASS = Date, .indexTZ = , index = structure(c(1270105200,
1270191600, 1270278000, 1270364400, 1270450800, 1270537200), tzone =
, tclass = Date), .Dim = c(6L,
1L), .Dimnames = list(NULL, ZWD.UGX))
 dput(b)
structure(c(28.02, 28.3, 28.38, 28.21, 28.31, 28.47), .indexCLASS =
Date, .indexTZ = , src = yahoo, updated =
structure(1314356091.21457, class = c(POSIXct,
POSIXt)), class = c(xts, zoo), index = structure(c(1270072800,
1270418400, 1270504800, 1270591200, 1270677600, 1270764000), tzone =
, tclass = Date), .Dim = c(6L,
1L), .Dimnames = list(NULL, SCHB.Close))
 merge(a,b)
 merge(a,b)
   ZWD.UGX SCHB.Close
2010-04-01  NA  28.02
2010-04-01  7.6343 NA
2010-04-02  7.6343 NA
2010-04-03  7.5458 NA
2010-04-04  7.4532 NA
2010-04-05  NA  28.30
2010-04-05  7.4040 NA
2010-04-06  NA  28.38
2010-04-06  7.3317 NA
2010-04-07  NA  28.21
2010-04-08  NA  28.31
2010-04-09  NA  28.47
 q()

So, no, merge doesn't work...
-- 
Sent from my mobile device
Envoyait de mon telephone mobil

__
R-help@r-project.org mailing list
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] cbind giving NA's?

2011-08-26 Thread Petr PIKAL
Hi

 
 On 26 August 2011 03:37, R. Michael Weylandt 
michael.weyla...@gmail.com wrote:
  If you could, dput() them so we can see everything about them. You 
also
  might see if merge() gives you more expected behavior
 
 Ok...
  dput(a)
 structure(c(7.6343, 7.6343, 7.5458, 7.4532, 7.404, 7.3317), class = 
c(xts,
 zoo), .indexCLASS = Date, .indexTZ = , index = 
structure(c(1270105200,
 1270191600, 1270278000, 1270364400, 1270450800, 1270537200), tzone =
 , tclass = Date), .Dim = c(6L,
 1L), .Dimnames = list(NULL, ZWD.UGX))
  dput(b)
 structure(c(28.02, 28.3, 28.38, 28.21, 28.31, 28.47), .indexCLASS =
 Date, .indexTZ = , src = yahoo, updated =
 structure(1314356091.21457, class = c(POSIXct,
 POSIXt)), class = c(xts, zoo), index = structure(c(1270072800,
 1270418400, 1270504800, 1270591200, 1270677600, 1270764000), tzone =
 , tclass = Date), .Dim = c(6L,
 1L), .Dimnames = list(NULL, SCHB.Close))
  merge(a,b)
  merge(a,b)
ZWD.UGX SCHB.Close
 2010-04-01  NA  28.02
 2010-04-01  7.6343 NA
 2010-04-02  7.6343 NA
 2010-04-03  7.5458 NA
 2010-04-04  7.4532 NA
 2010-04-05  NA  28.30
 2010-04-05  7.4040 NA
 2010-04-06  NA  28.38
 2010-04-06  7.3317 NA
 2010-04-07  NA  28.21
 2010-04-08  NA  28.31
 2010-04-09  NA  28.47
  q()

I get slightly different result

 xx-(merge(a,b))
 xx
   ZWD.UGX SCHB.Close
1270072800  NA  28.02
1270105200  7.6343 NA
1270191600  7.6343 NA
1270278000  7.5458 NA
1270364400  7.4532 NA
1270418400  NA  28.30
1270450800  7.4040 NA
1270504800  NA  28.38
1270537200  7.3317 NA
1270591200  NA  28.21
1270677600  NA  28.31
1270764000  NA  28.47

but from what you want it seems to me that aggregate could be suitable 
second step.

something like

aggregate(xx, list(those dates you have but I don't), mean, na.rm=T)

could do what you want.

Regards
Petr



 
 So, no, merge doesn't work...
 -- 
 Sent from my mobile device
 Envoyait de mon telephone mobil
 
 __
 R-help@r-project.org mailing list
 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
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] cbind giving NA's?

2011-08-26 Thread Dennis Murphy
Hi:

Try this:

require('xts')
 merge.zoo(zoo(a), zoo(b), all = c(TRUE, TRUE))
   ZWD.UGX SCHB.Close
2010-03-31  NA  28.02
2010-04-01  7.6343 NA
2010-04-02  7.6343 NA
2010-04-03  7.5458 NA
2010-04-04  7.4532  28.30
2010-04-05  7.4040  28.38
2010-04-06  7.3317  28.21
2010-04-07  NA  28.31
2010-04-08  NA  28.47

HTH,
Dennis

On Fri, Aug 26, 2011 at 3:58 AM, Hasan Diwan hasan.di...@gmail.com wrote:
 On 26 August 2011 03:37, R. Michael Weylandt michael.weyla...@gmail.com 
 wrote:
 If you could, dput() them so we can see everything about them. You also
 might see if merge() gives you more expected behavior

 Ok...
 dput(a)
 structure(c(7.6343, 7.6343, 7.5458, 7.4532, 7.404, 7.3317), class = c(xts,
 zoo), .indexCLASS = Date, .indexTZ = , index = structure(c(1270105200,
 1270191600, 1270278000, 1270364400, 1270450800, 1270537200), tzone =
 , tclass = Date), .Dim = c(6L,
 1L), .Dimnames = list(NULL, ZWD.UGX))
 dput(b)
 structure(c(28.02, 28.3, 28.38, 28.21, 28.31, 28.47), .indexCLASS =
 Date, .indexTZ = , src = yahoo, updated =
 structure(1314356091.21457, class = c(POSIXct,
 POSIXt)), class = c(xts, zoo), index = structure(c(1270072800,
 1270418400, 1270504800, 1270591200, 1270677600, 1270764000), tzone =
 , tclass = Date), .Dim = c(6L,
 1L), .Dimnames = list(NULL, SCHB.Close))
 merge(a,b)
 merge(a,b)
           ZWD.UGX SCHB.Close
 2010-04-01      NA      28.02
 2010-04-01  7.6343         NA
 2010-04-02  7.6343         NA
 2010-04-03  7.5458         NA
 2010-04-04  7.4532         NA
 2010-04-05      NA      28.30
 2010-04-05  7.4040         NA
 2010-04-06      NA      28.38
 2010-04-06  7.3317         NA
 2010-04-07      NA      28.21
 2010-04-08      NA      28.31
 2010-04-09      NA      28.47
 q()

 So, no, merge doesn't work...
 --
 Sent from my mobile device
 Envoyait de mon telephone mobil

 __
 R-help@r-project.org mailing list
 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
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] cbind giving NA's?

2011-08-26 Thread Petr PIKAL
I was rather too quick

It has probably something to do with versions of zoo and xts

after updating to zoo 1.7.4 and xts 0.8.2 I got with your examples

 merge(a,b)
   ZWD.UGX SCHB.Close
2010-04-01  NA  28.02
2010-04-01  7.6343 NA
2010-04-02  7.6343 NA
2010-04-03  7.5458 NA
2010-04-04  7.4532 NA
2010-04-05  NA  28.30
2010-04-05  7.4040 NA
2010-04-06  NA  28.38
2010-04-06  7.3317 NA
2010-04-07  NA  28.21
2010-04-08  NA  28.31
2010-04-09  NA  28.47

but when I did

 a-as.zoo(a)
 b-as.zoo(b)

I got

 merge(a,b)
   ZWD.UGX SCHB.Close
2010-04-01  7.6343  28.02
2010-04-02  7.6343 NA
2010-04-03  7.5458 NA
2010-04-04  7.4532 NA
2010-04-05  7.4040  28.30
2010-04-06  7.3317  28.38
2010-04-07  NA  28.21
2010-04-08  NA  28.31
2010-04-09  NA  28.47

which is probably what you want.

Regards
Petr

 

 Hi
 
  
  On 26 August 2011 03:37, R. Michael Weylandt 
michael.weyla...@gmail.com wrote:
   If you could, dput() them so we can see everything about them. You 
also
   might see if merge() gives you more expected behavior
  
  Ok...
   dput(a)
  structure(c(7.6343, 7.6343, 7.5458, 7.4532, 7.404, 7.3317), class = 
c(xts,
  zoo), .indexCLASS = Date, .indexTZ = , index = 
structure(c(1270105200,
  1270191600, 1270278000, 1270364400, 1270450800, 1270537200), tzone =
  , tclass = Date), .Dim = c(6L,
  1L), .Dimnames = list(NULL, ZWD.UGX))
   dput(b)
  structure(c(28.02, 28.3, 28.38, 28.21, 28.31, 28.47), .indexCLASS =
  Date, .indexTZ = , src = yahoo, updated =
  structure(1314356091.21457, class = c(POSIXct,
  POSIXt)), class = c(xts, zoo), index = structure(c(1270072800,
  1270418400, 1270504800, 1270591200, 1270677600, 1270764000), tzone =
  , tclass = Date), .Dim = c(6L,
  1L), .Dimnames = list(NULL, SCHB.Close))
   merge(a,b)
   merge(a,b)
 ZWD.UGX SCHB.Close
  2010-04-01  NA  28.02
  2010-04-01  7.6343 NA
  2010-04-02  7.6343 NA
  2010-04-03  7.5458 NA
  2010-04-04  7.4532 NA
  2010-04-05  NA  28.30
  2010-04-05  7.4040 NA
  2010-04-06  NA  28.38
  2010-04-06  7.3317 NA
  2010-04-07  NA  28.21
  2010-04-08  NA  28.31
  2010-04-09  NA  28.47
   q()

 I get slightly different result
 
  xx-(merge(a,b))
  xx
ZWD.UGX SCHB.Close
 1270072800  NA  28.02
 1270105200  7.6343 NA
 1270191600  7.6343 NA
 1270278000  7.5458 NA
 1270364400  7.4532 NA
 1270418400  NA  28.30
 1270450800  7.4040 NA
 1270504800  NA  28.38
 1270537200  7.3317 NA
 1270591200  NA  28.21
 1270677600  NA  28.31
 1270764000  NA  28.47
 
 but from what you want it seems to me that aggregate could be suitable 
second step.
 
 something like
 
 aggregate(xx, list(those dates you have but I don't), mean, na.rm=T)
 
 could do what you want.
 
 Regards
 Petr
 
  
  So, no, merge doesn't work...
  -- 
  Sent from my mobile device
  Envoyait de mon telephone mobil
  
  __
  R-help@r-project.org mailing list
  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
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] cbind giving NA's?

2011-08-26 Thread R. Michael Weylandt
This seems to be the easiest way to handle the problem:

 a = xts(coredata(a), time(a))
 b = xts(coredata(b), time(b))
 merge(a,b)


   ZWD.UGX SCHB.Close
2010-03-31  NA  28.02
2010-04-01  7.6343 NA
2010-04-02  7.6343 NA
2010-04-03  7.5458 NA
2010-04-04  7.4532  28.30
2010-04-05  7.4040  28.38
2010-04-06  7.3317  28.21
2010-04-07  NA  28.31
2010-04-08  NA  28.47

I can't be sure where the problem was coming from, but if you look
closely into the mumbo jumbo of the dput() code you'll see that the
time indices don't actually match anywhere as POSIXct/POSIXt objects.
Even though they map to the same day, something or other is keeping
them as internally different and it's probably inherited from the data
sources -- one set maps to times captured at 2300 EDT on my machine
and the other to 0800 EDT. If you want, it might be interesting to
track down the difference, but the fix I gave above, which basically
pulls the time data out through the xts class and only keeps the date
in creating a new xts that's exactly the same should handle it.

In short, it's all black magic derived from R's various time/date
mechanisms. If you don't


really want to see whats under the hood, this will also work in this case.


 a = as.xts(a)
 b = as.xts(b)
 merge(a, b)

But this can lead to some strange code if you don't take a moment to think
about it

 is.xts(a)
TRUE

 identical(a, as.xts(a))
FALSE

Hope this helps and thanks for the fun problem!

Michael Weylandt




On Fri, Aug 26, 2011 at 8:08 AM, Petr PIKAL petr.pi...@precheza.cz wrote:

 I was rather too quick

 It has probably something to do with versions of zoo and xts

 after updating to zoo 1.7.4 and xts 0.8.2 I got with your examples

  merge(a,b)
   ZWD.UGX SCHB.Close
 2010-04-01  NA  28.02
 2010-04-01  7.6343 NA
 2010-04-02  7.6343 NA
 2010-04-03  7.5458 NA
 2010-04-04  7.4532 NA
 2010-04-05  NA  28.30
 2010-04-05  7.4040 NA
 2010-04-06  NA  28.38
 2010-04-06  7.3317 NA
 2010-04-07  NA  28.21
 2010-04-08  NA  28.31
 2010-04-09  NA  28.47

 but when I did

  a-as.zoo(a)
  b-as.zoo(b)

 I got

  merge(a,b)
   ZWD.UGX SCHB.Close
 2010-04-01  7.6343  28.02
 2010-04-02  7.6343 NA
 2010-04-03  7.5458 NA
 2010-04-04  7.4532 NA
 2010-04-05  7.4040  28.30
 2010-04-06  7.3317  28.38
 2010-04-07  NA  28.21
 2010-04-08  NA  28.31
 2010-04-09  NA  28.47

 which is probably what you want.

 Regards
 Petr

 

  Hi
 
  
   On 26 August 2011 03:37, R. Michael Weylandt
 michael.weyla...@gmail.com wrote:
If you could, dput() them so we can see everything about them. You
 also
might see if merge() gives you more expected behavior
  
   Ok...
dput(a)
   structure(c(7.6343, 7.6343, 7.5458, 7.4532, 7.404, 7.3317), class =
 c(xts,
   zoo), .indexCLASS = Date, .indexTZ = , index =
 structure(c(1270105200,
   1270191600, 1270278000, 1270364400, 1270450800, 1270537200), tzone =
   , tclass = Date), .Dim = c(6L,
   1L), .Dimnames = list(NULL, ZWD.UGX))
dput(b)
   structure(c(28.02, 28.3, 28.38, 28.21, 28.31, 28.47), .indexCLASS =
   Date, .indexTZ = , src = yahoo, updated =
   structure(1314356091.21457, class = c(POSIXct,
   POSIXt)), class = c(xts, zoo), index = structure(c(1270072800,
   1270418400, 1270504800, 1270591200, 1270677600, 1270764000), tzone =
   , tclass = Date), .Dim = c(6L,
   1L), .Dimnames = list(NULL, SCHB.Close))
merge(a,b)
merge(a,b)
  ZWD.UGX SCHB.Close
   2010-04-01  NA  28.02
   2010-04-01  7.6343 NA
   2010-04-02  7.6343 NA
   2010-04-03  7.5458 NA
   2010-04-04  7.4532 NA
   2010-04-05  NA  28.30
   2010-04-05  7.4040 NA
   2010-04-06  NA  28.38
   2010-04-06  7.3317 NA
   2010-04-07  NA  28.21
   2010-04-08  NA  28.31
   2010-04-09  NA  28.47
q()

  I get slightly different result
 
   xx-(merge(a,b))
   xx
 ZWD.UGX SCHB.Close
  1270072800  NA  28.02
  1270105200  7.6343 NA
  1270191600  7.6343 NA
  1270278000  7.5458 NA
  1270364400  7.4532 NA
  1270418400  NA  28.30
  1270450800  7.4040 NA
  1270504800  NA  28.38
  1270537200  7.3317 NA
  1270591200  NA  28.21
  1270677600  NA  28.31
  1270764000  NA  28.47
 
  but from what you want it seems to me that aggregate could be suitable
 second step.
 
  something like
 
  aggregate(xx, list(those dates you have but I don't), mean, na.rm=T)
 
  could do what you want.
 
  Regards
  Petr
 
  
   So, no, merge doesn't work...
   --
   Sent from my mobile device
   Envoyait de mon telephone mobil
  
   __
   R-help@r-project.org mailing list
   https://stat.ethz.ch/mailman/listinfo/r-help
   PLEASE do 

Re: [R] cbind in aggregate formula - based on an existing object (vector)

2011-07-15 Thread peter dalgaard
For a little lateral thinking, consider the use of . on the LHS. That could 
play out as follows:

 myvars - c(Ozone,Wind)
 f - . ~ Month
 j - union(all.vars(f[[3]]), myvars)
 aggregate(. ~ Month, data=airquality[j], mean, na.rm=T)
  MonthOzone  Wind
1 5 23.61538 11.457692
2 6 29.4 12.18
3 7 59.11538  8.523077
4 8 59.96154  8.565385
5 9 31.44828 10.075862

(and of course, when you play with something unusual, a buglet pops up: it 
doesn't work with f instead of the explicit formula in the call to aggregate.)


On Jul 15, 2011, at 00:10 , Dennis Murphy wrote:

 Hi:
 
 I think Bill's got the right idea for your problem, but for the fun of
 it, here's how Bert's suggestion would play out:
 
 # Kind of works, but only for the first variable in myvars...
 aggregate(get(myvars) ~ group + mydate, FUN = sum, data = example)
   group mydate get(myvars)
 1 group1 2008-12-01   4
 2 group2 2008-12-01   6
 3 group1 2009-01-01  40
 4 group2 2009-01-01  60
 5 group1 2009-02-01 400
 6 group2 2009-02-01 600
 
 # Maybe sapply() with get as the function will work...
 aggregate(sapply(myvars, get) ~ group + mydate, FUN = sum, data = example)
   group mydate myvars   get
 1 group1 2008-12-01  4   4.2
 2 group2 2008-12-01  6   6.2
 3 group1 2009-01-01 40  40.2
 4 group2 2009-01-01 60  60.2
 5 group1 2009-02-01400 400.2
 6 group2 2009-02-01600 600.2
 
 Apart from the variable names, it matches example.agg1. OTOH, Bill's
 suggestion matches example.agg1 exactly and has an advantage in terms
 of code clarity:
 
 byVars - c('group', 'mydate')
 aggregate(example[myvars], by = example[byVars], FUN = sum)
   group mydate value1 value2
 1 group1 2008-12-01  44.2
 2 group2 2008-12-01  66.2
 3 group1 2009-01-01 40   40.2
 4 group2 2009-01-01 60   60.2
 5 group1 2009-02-01400  400.2
 6 group2 2009-02-01600  600.2
 
 FWIW,
 Dennis
 
 On Thu, Jul 14, 2011 at 12:05 PM, Dimitri Liakhovitski
 dimitri.liakhovit...@gmail.com wrote:
 Hello!
 
 I am aggregating using a formula in aggregate - of the type:
 aggregate(cbind(var1,var2,var3)~factor1+factor2,sum,data=mydata)
 
 However, I actually have an object (vector of my variables to be aggregated):
 myvars-c(var1,var2,var3)
 
 I'd like my aggregate formula (its cbind part) to be able to use my
 myvars object. Is it possible?
 Thanks for your help!
 
 Dimitri
 
 Reproducible example:
 
 mydate = rep(seq(as.Date(2008-12-01), length = 3, by = month),4)
 value1=c(1,10,100,2,20,200,3,30,300,4,40,400)
 value2=c(1.1,10.1,100.1,2.1,20.1,200.1,3.1,30.1,300.1,4.1,40.1,400.1)
 
 example-data.frame(mydate=mydate,value1=value1,value2=value2)
 example$group-c(rep(group1,3),rep(group2,3),rep(group1,3),rep(group2,3))
 example$group-as.factor(example$group)
 (example);str(example)
 
 example.agg1-aggregate(cbind(value1,value2)~group+mydate,sum,data=example)
 # this works
 (example.agg1)
 
 ### Building my object (vector of 2 names - in reality, many more):
 myvars-c(value1,value2)
 example.agg1-aggregate(cbind(myvars)~group+mydate,sum,data=example)
 ### does not work
 
 
 --
 Dimitri Liakhovitski
 Ninah Consulting
 www.ninah.com
 
 __
 R-help@r-project.org mailing list
 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
 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.

-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
R-help@r-project.org mailing list
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] cbind in aggregate formula - based on an existing object (vector)

2011-07-15 Thread Dimitri Liakhovitski
THAT'S IT, Bill - exactly what I was looking for! Thanks a lot for the
input, everyone.
I find the by method the most straigtfoward and clear.
Dimitri


On Thu, Jul 14, 2011 at 5:12 PM, William Dunlap wdun...@tibco.com wrote:
 You may find it easier to use the data.frame method for aggregate
 instead of the formula method when you are using vectors of column
 names.   E.g.,

  responseVars - c(mpg, wt)
  byVars - c(cyl, gear)
  aggregate(mtcars[responseVars], by=mtcars[byVars], FUN=median)

 gives the same result as

  aggregate(cbind(mpg, wt) ~ cyl + gear, FUN=median, data=mtcars)

 Bill Dunlap
 Spotfire, TIBCO Software
 wdunlap tibco.com

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
 Behalf Of Dimitri Liakhovitski
 Sent: Thursday, July 14, 2011 1:45 PM
 To: David Winsemius
 Cc: r-help
 Subject: Re: [R] cbind in aggregate formula - based on an existing object 
 (vector)

 Thanks a lot!

 actually, what I tried to do is very simple - just passing tons of
 variable names into the formula. Maybe that get thing suggested by
 Bert would work...

 Dimitri


 On Thu, Jul 14, 2011 at 4:01 PM, David Winsemius dwinsem...@comcast.net 
 wrote:
 Dmitri:

 as.matrix makes a matrix out of the dataframe that is passed to it.

 As a further note I attempted and failed for reasons that are unclear to me
 to construct a formula that would (I hoped) preserve the column names which
 are being mangle in the posted effort:

 form - as.formula(paste(
                     cbind(,
                      paste( myvars, collapse=,),
                      ) ~ group+mydate,
                      sep= ) )
 myvars-c(value1,value2)
 example.agg1-aggregate(formula=form,data=example, FUN=sum)
 Error in m[[2L]][[2L]] : object of type 'symbol' is not subsettable
 traceback()
 2: aggregate.formula(formula = form, data = example, FUN = sum)
 1: aggregate(formula = form, data = example, FUN = sum)

 form
 cbind(value1, value2) ~ group + mydate
 parse(text=form)
 expression(~
 cbind(value1, value2), group + mydate)

 So it seems to be correctly dispatched to aggregate.formula but not passing
 some check or another. Also tried with formula() rather than as.formula with
 identical error message. Also tried including without naming the argument.

 --
 David


 On Jul 14, 2011, at 3:32 PM, Dimitri Liakhovitski wrote:

 Thank you, David, it does work.
 Could you please explain why? What exactly does changing it to as matrix
 do?
 Thank you!
 Dimitri

 On Thu, Jul 14, 2011 at 3:25 PM, David Winsemius dwinsem...@comcast.net
 wrote:

 On Jul 14, 2011, at 3:05 PM, Dimitri Liakhovitski wrote:

 Hello!

 I am aggregating using a formula in aggregate - of the type:
 aggregate(cbind(var1,var2,var3)~factor1+factor2,sum,data=mydata)

 However, I actually have an object (vector of my variables to be
 aggregated):
 myvars-c(var1,var2,var3)

 I'd like my aggregate formula (its cbind part) to be able to use my
 myvars object. Is it possible?
 Thanks for your help!


 Not sure I have gotten all the way there, but this does work:


 example.agg1-aggregate(as.matrix(example[myvars])~group+mydate,sum,data=example)

 example.agg1

  group     mydate example[myvars]    NA
 1 group1 2008-12-01               4   4.2
 2 group2 2008-12-01               6   6.2
 3 group1 2009-01-01              40  40.2
 4 group2 2009-01-01              60  60.2
 5 group1 2009-02-01             400 400.2
 6 group2 2009-02-01             600 600.2

 Dimitri

 Reproducible example:

 mydate = rep(seq(as.Date(2008-12-01), length = 3, by = month),4)
 value1=c(1,10,100,2,20,200,3,30,300,4,40,400)
 value2=c(1.1,10.1,100.1,2.1,20.1,200.1,3.1,30.1,300.1,4.1,40.1,400.1)

 example-data.frame(mydate=mydate,value1=value1,value2=value2)


 example$group-c(rep(group1,3),rep(group2,3),rep(group1,3),rep(group2,3))
 example$group-as.factor(example$group)
 (example);str(example)



 example.agg1-aggregate(cbind(value1,value2)~group+mydate,sum,data=example)
 # this works
 (example.agg1)

 ### Building my object (vector of 2 names - in reality, many more):
 myvars-c(value1,value2)
 example.agg1-aggregate(cbind(myvars)~group+mydate,sum,data=example)
 ### does not work


 --
 Dimitri Liakhovitski
 Ninah Consulting
 www.ninah.com

 __
 R-help@r-project.org mailing list
 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, MD
 West Hartford, CT





 --
 Dimitri Liakhovitski
 Ninah Consulting
 www.ninah.com

 David Winsemius, MD
 West Hartford, CT





 --
 Dimitri Liakhovitski
 Ninah Consulting
 www.ninah.com

 __
 R-help@r-project.org mailing list
 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

Re: [R] cbind in aggregate formula - based on an existing object (vector)

2011-07-15 Thread peter dalgaard

On Jul 15, 2011, at 15:06 , peter dalgaard wrote:

 For a little lateral thinking, consider the use of . on the LHS. That could 
 play out as follows:
 
 myvars - c(Ozone,Wind)
 f - . ~ Month
 j - union(all.vars(f[[3]]), myvars)
 aggregate(. ~ Month, data=airquality[j], mean, na.rm=T)
  MonthOzone  Wind
 1 5 23.61538 11.457692
 2 6 29.4 12.18
 3 7 59.11538  8.523077
 4 8 59.96154  8.565385
 5 9 31.44828 10.075862
 
 (and of course, when you play with something unusual, a buglet pops up: it 
 doesn't work with f instead of the explicit formula in the call to aggregate.)
 


...however, once you go down that road, you might as well construct the LHS 
directly:

 lhs - as.call(lapply(c(cbind, myvars), as.name))
 eval(bquote(aggregate(.(lhs) ~ Month, data=airquality, mean, na.rm=T)))
  MonthOzone  Wind
1 5 23.61538 11.457692
2 6 29.4 12.18
3 7 59.11538  8.523077
4 8 59.96154  8.565385
5 9 31.44828 10.075862


-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
R-help@r-project.org mailing list
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] cbind in aggregate formula - based on an existing object (vector)

2011-07-14 Thread Dimitri Liakhovitski
Hello!

I am aggregating using a formula in aggregate - of the type:
aggregate(cbind(var1,var2,var3)~factor1+factor2,sum,data=mydata)

However, I actually have an object (vector of my variables to be aggregated):
myvars-c(var1,var2,var3)

I'd like my aggregate formula (its cbind part) to be able to use my
myvars object. Is it possible?
Thanks for your help!

Dimitri

Reproducible example:

mydate = rep(seq(as.Date(2008-12-01), length = 3, by = month),4)
value1=c(1,10,100,2,20,200,3,30,300,4,40,400)
value2=c(1.1,10.1,100.1,2.1,20.1,200.1,3.1,30.1,300.1,4.1,40.1,400.1)

example-data.frame(mydate=mydate,value1=value1,value2=value2)
example$group-c(rep(group1,3),rep(group2,3),rep(group1,3),rep(group2,3))
example$group-as.factor(example$group)
(example);str(example)

example.agg1-aggregate(cbind(value1,value2)~group+mydate,sum,data=example)
# this works
(example.agg1)

### Building my object (vector of 2 names - in reality, many more):
myvars-c(value1,value2)
example.agg1-aggregate(cbind(myvars)~group+mydate,sum,data=example)
### does not work


-- 
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com

__
R-help@r-project.org mailing list
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] cbind in aggregate formula - based on an existing object (vector)

2011-07-14 Thread David Winsemius


On Jul 14, 2011, at 3:05 PM, Dimitri Liakhovitski wrote:


Hello!

I am aggregating using a formula in aggregate - of the type:
aggregate(cbind(var1,var2,var3)~factor1+factor2,sum,data=mydata)

However, I actually have an object (vector of my variables to be  
aggregated):

myvars-c(var1,var2,var3)

I'd like my aggregate formula (its cbind part) to be able to use my
myvars object. Is it possible?
Thanks for your help!



Not sure I have gotten all the way there, but this does work:

example.agg1-aggregate(as.matrix(example[myvars])~group 
+mydate,sum,data=example)


 example.agg1
   group mydate example[myvars]NA
1 group1 2008-12-01   4   4.2
2 group2 2008-12-01   6   6.2
3 group1 2009-01-01  40  40.2
4 group2 2009-01-01  60  60.2
5 group1 2009-02-01 400 400.2
6 group2 2009-02-01 600 600.2


Dimitri

Reproducible example:

mydate = rep(seq(as.Date(2008-12-01), length = 3, by = month),4)
value1=c(1,10,100,2,20,200,3,30,300,4,40,400)
value2=c(1.1,10.1,100.1,2.1,20.1,200.1,3.1,30.1,300.1,4.1,40.1,400.1)

example-data.frame(mydate=mydate,value1=value1,value2=value2)
example$group-c(rep(group1,3),rep(group2,3),rep(group1, 
3),rep(group2,3))

example$group-as.factor(example$group)
(example);str(example)

example.agg1-aggregate(cbind(value1,value2)~group 
+mydate,sum,data=example)

# this works
(example.agg1)

### Building my object (vector of 2 names - in reality, many more):
myvars-c(value1,value2)
example.agg1-aggregate(cbind(myvars)~group+mydate,sum,data=example)
### does not work


--
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com

__
R-help@r-project.org mailing list
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, MD
West Hartford, CT

__
R-help@r-project.org mailing list
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] cbind in aggregate formula - based on an existing object (vector)

2011-07-14 Thread Dimitri Liakhovitski
Thank you, David, it does work.
Could you please explain why? What exactly does changing it to as matrix do?
Thank you!
Dimitri

On Thu, Jul 14, 2011 at 3:25 PM, David Winsemius dwinsem...@comcast.net wrote:

 On Jul 14, 2011, at 3:05 PM, Dimitri Liakhovitski wrote:

 Hello!

 I am aggregating using a formula in aggregate - of the type:
 aggregate(cbind(var1,var2,var3)~factor1+factor2,sum,data=mydata)

 However, I actually have an object (vector of my variables to be
 aggregated):
 myvars-c(var1,var2,var3)

 I'd like my aggregate formula (its cbind part) to be able to use my
 myvars object. Is it possible?
 Thanks for your help!


 Not sure I have gotten all the way there, but this does work:

 example.agg1-aggregate(as.matrix(example[myvars])~group+mydate,sum,data=example)

 example.agg1
   group     mydate example[myvars]    NA
 1 group1 2008-12-01               4   4.2
 2 group2 2008-12-01               6   6.2
 3 group1 2009-01-01              40  40.2
 4 group2 2009-01-01              60  60.2
 5 group1 2009-02-01             400 400.2
 6 group2 2009-02-01             600 600.2

 Dimitri

 Reproducible example:

 mydate = rep(seq(as.Date(2008-12-01), length = 3, by = month),4)
 value1=c(1,10,100,2,20,200,3,30,300,4,40,400)
 value2=c(1.1,10.1,100.1,2.1,20.1,200.1,3.1,30.1,300.1,4.1,40.1,400.1)

 example-data.frame(mydate=mydate,value1=value1,value2=value2)

 example$group-c(rep(group1,3),rep(group2,3),rep(group1,3),rep(group2,3))
 example$group-as.factor(example$group)
 (example);str(example)


 example.agg1-aggregate(cbind(value1,value2)~group+mydate,sum,data=example)
 # this works
 (example.agg1)

 ### Building my object (vector of 2 names - in reality, many more):
 myvars-c(value1,value2)
 example.agg1-aggregate(cbind(myvars)~group+mydate,sum,data=example)
 ### does not work


 --
 Dimitri Liakhovitski
 Ninah Consulting
 www.ninah.com

 __
 R-help@r-project.org mailing list
 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, MD
 West Hartford, CT





-- 
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com

__
R-help@r-project.org mailing list
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] cbind in aggregate formula - based on an existing object (vector)

2011-07-14 Thread Bert Gunter
Dmitri:

Look at my vars from myvars-c(value1,value2)
It's just a character vector of length 2!

You can't cbind a character vector of length 2! These are not
references/pointers.

It's not at all clear to me what you ultimately want to do, but IF
it's: pass a character vector of names to be used as the LHS of the
aggregate .formula call, then something like: (untested)

MyVars - do.call(cbind, lapply(myvars,get))

and then

aggregate(MyVars ~ ...)

might do. But there are so many potential scoping problems here that I
would not be surprised if it failed. The usual advice for this sort of
thing is to use substitute() or maybe the dreaded eval(parse(...))
construction -- but as I said, I don't really understand what you're
after.

-- Bert



On Thu, Jul 14, 2011 at 12:32 PM, Dimitri Liakhovitski
dimitri.liakhovit...@gmail.com wrote:
 Thank you, David, it does work.
 Could you please explain why? What exactly does changing it to as matrix do?
 Thank you!
 Dimitri

 On Thu, Jul 14, 2011 at 3:25 PM, David Winsemius dwinsem...@comcast.net 
 wrote:

 On Jul 14, 2011, at 3:05 PM, Dimitri Liakhovitski wrote:

 Hello!

 I am aggregating using a formula in aggregate - of the type:
 aggregate(cbind(var1,var2,var3)~factor1+factor2,sum,data=mydata)

 However, I actually have an object (vector of my variables to be
 aggregated):
 myvars-c(var1,var2,var3)

 I'd like my aggregate formula (its cbind part) to be able to use my
 myvars object. Is it possible?
 Thanks for your help!


 Not sure I have gotten all the way there, but this does work:

 example.agg1-aggregate(as.matrix(example[myvars])~group+mydate,sum,data=example)

 example.agg1
   group     mydate example[myvars]    NA
 1 group1 2008-12-01               4   4.2
 2 group2 2008-12-01               6   6.2
 3 group1 2009-01-01              40  40.2
 4 group2 2009-01-01              60  60.2
 5 group1 2009-02-01             400 400.2
 6 group2 2009-02-01             600 600.2

 Dimitri

 Reproducible example:

 mydate = rep(seq(as.Date(2008-12-01), length = 3, by = month),4)
 value1=c(1,10,100,2,20,200,3,30,300,4,40,400)
 value2=c(1.1,10.1,100.1,2.1,20.1,200.1,3.1,30.1,300.1,4.1,40.1,400.1)

 example-data.frame(mydate=mydate,value1=value1,value2=value2)

 example$group-c(rep(group1,3),rep(group2,3),rep(group1,3),rep(group2,3))
 example$group-as.factor(example$group)
 (example);str(example)


 example.agg1-aggregate(cbind(value1,value2)~group+mydate,sum,data=example)
 # this works
 (example.agg1)

 ### Building my object (vector of 2 names - in reality, many more):
 myvars-c(value1,value2)
 example.agg1-aggregate(cbind(myvars)~group+mydate,sum,data=example)
 ### does not work


 --
 Dimitri Liakhovitski
 Ninah Consulting
 www.ninah.com

 __
 R-help@r-project.org mailing list
 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, MD
 West Hartford, CT





 --
 Dimitri Liakhovitski
 Ninah Consulting
 www.ninah.com

 __
 R-help@r-project.org mailing list
 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.




-- 
Men by nature long to get on to the ultimate truths, and will often
be impatient with elementary studies or fight shy of them. If it were
possible to reach the ultimate truths without the elementary studies
usually prefixed to them, these would not be preparatory studies but
superfluous diversions.

-- Maimonides (1135-1204)

Bert Gunter
Genentech Nonclinical Biostatistics
467-7374
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
R-help@r-project.org mailing list
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] cbind in aggregate formula - based on an existing object (vector)

2011-07-14 Thread David Winsemius

Dmitri:

as.matrix makes a matrix out of the dataframe that is passed to it.

As a further note I attempted and failed for reasons that are unclear  
to me to construct a formula that would (I hoped) preserve the column  
names which are being mangle in the posted effort:


form - as.formula(paste(
 cbind(,
  paste( myvars, collapse=,),
  ) ~ group+mydate,
  sep= ) )
 myvars-c(value1,value2)
 example.agg1-aggregate(formula=form,data=example, FUN=sum)
Error in m[[2L]][[2L]] : object of type 'symbol' is not subsettable
 traceback()
2: aggregate.formula(formula = form, data = example, FUN = sum)
1: aggregate(formula = form, data = example, FUN = sum)

 form
cbind(value1, value2) ~ group + mydate
 parse(text=form)
expression(~
cbind(value1, value2), group + mydate)

So it seems to be correctly dispatched to aggregate.formula but not  
passing some check or another. Also tried with formula() rather than  
as.formula with identical error message. Also tried including without  
naming the argument.


--
David


On Jul 14, 2011, at 3:32 PM, Dimitri Liakhovitski wrote:


Thank you, David, it does work.
Could you please explain why? What exactly does changing it to as  
matrix do?

Thank you!
Dimitri

On Thu, Jul 14, 2011 at 3:25 PM, David Winsemius dwinsem...@comcast.net 
 wrote:


On Jul 14, 2011, at 3:05 PM, Dimitri Liakhovitski wrote:


Hello!

I am aggregating using a formula in aggregate - of the type:
aggregate(cbind(var1,var2,var3)~factor1+factor2,sum,data=mydata)

However, I actually have an object (vector of my variables to be
aggregated):
myvars-c(var1,var2,var3)

I'd like my aggregate formula (its cbind part) to be able to use  
my

myvars object. Is it possible?
Thanks for your help!



Not sure I have gotten all the way there, but this does work:

example.agg1-aggregate(as.matrix(example[myvars])~group 
+mydate,sum,data=example)



example.agg1

  group mydate example[myvars]NA
1 group1 2008-12-01   4   4.2
2 group2 2008-12-01   6   6.2
3 group1 2009-01-01  40  40.2
4 group2 2009-01-01  60  60.2
5 group1 2009-02-01 400 400.2
6 group2 2009-02-01 600 600.2


Dimitri

Reproducible example:

mydate = rep(seq(as.Date(2008-12-01), length = 3, by = month),4)
value1=c(1,10,100,2,20,200,3,30,300,4,40,400)
value2 
=c(1.1,10.1,100.1,2.1,20.1,200.1,3.1,30.1,300.1,4.1,40.1,400.1)


example-data.frame(mydate=mydate,value1=value1,value2=value2)

example$group-c(rep(group1,3),rep(group2,3),rep(group1, 
3),rep(group2,3))

example$group-as.factor(example$group)
(example);str(example)


example.agg1-aggregate(cbind(value1,value2)~group 
+mydate,sum,data=example)

# this works
(example.agg1)

### Building my object (vector of 2 names - in reality, many more):
myvars-c(value1,value2)
example.agg1-aggregate(cbind(myvars)~group+mydate,sum,data=example)
### does not work


--
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com

__
R-help@r-project.org mailing list
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, MD
West Hartford, CT






--
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com


David Winsemius, MD
West Hartford, CT

__
R-help@r-project.org mailing list
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] cbind in aggregate formula - based on an existing object (vector)

2011-07-14 Thread Dimitri Liakhovitski
Thanks a lot!

actually, what I tried to do is very simple - just passing tons of
variable names into the formula. Maybe that get thing suggested by
Bert would work...

Dimitri


On Thu, Jul 14, 2011 at 4:01 PM, David Winsemius dwinsem...@comcast.net wrote:
 Dmitri:

 as.matrix makes a matrix out of the dataframe that is passed to it.

 As a further note I attempted and failed for reasons that are unclear to me
 to construct a formula that would (I hoped) preserve the column names which
 are being mangle in the posted effort:

 form - as.formula(paste(
                     cbind(,
                      paste( myvars, collapse=,),
                      ) ~ group+mydate,
                      sep= ) )
 myvars-c(value1,value2)
 example.agg1-aggregate(formula=form,data=example, FUN=sum)
 Error in m[[2L]][[2L]] : object of type 'symbol' is not subsettable
 traceback()
 2: aggregate.formula(formula = form, data = example, FUN = sum)
 1: aggregate(formula = form, data = example, FUN = sum)

 form
 cbind(value1, value2) ~ group + mydate
 parse(text=form)
 expression(~
 cbind(value1, value2), group + mydate)

 So it seems to be correctly dispatched to aggregate.formula but not passing
 some check or another. Also tried with formula() rather than as.formula with
 identical error message. Also tried including without naming the argument.

 --
 David


 On Jul 14, 2011, at 3:32 PM, Dimitri Liakhovitski wrote:

 Thank you, David, it does work.
 Could you please explain why? What exactly does changing it to as matrix
 do?
 Thank you!
 Dimitri

 On Thu, Jul 14, 2011 at 3:25 PM, David Winsemius dwinsem...@comcast.net
 wrote:

 On Jul 14, 2011, at 3:05 PM, Dimitri Liakhovitski wrote:

 Hello!

 I am aggregating using a formula in aggregate - of the type:
 aggregate(cbind(var1,var2,var3)~factor1+factor2,sum,data=mydata)

 However, I actually have an object (vector of my variables to be
 aggregated):
 myvars-c(var1,var2,var3)

 I'd like my aggregate formula (its cbind part) to be able to use my
 myvars object. Is it possible?
 Thanks for your help!


 Not sure I have gotten all the way there, but this does work:


 example.agg1-aggregate(as.matrix(example[myvars])~group+mydate,sum,data=example)

 example.agg1

  group     mydate example[myvars]    NA
 1 group1 2008-12-01               4   4.2
 2 group2 2008-12-01               6   6.2
 3 group1 2009-01-01              40  40.2
 4 group2 2009-01-01              60  60.2
 5 group1 2009-02-01             400 400.2
 6 group2 2009-02-01             600 600.2

 Dimitri

 Reproducible example:

 mydate = rep(seq(as.Date(2008-12-01), length = 3, by = month),4)
 value1=c(1,10,100,2,20,200,3,30,300,4,40,400)
 value2=c(1.1,10.1,100.1,2.1,20.1,200.1,3.1,30.1,300.1,4.1,40.1,400.1)

 example-data.frame(mydate=mydate,value1=value1,value2=value2)


 example$group-c(rep(group1,3),rep(group2,3),rep(group1,3),rep(group2,3))
 example$group-as.factor(example$group)
 (example);str(example)



 example.agg1-aggregate(cbind(value1,value2)~group+mydate,sum,data=example)
 # this works
 (example.agg1)

 ### Building my object (vector of 2 names - in reality, many more):
 myvars-c(value1,value2)
 example.agg1-aggregate(cbind(myvars)~group+mydate,sum,data=example)
 ### does not work


 --
 Dimitri Liakhovitski
 Ninah Consulting
 www.ninah.com

 __
 R-help@r-project.org mailing list
 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, MD
 West Hartford, CT





 --
 Dimitri Liakhovitski
 Ninah Consulting
 www.ninah.com

 David Winsemius, MD
 West Hartford, CT





-- 
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com

__
R-help@r-project.org mailing list
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] cbind in aggregate formula - based on an existing object (vector)

2011-07-14 Thread Dimitri Liakhovitski
David - I tried exactly the thing you did (and after that asked my
question to the forum):
 form - as.formula(paste(
 cbind(,
  paste( myvars, collapse=,),
  ) ~ group+mydate,
  sep= ) )

And it did not work - although it looks clean. At the end I ended up
writing a loop across individual variables with this code in the body:
  myformula-as.formula(paste(i,~myfactor,sep=))
  temp-aggregate(myformula,sum,data=mydata)
 ...

I then it worked. Really don't understand why pasting the cbind(...)
text does not work.
Dimitri

On Thu, Jul 14, 2011 at 4:01 PM, David Winsemius dwinsem...@comcast.net wrote:
 Dmitri:

 as.matrix makes a matrix out of the dataframe that is passed to it.

 As a further note I attempted and failed for reasons that are unclear to me
 to construct a formula that would (I hoped) preserve the column names which
 are being mangle in the posted effort:

 form - as.formula(paste(
                     cbind(,
                      paste( myvars, collapse=,),
                      ) ~ group+mydate,
                      sep= ) )
 myvars-c(value1,value2)
 example.agg1-aggregate(formula=form,data=example, FUN=sum)
 Error in m[[2L]][[2L]] : object of type 'symbol' is not subsettable
 traceback()
 2: aggregate.formula(formula = form, data = example, FUN = sum)
 1: aggregate(formula = form, data = example, FUN = sum)

 form
 cbind(value1, value2) ~ group + mydate
 parse(text=form)
 expression(~
 cbind(value1, value2), group + mydate)

 So it seems to be correctly dispatched to aggregate.formula but not passing
 some check or another. Also tried with formula() rather than as.formula with
 identical error message. Also tried including without naming the argument.

 --
 David


 On Jul 14, 2011, at 3:32 PM, Dimitri Liakhovitski wrote:

 Thank you, David, it does work.
 Could you please explain why? What exactly does changing it to as matrix
 do?
 Thank you!
 Dimitri

 On Thu, Jul 14, 2011 at 3:25 PM, David Winsemius dwinsem...@comcast.net
 wrote:

 On Jul 14, 2011, at 3:05 PM, Dimitri Liakhovitski wrote:

 Hello!

 I am aggregating using a formula in aggregate - of the type:
 aggregate(cbind(var1,var2,var3)~factor1+factor2,sum,data=mydata)

 However, I actually have an object (vector of my variables to be
 aggregated):
 myvars-c(var1,var2,var3)

 I'd like my aggregate formula (its cbind part) to be able to use my
 myvars object. Is it possible?
 Thanks for your help!


 Not sure I have gotten all the way there, but this does work:


 example.agg1-aggregate(as.matrix(example[myvars])~group+mydate,sum,data=example)

 example.agg1

  group     mydate example[myvars]    NA
 1 group1 2008-12-01               4   4.2
 2 group2 2008-12-01               6   6.2
 3 group1 2009-01-01              40  40.2
 4 group2 2009-01-01              60  60.2
 5 group1 2009-02-01             400 400.2
 6 group2 2009-02-01             600 600.2

 Dimitri

 Reproducible example:

 mydate = rep(seq(as.Date(2008-12-01), length = 3, by = month),4)
 value1=c(1,10,100,2,20,200,3,30,300,4,40,400)
 value2=c(1.1,10.1,100.1,2.1,20.1,200.1,3.1,30.1,300.1,4.1,40.1,400.1)

 example-data.frame(mydate=mydate,value1=value1,value2=value2)


 example$group-c(rep(group1,3),rep(group2,3),rep(group1,3),rep(group2,3))
 example$group-as.factor(example$group)
 (example);str(example)



 example.agg1-aggregate(cbind(value1,value2)~group+mydate,sum,data=example)
 # this works
 (example.agg1)

 ### Building my object (vector of 2 names - in reality, many more):
 myvars-c(value1,value2)
 example.agg1-aggregate(cbind(myvars)~group+mydate,sum,data=example)
 ### does not work


 --
 Dimitri Liakhovitski
 Ninah Consulting
 www.ninah.com

 __
 R-help@r-project.org mailing list
 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, MD
 West Hartford, CT





 --
 Dimitri Liakhovitski
 Ninah Consulting
 www.ninah.com

 David Winsemius, MD
 West Hartford, CT





-- 
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com

__
R-help@r-project.org mailing list
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] cbind in aggregate formula - based on an existing object (vector)

2011-07-14 Thread William Dunlap
You may find it easier to use the data.frame method for aggregate
instead of the formula method when you are using vectors of column
names.   E.g.,

  responseVars - c(mpg, wt)
  byVars - c(cyl, gear)
  aggregate(mtcars[responseVars], by=mtcars[byVars], FUN=median)

gives the same result as

  aggregate(cbind(mpg, wt) ~ cyl + gear, FUN=median, data=mtcars)

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Dimitri Liakhovitski
Sent: Thursday, July 14, 2011 1:45 PM
To: David Winsemius
Cc: r-help
Subject: Re: [R] cbind in aggregate formula - based on an existing object 
(vector)

Thanks a lot!

actually, what I tried to do is very simple - just passing tons of
variable names into the formula. Maybe that get thing suggested by
Bert would work...

Dimitri


On Thu, Jul 14, 2011 at 4:01 PM, David Winsemius dwinsem...@comcast.net wrote:
 Dmitri:

 as.matrix makes a matrix out of the dataframe that is passed to it.

 As a further note I attempted and failed for reasons that are unclear to me
 to construct a formula that would (I hoped) preserve the column names which
 are being mangle in the posted effort:

 form - as.formula(paste(
                     cbind(,
                      paste( myvars, collapse=,),
                      ) ~ group+mydate,
                      sep= ) )
 myvars-c(value1,value2)
 example.agg1-aggregate(formula=form,data=example, FUN=sum)
 Error in m[[2L]][[2L]] : object of type 'symbol' is not subsettable
 traceback()
 2: aggregate.formula(formula = form, data = example, FUN = sum)
 1: aggregate(formula = form, data = example, FUN = sum)

 form
 cbind(value1, value2) ~ group + mydate
 parse(text=form)
 expression(~
 cbind(value1, value2), group + mydate)

 So it seems to be correctly dispatched to aggregate.formula but not passing
 some check or another. Also tried with formula() rather than as.formula with
 identical error message. Also tried including without naming the argument.

 --
 David


 On Jul 14, 2011, at 3:32 PM, Dimitri Liakhovitski wrote:

 Thank you, David, it does work.
 Could you please explain why? What exactly does changing it to as matrix
 do?
 Thank you!
 Dimitri

 On Thu, Jul 14, 2011 at 3:25 PM, David Winsemius dwinsem...@comcast.net
 wrote:

 On Jul 14, 2011, at 3:05 PM, Dimitri Liakhovitski wrote:

 Hello!

 I am aggregating using a formula in aggregate - of the type:
 aggregate(cbind(var1,var2,var3)~factor1+factor2,sum,data=mydata)

 However, I actually have an object (vector of my variables to be
 aggregated):
 myvars-c(var1,var2,var3)

 I'd like my aggregate formula (its cbind part) to be able to use my
 myvars object. Is it possible?
 Thanks for your help!


 Not sure I have gotten all the way there, but this does work:


 example.agg1-aggregate(as.matrix(example[myvars])~group+mydate,sum,data=example)

 example.agg1

  group     mydate example[myvars]    NA
 1 group1 2008-12-01               4   4.2
 2 group2 2008-12-01               6   6.2
 3 group1 2009-01-01              40  40.2
 4 group2 2009-01-01              60  60.2
 5 group1 2009-02-01             400 400.2
 6 group2 2009-02-01             600 600.2

 Dimitri

 Reproducible example:

 mydate = rep(seq(as.Date(2008-12-01), length = 3, by = month),4)
 value1=c(1,10,100,2,20,200,3,30,300,4,40,400)
 value2=c(1.1,10.1,100.1,2.1,20.1,200.1,3.1,30.1,300.1,4.1,40.1,400.1)

 example-data.frame(mydate=mydate,value1=value1,value2=value2)


 example$group-c(rep(group1,3),rep(group2,3),rep(group1,3),rep(group2,3))
 example$group-as.factor(example$group)
 (example);str(example)



 example.agg1-aggregate(cbind(value1,value2)~group+mydate,sum,data=example)
 # this works
 (example.agg1)

 ### Building my object (vector of 2 names - in reality, many more):
 myvars-c(value1,value2)
 example.agg1-aggregate(cbind(myvars)~group+mydate,sum,data=example)
 ### does not work


 --
 Dimitri Liakhovitski
 Ninah Consulting
 www.ninah.com

 __
 R-help@r-project.org mailing list
 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, MD
 West Hartford, CT





 --
 Dimitri Liakhovitski
 Ninah Consulting
 www.ninah.com

 David Winsemius, MD
 West Hartford, CT





-- 
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com

__
R-help@r-project.org mailing list
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
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

Re: [R] cbind in aggregate formula - based on an existing object (vector)

2011-07-14 Thread Dennis Murphy
Hi:

I think Bill's got the right idea for your problem, but for the fun of
it, here's how Bert's suggestion would play out:

# Kind of works, but only for the first variable in myvars...
 aggregate(get(myvars) ~ group + mydate, FUN = sum, data = example)
   group mydate get(myvars)
1 group1 2008-12-01   4
2 group2 2008-12-01   6
3 group1 2009-01-01  40
4 group2 2009-01-01  60
5 group1 2009-02-01 400
6 group2 2009-02-01 600

# Maybe sapply() with get as the function will work...
 aggregate(sapply(myvars, get) ~ group + mydate, FUN = sum, data = example)
   group mydate myvars   get
1 group1 2008-12-01  4   4.2
2 group2 2008-12-01  6   6.2
3 group1 2009-01-01 40  40.2
4 group2 2009-01-01 60  60.2
5 group1 2009-02-01400 400.2
6 group2 2009-02-01600 600.2

Apart from the variable names, it matches example.agg1. OTOH, Bill's
suggestion matches example.agg1 exactly and has an advantage in terms
of code clarity:

byVars - c('group', 'mydate')
 aggregate(example[myvars], by = example[byVars], FUN = sum)
   group mydate value1 value2
1 group1 2008-12-01  44.2
2 group2 2008-12-01  66.2
3 group1 2009-01-01 40   40.2
4 group2 2009-01-01 60   60.2
5 group1 2009-02-01400  400.2
6 group2 2009-02-01600  600.2

FWIW,
Dennis

On Thu, Jul 14, 2011 at 12:05 PM, Dimitri Liakhovitski
dimitri.liakhovit...@gmail.com wrote:
 Hello!

 I am aggregating using a formula in aggregate - of the type:
 aggregate(cbind(var1,var2,var3)~factor1+factor2,sum,data=mydata)

 However, I actually have an object (vector of my variables to be aggregated):
 myvars-c(var1,var2,var3)

 I'd like my aggregate formula (its cbind part) to be able to use my
 myvars object. Is it possible?
 Thanks for your help!

 Dimitri

 Reproducible example:

 mydate = rep(seq(as.Date(2008-12-01), length = 3, by = month),4)
 value1=c(1,10,100,2,20,200,3,30,300,4,40,400)
 value2=c(1.1,10.1,100.1,2.1,20.1,200.1,3.1,30.1,300.1,4.1,40.1,400.1)

 example-data.frame(mydate=mydate,value1=value1,value2=value2)
 example$group-c(rep(group1,3),rep(group2,3),rep(group1,3),rep(group2,3))
 example$group-as.factor(example$group)
 (example);str(example)

 example.agg1-aggregate(cbind(value1,value2)~group+mydate,sum,data=example)
 # this works
 (example.agg1)

 ### Building my object (vector of 2 names - in reality, many more):
 myvars-c(value1,value2)
 example.agg1-aggregate(cbind(myvars)~group+mydate,sum,data=example)
 ### does not work


 --
 Dimitri Liakhovitski
 Ninah Consulting
 www.ninah.com

 __
 R-help@r-project.org mailing list
 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
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] cbind 3 or more matrices

2011-06-04 Thread Jim Silverton
How can I cbind three or more matrices like A,B and C. This does not work:

cbind(A,B,C)


-- 
Thanks,
Jim.

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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] cbind 3 or more matrices

2011-06-04 Thread Sarah Goslee
do.call(cbind, list(A, B, C))


On Sat, Jun 4, 2011 at 7:14 PM, Jim Silverton jim.silver...@gmail.com wrote:
 How can I cbind three or more matrices like A,B and C. This does not work:

 cbind(A,B,C)


 --
 Thanks,
 Jim.


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

__
R-help@r-project.org mailing list
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] cbind 3 or more matrices

2011-06-04 Thread baptiste auguie
A, B, C should have the same number of rows.

mlist = replicate(3, matrix(rnorm(6), 2), simplify=FALSE)
names(mlist) = LETTERS[seq_along(mlist)]
with(mlist, cbind(A,B,C))

or,

do.call(cbind, mlist)

HTH,

baptiste

On 5 June 2011 11:14, Jim Silverton jim.silver...@gmail.com wrote:
 How can I cbind three or more matrices like A,B and C. This does not work:

 cbind(A,B,C)


 --
 Thanks,
 Jim.

        [[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list
 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
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] cbind 3 or more matrices

2011-06-04 Thread Phil Spector

Jim -
   In what sense does cbind(A,B,C) not work?


A = matrix(rnorm(10),5,2)
B = matrix(rnorm(15),5,3)
C = matrix(rnorm(20),5,4)
cbind(A,B,C)

[,1]   [,2]  [,3] [,4]   [,5][,6]
[1,] -0.54194873 -1.1105170 -0.479010  0.619911163  0.1610162  0.49028633
[2,] -0.39289246  0.0752089  1.427386 -0.921868090 -0.7637016 -0.34905125
[3,] -0.07082828 -0.1060497 -1.007713 -0.003673573 -0.8384406 -0.88816295
[4,]  0.22733701 -1.6134894 -1.993654  2.277865363 -2.3599239 -0.21704046
[5,] -0.13809337  0.3443488 -1.384425  0.132130433  0.1345938 -0.04170648
   [,7]   [,8][,9]
[1,] -1.7481451  0.4467964 -0.41358420
[2,] -0.2882922  1.0243662 -0.48263684
[3,]  0.9402479  0.5467952 -0.01922035
[4,]  0.6795783  1.4560765 -0.23013826
[5,]  0.9800312 -1.3462175 -0.77064872

- Phil Spector
 Statistical Computing Facility
 Department of Statistics
 UC Berkeley
 spec...@stat.berkeley.edu


On Sat, 4 Jun 2011, Jim Silverton wrote:


How can I cbind three or more matrices like A,B and C. This does not work:

cbind(A,B,C)


--
Thanks,
Jim.

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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
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] Cbind query

2010-10-23 Thread karthicklakshman

Hello jholtman,

Thanks very much for the suggestion.

I tried using as.is=TRUE and it worked as the way I expected. Sorry for
not being clear about the problem in my mail.

The characters are very much needed, cos I am trying to create a signaling
network using Rgraphviz.

Thanks again.
Regards,
kaarz
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Cbind-query-tp3006988p3008190.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
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] Cbind query

2010-10-22 Thread karthicklakshman

I am new to R and request your kind help.

I have a table like the one below, 
   one two
1  apple  fruit
2  ball game
3  chair   wood
4  wood   plain
5  fruitbanana
6  cloth   silk

Note: duplicate entries are there

the task is to create relations to each each row entries, like apple -
fruit . when I tried to combine column1 with column 2 (one, two), using
cbind the string is changed to numerical value...something like this
[,1] [,2]
   [1,]   10   53
   [2,]   25  562
   [3,]   25  462
   [4,]   25 1045
   [5,]   25  488
   [6,]   26 1062
   [7,]   27  951
   [8,]   27  144
   [9,]   27  676
  [10,]   27  486

Please suggest me how to get the string names back like the first table in
the out put, using cbind.

Thanks in advance
regards
kaarz

-- 
View this message in context: 
http://r.789695.n4.nabble.com/Cbind-query-tp3006988p3006988.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
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] Cbind query

2010-10-22 Thread jim holtman
This comes about since when using read.table (which I assume you did,
but you did not show us what commands you were using), characters are
converted to factors.  If you are not using factors, then you probably
want the data read in as characters.  You should understand the use of
'str' to look at the structure of your objects to understand what
might be happening.  See example below:

 tab - read.table(textConnection(  one two
+ 1  apple  fruit
+ 2  ball game
+ 3  chair   wood
+ 4  wood   plain
+ 5  fruitbanana
+ 6  cloth   silk))
 closeAllConnections()
 str(tab)  # note the you have 'factors'
'data.frame':   6 obs. of  2 variables:
 $ one: Factor w/ 6 levels apple,ball,..: 1 2 3 6 5 4
 $ two: Factor w/ 6 levels banana,fruit,..: 2 3 6 4 1 5
 cbind(tab$one, tab$two)  # this gives numeric values of the factors
 [,1] [,2]
[1,]12
[2,]23
[3,]36
[4,]64
[5,]51
[6,]45
 # now read in data and not convert to factors (note:  as.is=TRUE)
 tab - read.table(textConnection(  one two
+ 1  apple  fruit
+ 2  ball game
+ 3  chair   wood
+ 4  wood   plain
+ 5  fruitbanana
+ 6  cloth   silk), as.is = TRUE)
 closeAllConnections()
 str(tab)  # now you have characters
'data.frame':   6 obs. of  2 variables:
 $ one: chr  apple ball chair wood ...
 $ two: chr  fruit game wood plain ...
 cbind(tab$one, tab$two)  # this gives character values
 [,1][,2]
[1,] apple fruit
[2,] ball  game
[3,] chair wood
[4,] wood  plain
[5,] fruit banana
[6,] cloth silk



On Fri, Oct 22, 2010 at 7:06 AM, karthicklakshman
karthick.laksh...@gmail.com wrote:

 I am new to R and request your kind help.

 I have a table like the one below,
   one         two
 1  apple      fruit
 2  ball         game
 3  chair       wood
 4  wood       plain
 5  fruit        banana
 6  cloth       silk

 Note: duplicate entries are there

 the task is to create relations to each each row entries, like apple -
 fruit . when I tried to combine column1 with column 2 (one, two), using
 cbind the string is changed to numerical value...something like this
        [,1] [,2]
   [1,]   10   53
   [2,]   25  562
   [3,]   25  462
   [4,]   25 1045
   [5,]   25  488
   [6,]   26 1062
   [7,]   27  951
   [8,]   27  144
   [9,]   27  676
  [10,]   27  486

 Please suggest me how to get the string names back like the first table in
 the out put, using cbind.

 Thanks in advance
 regards
 kaarz

 --
 View this message in context: 
 http://r.789695.n4.nabble.com/Cbind-query-tp3006988p3006988.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@r-project.org mailing list
 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.




-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

__
R-help@r-project.org mailing list
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] cbind

2010-08-21 Thread r.ookie
Is there a way to rename the columns to something like A and B in the cbind 
function?

x - rnorm(n = 10, mean = 0, sd = 1)
y - rnorm(n = 10, mean = 0, sd = 1)
cbind(x,y)

__
R-help@r-project.org mailing list
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] cbind

2010-08-21 Thread RICHARD M. HEIBERGER
cbind(A=x, B=y)



On Sat, Aug 21, 2010 at 6:53 PM, r.ookie r.oo...@live.com wrote:

 Is there a way to rename the columns to something like A and B in the cbind
 function?

 x - rnorm(n = 10, mean = 0, sd = 1)
 y - rnorm(n = 10, mean = 0, sd = 1)
 cbind(x,y)

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.htmlhttp://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
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] cbind

2010-08-21 Thread Peter Ehlers

On 2010-08-21 16:53, r.ookie wrote:

Is there a way to rename the columns to something like A and B in the cbind 
function?

x- rnorm(n = 10, mean = 0, sd = 1)
y- rnorm(n = 10, mean = 0, sd = 1)
cbind(x,y)


Unless I completely misunderstand your query, ?cbind tells you:

...   vectors or matrices. These can be given as named arguments.

Surely, you checked?

  -Peter Ehlers

__
R-help@r-project.org mailing list
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] cbind

2010-08-21 Thread r.ookie
Thanks.

On Aug 21, 2010, at 4:01 PM, RICHARD M. HEIBERGER wrote:

cbind(A=x, B=y)



On Sat, Aug 21, 2010 at 6:53 PM, r.ookie r.oo...@live.com wrote:
Is there a way to rename the columns to something like A and B in the cbind 
function?

x - rnorm(n = 10, mean = 0, sd = 1)
y - rnorm(n = 10, mean = 0, sd = 1)
cbind(x,y)

__
R-help@r-project.org mailing list
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
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] CBIND / MERGE two time series objects along time (overlapping indices, redundant data)

2010-07-30 Thread Bernhard Von Boyen
Hi there,

I need to merge/bind two time series objects (from RPackage: timeSeries) by 
column. The theory is laid out nicely, even for overlapping indices. 


In my example, I have overlapping indices (01.01.2001), where in one time 
series I have one data point and in the other redundant data. Default usage of 
merge and cbind would lead to the result that the one data point would be 
replicated (see example below). However, I do not like the replicating feature 
and would like to have the NA's filled instead.

Desired output, would be:
    TS.1 SS.2
2001-01-01 23.1 23.1
2001-01-01 NA  23.4

Is there a slick way to produce above result? Thank you for your answer - help 
is really appreciated.

Kind regards,

Bernhard.

---


# R Console example:
require(timeSeries)
date -timeDate(01.01.2001, format = %d.%m.%Y)
test1 - timeSeries(c(23.1), charvec = date)
test2 - timeSeries(c(23.1, 23.4), charvec = c(date, date))
colnames(test2) - SS.2 
merge(test1, test2)
GMT
   TS.1 SS.2
2001-01-01 23.1 23.1
2001-01-01 23.1 23.4
cbind(test1, test2)
Fehler in cbind(deparse.level, ...) : number of rows must match
cbind(test2, test1)
GMT
   SS.2 TS.1
2001-01-01 23.1 23.1
2001-01-01 23.4 23.1



  
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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] cbind in for loops

2010-07-12 Thread jd6688

I have 30 files in the current directories, i would like to perform the
cbind(fil1,file2,file3,file4file30)

how could i do this in a for loop:

such as:
 file2 - list.files(pattern=.out3$)
   for (j in file2) {
 cbind(j)...how to implement cbind here
  }


Thanks.


-- 
View this message in context: 
http://r.789695.n4.nabble.com/cbind-in-for-loops-tp2285690p2285690.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
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] cbind in for loops

2010-07-12 Thread Joshua Wiley
Hi,

Assuming that you have read the files into R,
and that their names (in R) are held in some object
(e.g., 'file2'), then this works

do.call(what = cbind, args = mget(x = file2, envir = .GlobalEnv)

Here is a reproducible example:

x1 - data.frame(x = 1:10)
x2 - data.frame(y = 1:10)
file.names - c(x1, x2)
do.call(cbind, mget(file.names, envir=.GlobalEnv))

Best regards,

Josh


On Sun, Jul 11, 2010 at 9:08 PM, jd6688 jdsignat...@gmail.com wrote:

 I have 30 files in the current directories, i would like to perform the
 cbind(fil1,file2,file3,file4file30)

 how could i do this in a for loop:

 such as:
     file2 - list.files(pattern=.out3$)
       for (j in file2) {
         cbind(j)...how to implement cbind here
      }


 Thanks.


 --
 View this message in context: 
 http://r.789695.n4.nabble.com/cbind-in-for-loops-tp2285690p2285690.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@r-project.org mailing list
 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.




-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

__
R-help@r-project.org mailing list
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] cbind in for loops

2010-07-12 Thread David Winsemius


On Jul 12, 2010, at 2:32 AM, Joshua Wiley wrote:


Hi,

Assuming that you have read the files into R,
and that their names (in R) are held in some object
(e.g., 'file2'), then this works

do.call(what = cbind, args = mget(x = file2, envir = .GlobalEnv)

Here is a reproducible example:

x1 - data.frame(x = 1:10)
x2 - data.frame(y = 1:10)
file.names - c(x1, x2)
do.call(cbind, mget(file.names, envir=.GlobalEnv))



If you want to automatically retrieve an unknown or large number of  
files from the current environment you can use grep with ls


 ls()[grep(^v, ls()) ]
[1] v1  v2  v3  v4  varname

I don't have any objects named ...out3 but you might try the above  
form with your pattern.


--
DAvid.

Best regards,

Josh


On Sun, Jul 11, 2010 at 9:08 PM, jd6688 jdsignat...@gmail.com wrote:


I have 30 files in the current directories, i would like to perform  
the

cbind(fil1,file2,file3,file4file30)

how could i do this in a for loop:

such as:
file2 - list.files(pattern=.out3$)
  for (j in file2) {
cbind(j)...how to implement cbind here
 }


Thanks.


--
View this message in context: 
http://r.789695.n4.nabble.com/cbind-in-for-loops-tp2285690p2285690.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
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.





--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

__
R-help@r-project.org mailing list
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, MD
West Hartford, CT

__
R-help@r-project.org mailing list
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] cbind in for loops

2010-07-12 Thread Henrique Dallazuanna
Try this:

do.call(cbind, lapply(dir(pattern = '.out3$'), read.table))

On Mon, Jul 12, 2010 at 1:08 AM, jd6688 jdsignat...@gmail.com wrote:


 I have 30 files in the current directories, i would like to perform the
 cbind(fil1,file2,file3,file4file30)

 how could i do this in a for loop:

 such as:
 file2 - list.files(pattern=.out3$)
   for (j in file2) {
 cbind(j)...how to implement cbind here
  }


 Thanks.


 --
 View this message in context:
 http://r.789695.n4.nabble.com/cbind-in-for-loops-tp2285690p2285690.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@r-project.org mailing list
 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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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] cbind with vectors of different lengths?

2010-06-10 Thread Roman Luštrik

I wrote a function that cbinds vectors of different lengths. Basically, the
function adds NAs to shorter vectors and cbinds in the end. I'm attaching
the code for guidance.

# This function takes in a list() of vectors and cbinds them into a
data.frame.
timerMelt - function(x, write.down = FALSE, filename = output) {
stopifnot(is.list(x))
   
filename - paste(filename, .txt, sep = )
len1 - length(x[[1]])
len2 - length(x[[2]])
len3 - length(x[[3]])
max.len - max(c(len1, len2, len3))
   
x.cat1 - data.frame(rep(NA, max.len))
x.cat2 - data.frame(rep(NA, max.len))
x.cat3 - data.frame(rep(NA, max.len))
   
if (len1  max.len) {
x.cat1[1:len1,] - data.frame(x[[1]])
} else {
x.cat1 - data.frame(x[[1]])
}
   
if (len2  max.len) {
x.cat2[1:len2,] - data.frame(x[[2]])
} else {
x.cat2 - data.frame(x[[2]])
}
   
if (len3  max.len) {
x.cat3[1:len3,] - data.frame(x[[3]])
} else {
x.cat3 - data.frame(x[[3]])
}
   
result - cbind(x.cat1, x.cat2, x.cat3)
names(result) - c(s, p, r)
   
if (write.down == TRUE) {
write.table(result, filename, row.names = FALSE)
}
   
return(result)
}

Cheers,
Roman 
-- 
View this message in context: 
http://r.789695.n4.nabble.com/cbind-with-vectors-of-different-lengths-tp2249680p2250025.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
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] cbind with vectors of different lengths?

2010-06-09 Thread Arantzazu Blanco Bernardeau

Hello R help
I have a dataframe, with 71 samples (rows) and 30 variables. I got linear 
models for some of the variables,  and I want to join fitted and residuals of 
these models to the data frame. Sometimes, these vectors have the same length 
of the dependant variable, but in a few cases, NA values can be found on my 
data, and therefore, both fitted and residuals have a few rows less than the 
original data frame. As I try cbind, R answers with error, because both vectors 
have different lenghts. I have tried with merge but... suddenly  I had a lot of 
rows of repeated values. I think (with my small idea of R and the manuals and 
helps I did read) that first, I have to force residuals and fitted of my model 
to be a data frame.
as.data.frame(fitted(lm))
this gives, for example
[1] 1.1


[3] 3.8


[4] 1.3

[5] 0.9

instead of the original fitted(lm)
1        3   4  5
1.1     3.8     1.3  0.9


that I want to join to my data frame
[1] 17.0
[2] 15.2


[3] 17.3


[4] 15.0

[5] 17.4  
as you can see, row 2 does not exist in fitted... how can I tell R to leave 
this row as NA, as below?

[1] 17.0    1.1

[2] 15.2    NA



[3] 17.3    3.8



[4] 15.0    1.3


[5] 17.4    0.9  
thanks and greets.
Arantzazu Blanco Bernardeau
Dpto de Química Agrícola, Geología y Edafología
Universidad de Murcia-Campus de Espinardo
  
_
¿Un navegador seguro buscando estás? ¡Protegete ya en www.ayudartepodria.com!
www.ayudartepodria.com
__
R-help@r-project.org mailing list
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] cbind and automatic type conversion

2010-05-02 Thread Uwe Ligges



On 01.05.2010 21:09, Giovanni Azua wrote:

Hello,

I have three method types and 100 generalization errors for each, all in the 
range [0.65,0.81]. I would like to make a stacked histogram plot using ggplot2 
with this data ...

Therefore I need a data frame of the form e.g.

Method   GE
--   --
Classic0.76
Classic0.79
Own Bootstrap   0.81
Own Bootstrap   0.79
R Bootstrap0.71
R Bootstrap0.75

So I combine the data in the following way:

normalerrors- rbind(cbind(rep(Classic,S),classicge[,1]),
 cbind(rep(Own Bootstrap,S),ownge[,1]),cbind(rep(R 
Bootstrap,S),rbootge[,1]))
normalerrors- data.frame(method=factor(normalerrors[,1]),ge=normalerrors[,2])

But doing it in this way my GE coefficients get automatically converted to 
string type ... how can I avoid this conversion when doing the cbind?


Not at all, since cbind() constructs a matrix which is of exactly one 
type. You probably want to construct the data.frame directly as in



labels - c(Classic, Own Bootstrap, R Bootstrap)
normalerrors - data.frame(
method = gl(length(labels), S, labels = labels),
ge = c(classicge[,1], ownge[,1], rbootge[,1]))

Best,
Uwe Ligges



TIA,
Best regards,
Giovanni


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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
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] cbind and automatic type conversion

2010-05-01 Thread Giovanni Azua
Hello,

I have three method types and 100 generalization errors for each, all in the 
range [0.65,0.81]. I would like to make a stacked histogram plot using ggplot2 
with this data ...
 
Therefore I need a data frame of the form e.g.

Method   GE
--   --
Classic0.76
Classic0.79
Own Bootstrap   0.81
Own Bootstrap   0.79
R Bootstrap0.71
R Bootstrap0.75

So I combine the data in the following way:

normalerrors - rbind(cbind(rep(Classic,S),classicge[,1]),
cbind(rep(Own Bootstrap,S),ownge[,1]),cbind(rep(R 
Bootstrap,S),rbootge[,1]))
normalerrors - data.frame(method=factor(normalerrors[,1]),ge=normalerrors[,2])

But doing it in this way my GE coefficients get automatically converted to 
string type ... how can I avoid this conversion when doing the cbind?

TIA,
Best regards,
Giovanni


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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] cbind, row names

2010-01-29 Thread soeren . vogel

Hello,

I read the help as well as the examples, but I can not figure out why  
the following code does not produce the *given* row names, x and y:


x - 1:20
y - 21:40
rbind(
  x=cbind(N=length(x), M=mean(x), SD=sd(x)),
  y=cbind(N=length(y), M=mean(y), SD=sd(y))
)

Could you please help?

Thank you

Sören

__
R-help@r-project.org mailing list
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] cbind, row names

2010-01-29 Thread Henrique Dallazuanna
This gives what you want:

rbind.data.frame(
  x=cbind(N=length(x), M=mean(x), SD=sd(x)),
  y=cbind(N=length(y), M=mean(y), SD=sd(y))
)


On Fri, Jan 29, 2010 at 8:49 AM,  soeren.vo...@eawag.ch wrote:
 Hello,

 I read the help as well as the examples, but I can not figure out why the
 following code does not produce the *given* row names, x and y:

 x - 1:20
 y - 21:40
 rbind(
  x=cbind(N=length(x), M=mean(x), SD=sd(x)),
  y=cbind(N=length(y), M=mean(y), SD=sd(y))
 )

 Could you please help?

 Thank you

 Sören

 __
 R-help@r-project.org mailing list
 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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

__
R-help@r-project.org mailing list
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] cbind, row names

2010-01-29 Thread K. Elo
Hi!

29.01.2010 12:49, soeren.vo...@eawag.ch wrote:
 Hello,
 
 I read the help as well as the examples, but I can not figure out why
 the following code does not produce the *given* row names, x and y:
 
 x - 1:20
 y - 21:40
 rbind(
   x=cbind(N=length(x), M=mean(x), SD=sd(x)),
   y=cbind(N=length(y), M=mean(y), SD=sd(y))
 )
 

Maybe because the cbinds in your code produce matrices:

is.matrix(cbind(N=length(x), M=mean(x), SD=sd(x)))
[1] TRUE

Quote ?rbind:
For cbind (rbind) the column (row) names are taken from the colnames
(rownames) of the arguments if these are matrix-like.

HTH,
Kimmo

__
R-help@r-project.org mailing list
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.


  1   2   >