[R] Clustered Standard Errors?

2015-11-16 Thread Ignacio Martinez
Hi,

I found this

post
from 2011 for doing clustered standard errors in R.

Is there any update to the lm package that allows to do this without having
to write your own function? In STATA is as simple as adding cluster to the reg
command.

Thanks a lot for the help!

Ignacio

[[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-es] ¿Clustered Standard Errors?

2015-11-16 Thread Ignacio Martinez
Hola,

En STATA uno puedo usar `Clustered Standard Errors` simplemente agregando a
`reg` la opcion `, cluster(X)`. Hay algo similar para R? No pude encontrar
esa opcion en `lm`

Lo mas cercano que encontro es este post



Gracias por la ayuda!

Ignacio

[[alternative HTML version deleted]]

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



[R-es] Crear un paquete que usa una subrutina escrita en Fortran (en windows)

2015-07-27 Thread Ignacio Martinez
Hola,

Estoy aprendiendo a crear paquetes para R que llaman a subutinas escritas
en Fortran. En linux logre crear el paquete y funciona como esperaba.

Ahora estoy tratando de hacelo en windows. Cuando hago click en el boton de
'build and reload' en Rstudio me salen estos errores:

== Rcmd.exe INSTALL --no-multiarch --with-keep.source MyPi
* installing to library
'C:/Users/IMartinez/Documents/R/R-3.2.1/library'* installing *source*
package 'MyPi' ...** libs
gfortran -m64 -shared -s -static-libgcc -o MyPi.dll tmp.def Fpi.o
-Ld:/RCompile/r-compiling/local/local320/lib/x64
-Ld:/RCompile/r-compiling/local/local320/lib
-LC:/Users/IMARTI~1/DOCUME~1/R/R-32~1.1/bin/x64 -lR
Fpi.o: In function `__fortranpi_MOD_dboard':
Fpi.f90:(.text+0xd7): undefined reference to `__stack_chk_fail'
Fpi.o: In function `pi_':
Fpi.f90:(.text+0x249): undefined reference to `__stack_chk_fail'
collect2: ld returned 1 exit status
no DLL was created
ERROR: compilation failed for package 'MyPi'* removing
'C:/Users/IMartinez/Documents/R/R-3.2.1/library/MyPi'

Exited with status 1.

Este es mi codigo en Fortran:

Module Fortranpi
IMPLICIT NONE
contains
subroutine dboard(darts, dartsscore)
  integer, intent(in):: darts
  double precision, intent(out)  :: dartsscore
  double precision   :: x_coord, y_coord
  integer:: score, n

score = 0
do n = 1, darts
  call random_number(x_coord)
  call random_number(y_coord)

  if ((x_coord**2 + y_coord**2) = 1.0d0) then
  score = score + 1
  end if
end do

dartsscore = 4.0d0*score/darts

end subroutine dboard

subroutine pi(avepi, DARTS, ROUNDS) bind(C, name=pi_)
  use, intrinsic :: iso_c_binding, only :
c_double, c_int
  real(c_double), intent(out)::  avepi
  integer(c_int), intent(in) ::  DARTS, ROUNDS
  integer::  MASTER, rank, i, n
  integer, allocatable   ::  seed(:)
  double precision   ::  pi_est, homepi, pirecv, pisum
! we set it to zero in the sequential run
rank = 0! initialize the random number generator! we make sure the
seed is different for each task
call random_seed()
call random_seed(size = n)
allocate(seed(n))
seed = 12 + rank*11
call random_seed(put=seed(1:n))
deallocate(seed)

avepi = 0
do i = 0, ROUNDS-1
  call dboard(darts, pi_est)
  ! calculate the average value of pi over all iterations
  avepi = ((avepi*i) + pi_est)/(i + 1)
end do
end subroutine pi

end module Fortranpi

Una de las recomendaciones fue agregar -fno-stack-protector -lssp. Hice eso
pero todavia no puedo compilar el paquete
http://i.stack.imgur.com/lC82X.png

Si trato de compilar a mano en lugar de con el boton, me salen estos
errores http://i.stack.imgur.com/WY4VD.png

 system(R CMD SHLIB -fno-stack-protector -lssp ./src/Fpi.f90)
gfortran -m64 -shared -s -static-libgcc -o src/Fpi.dll tmp.def
./src/Fpi.o -fno-stack-protector -lssp
-Ld:/RCompile/r-compiling/local/local320/lib/x64
-Ld:/RCompile/r-compiling/local/local320/lib
-LC:/Users/IMARTI~1/DOCUME~1/R/R-32~1.1/bin/x64 -lR
dyn.load(./src/Fpi.dll)
Error in inDL(x, as.logical(local), as.logical(now), ...) :
  unable to load shared object 'C:/Users/IMartinez/Projects/MyPi/./src/Fpi.dll':
  LoadLibrary failure:  %1 is not a valid Win32 application.
'C:/Users/IMartinez/Projects/MyPi/./src/Fpi.dll':
  LoadLibrary failure:  %1 is not a valid Win32 application.

Gracias por la ayuda!

Ignacio

PS: Hace un par de dias hice esta pregunta en stackoverflow
http://stackoverflow.com/questions/31638934/r-package-with-fortran-module-on-windows-undefined-reference-to-stack-chk-fa?noredirect=1#comment51231196_31638934
pero no todavia no tengo una respuesta.

[[alternative HTML version deleted]]

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


[R] R package with Fortran module on Windows? undefined reference to `__stack_chk_fail'

2015-07-27 Thread Ignacio Martinez
Hi,

I created a R library that uses a Fortran module. Everything works like a
charm on linux.

Now I'm trying to make it work on Windows. I cloned my git repository
https://github.com/ignacio82/MyPi on a windows computer, and when I press
the build and reload button on Rstudio I get these errors:

== Rcmd.exe INSTALL --no-multiarch --with-keep.source MyPi
* installing to library
'C:/Users/IMartinez/Documents/R/R-3.2.1/library'* installing *source*
package 'MyPi' ...** libs
gfortran -m64 -shared -s -static-libgcc -o MyPi.dll tmp.def Fpi.o
-Ld:/RCompile/r-compiling/local/local320/lib/x64
-Ld:/RCompile/r-compiling/local/local320/lib
-LC:/Users/IMARTI~1/DOCUME~1/R/R-32~1.1/bin/x64 -lR
Fpi.o: In function `__fortranpi_MOD_dboard':
Fpi.f90:(.text+0xd7): undefined reference to `__stack_chk_fail'
Fpi.o: In function `pi_':
Fpi.f90:(.text+0x249): undefined reference to `__stack_chk_fail'
collect2: ld returned 1 exit status
no DLL was created
ERROR: compilation failed for package 'MyPi'* removing
'C:/Users/IMartinez/Documents/R/R-3.2.1/library/MyPi'

Exited with status 1.


This is  the Fortran code:


Module Fortranpi
IMPLICIT NONE
contains
subroutine dboard(darts, dartsscore)
  integer, intent(in):: darts
  double precision, intent(out)  :: dartsscore
  double precision   :: x_coord, y_coord
  integer:: score, n

score = 0
do n = 1, darts
  call random_number(x_coord)
  call random_number(y_coord)

  if ((x_coord**2 + y_coord**2) = 1.0d0) then
  score = score + 1
  end if
end do

dartsscore = 4.0d0*score/darts

end subroutine dboard

subroutine pi(avepi, DARTS, ROUNDS) bind(C, name=pi_)
  use, intrinsic :: iso_c_binding, only :
c_double, c_int
  real(c_double), intent(out)::  avepi
  integer(c_int), intent(in) ::  DARTS, ROUNDS
  integer::  MASTER, rank, i, n
  integer, allocatable   ::  seed(:)
  double precision   ::  pi_est, homepi, pirecv, pisum
! we set it to zero in the sequential run
rank = 0! initialize the random number generator! we make sure the
seed is different for each task
call random_seed()
call random_seed(size = n)
allocate(seed(n))
seed = 12 + rank*11
call random_seed(put=seed(1:n))
deallocate(seed)

avepi = 0
do i = 0, ROUNDS-1
  call dboard(darts, pi_est)
  ! calculate the average value of pi over all iterations
  avepi = ((avepi*i) + pi_est)/(i + 1)
end do
end subroutine pi

end module Fortranpi


I tried adding http://i.stack.imgur.com/lC82X.png -fno-stack-protector
-lssp but it did not help.

I also tried doing this by hand http://i.stack.imgur.com/WY4VD.png and
I get these errors:


 system(R CMD SHLIB -fno-stack-protector -lssp ./src/Fpi.f90)
gfortran -m64 -shared -s -static-libgcc -o src/Fpi.dll tmp.def
./src/Fpi.o -fno-stack-protector -lssp
-Ld:/RCompile/r-compiling/local/local320/lib/x64
-Ld:/RCompile/r-compiling/local/local320/lib
-LC:/Users/IMARTI~1/DOCUME~1/R/R-32~1.1/bin/x64 -lR
dyn.load(./src/Fpi.dll)
Error in inDL(x, as.logical(local), as.logical(now), ...) :
  unable to load shared object 'C:/Users/IMartinez/Projects/MyPi/./src/Fpi.dll':
  LoadLibrary failure:  %1 is not a valid Win32 application.
'C:/Users/IMartinez/Projects/MyPi/./src/Fpi.dll':
  LoadLibrary failure:  %1 is not a valid Win32 application.


Thanks for the help!


Ignacio


PS: I posted this question in stackoverflow with no luck.
http://stackoverflow.com/questions/31638934/r-package-with-fortran-module-on-windows-undefined-reference-to-stack-chk-fa

[[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] R package with Fortran module on Windows? undefined reference to `__stack_chk_fail'

2015-07-27 Thread Ignacio Martinez
:( I sent this to R-package... I hope people are not hating me for spamming
the lists... I will email R-devel

Thanks!

On Mon, Jul 27, 2015 at 1:55 PM peter dalgaard pda...@gmail.com wrote:

 You went through the trouble to chastise a beginner and failed to spot the
 difference between R-packages and R-package-devel?

 (I think R-devel is better for now. R-package-devel is more for people
 trying to make CRAN happy.)

 -pd

 PS: The by-hand example suggests that there is a 32/64 bit mixup.

  On 27 Jul 2015, at 17:58 , Jeff Newmiller jdnew...@dcn.davis.ca.us
 wrote:
 
  You went to all that trouble to find a mailing list to ask your question
 on and failed to find R-packages or R-devel?
 
 ---
  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.
 
  On July 27, 2015 10:21:51 AM CDT, Ignacio Martinez ignaci...@gmail.com
 wrote:
  Hi,
 
  I created a R library that uses a Fortran module. Everything works like
  a
  charm on linux.
 
  Now I'm trying to make it work on Windows. I cloned my git repository
  https://github.com/ignacio82/MyPi on a windows computer, and when I
  press
  the build and reload button on Rstudio I get these errors:
 
  == Rcmd.exe INSTALL --no-multiarch --with-keep.source MyPi
  * installing to library
  'C:/Users/IMartinez/Documents/R/R-3.2.1/library'* installing *source*
  package 'MyPi' ...** libs
  gfortran -m64 -shared -s -static-libgcc -o MyPi.dll tmp.def Fpi.o
  -Ld:/RCompile/r-compiling/local/local320/lib/x64
  -Ld:/RCompile/r-compiling/local/local320/lib
  -LC:/Users/IMARTI~1/DOCUME~1/R/R-32~1.1/bin/x64 -lR
  Fpi.o: In function `__fortranpi_MOD_dboard':
  Fpi.f90:(.text+0xd7): undefined reference to `__stack_chk_fail'
  Fpi.o: In function `pi_':
  Fpi.f90:(.text+0x249): undefined reference to `__stack_chk_fail'
  collect2: ld returned 1 exit status
  no DLL was created
  ERROR: compilation failed for package 'MyPi'* removing
  'C:/Users/IMartinez/Documents/R/R-3.2.1/library/MyPi'
 
  Exited with status 1.
 
 
  This is  the Fortran code:
 
 
  Module Fortranpi
  IMPLICIT NONE
  contains
  subroutine dboard(darts, dartsscore)
  integer, intent(in):: darts
  double precision, intent(out)  :: dartsscore
  double precision   :: x_coord, y_coord
  integer:: score, n
 
  score = 0
  do n = 1, darts
  call random_number(x_coord)
  call random_number(y_coord)
 
  if ((x_coord**2 + y_coord**2) = 1.0d0) then
  score = score + 1
  end if
  end do
 
  dartsscore = 4.0d0*score/darts
 
  end subroutine dboard
 
  subroutine pi(avepi, DARTS, ROUNDS) bind(C, name=pi_)
  use, intrinsic :: iso_c_binding, only :
  c_double, c_int
  real(c_double), intent(out)::  avepi
  integer(c_int), intent(in) ::  DARTS, ROUNDS
  integer::  MASTER, rank, i, n
  integer, allocatable   ::  seed(:)
  double precision   ::  pi_est, homepi, pirecv,
  pisum
  ! we set it to zero in the sequential run
  rank = 0! initialize the random number generator! we make sure the
  seed is different for each task
  call random_seed()
  call random_seed(size = n)
  allocate(seed(n))
  seed = 12 + rank*11
  call random_seed(put=seed(1:n))
  deallocate(seed)
 
  avepi = 0
  do i = 0, ROUNDS-1
  call dboard(darts, pi_est)
  ! calculate the average value of pi over all iterations
  avepi = ((avepi*i) + pi_est)/(i + 1)
  end do
  end subroutine pi
 
  end module Fortranpi
 
 
  I tried adding http://i.stack.imgur.com/lC82X.png
  -fno-stack-protector
  -lssp but it did not help.
 
  I also tried doing this by hand http://i.stack.imgur.com/WY4VD.png
  and
  I get these errors:
 
 
  system(R CMD SHLIB -fno-stack-protector -lssp ./src/Fpi.f90)
  gfortran -m64 -shared -s -static-libgcc -o src/Fpi.dll tmp.def
  ./src/Fpi.o -fno-stack-protector -lssp
  -Ld:/RCompile/r-compiling/local/local320/lib/x64
  -Ld:/RCompile/r-compiling/local/local320/lib
  -LC:/Users/IMARTI~1/DOCUME~1/R/R-32~1.1/bin/x64 -lR
  dyn.load(./src/Fpi.dll)
  Error in inDL(x, as.logical(local), as.logical(now), ...) :
  unable to load shared object
  'C:/Users/IMartinez/Projects/MyPi/./src/Fpi.dll':
  LoadLibrary failure:  %1 is not a valid Win32 application.
  'C:/Users/IMartinez/Projects/MyPi/./src/Fpi.dll':
  LoadLibrary failure:  %1 is not a valid Win32 application.
 
 
  Thanks for the help!
 
 
  Ignacio
 
 
  PS: I posted this question in stackoverflow

Re: [R] Speeding up code?

2015-07-16 Thread Ignacio Martinez
Hi Collin,

The objective of the gen.names function is to generate N *unique *random
names, where N is a *large *number. In my computer `gen.names(n = 5)`
takes under a second, so is probably not the root problem in my code. That
said, I would love to improve it. I'm not exactly sure how you propose to
change it using sample. What is the object that I would be sampling? I
would love to run a little benchmark to compare my version with yours.

What really takes a long time to run is:

separoPairs - as.data.frame(str_split(string = AllpairsTmp, pattern =
-))

So that and the chunk of code before that is probably where I would get big
gains in speed. Sadly, I have no clue how to do it differently

Thanks a lot for the help!!

Ignacio


On Wed, Jul 15, 2015 at 11:34 PM Collin Lynch cfly...@ncsu.edu wrote:

 Hi Ignacio, If I am reading your code correctly then the top while loop is
 essentially seeking to select a random set of names from the original set,
 then using unique to reduce it down, you then iterate until you have built
 your quota.  Ultimately this results in a very inefficient attempt at
 sampling without replacement.  Why not just sample without replacement
 rather than loop iteratively and use unique?  Or if the set of possible
 names are short enough why not just randomize it and then pull the first n
 items off?

 Best,
 Collin.

 On Wed, Jul 15, 2015 at 11:15 PM, Ignacio Martinez ignaci...@gmail.com
 wrote:

 Hi R-Help!

 I'm hoping that some of you may give me some tips that could make my code

 more efficient. More precisely, I would like to make the answer to my
 stakoverflow
 
 http://stackoverflow.com/questions/31137940/randomly-assign-teachers-to-classrooms-imposing-restrictions
 


 question more efficient.

 This is the code:

 library(dplyr)
 library(randomNames)
 library(geosphere)

 set.seed(7142015)# Define Parameters


 n.Schools - 20
 first.grade-3
 last.grade-5
 n.Grades -last.grade-first.grade+1
 n.Classrooms - 20 # THIS IS WHAT I WANTED TO BE ABLE TO CHANGE
 n.Teachers - (n.Schools*n.Grades*n.Classrooms)/2 #Two classrooms per
 teacher
 # Define Random names function:
 gen.names - function(n, which.names = both, name.order = last.first){
   names - unique(randomNames(n=n, which.names = which.names,
 name.order = name.order))
   need - n - length(names)
   while(need0){
 names - unique(c(randomNames(n=need, which.names = which.names,
 name.order = name.order), names))
 need - n - length(names)
   }
   return(names)}
 # Generate n.Schools names
 gen.schools - function(n.schools) {
   School.ID -
 paste0(gen.names(n = n.schools, which.names = last), ' School')
   School.long - rnorm(n = n.schools, mean = 21.7672, sd = 0.025)
   School.lat - rnorm(n = n.schools, mean = 58.8471, sd = 0.025)
   School.RE - rnorm(n = n.schools, mean = 0, sd = 1)
   Schools -
 data.frame(School.ID, School.lat, School.long, School.RE) %%
 mutate(School.ID = as.character(School.ID)) %%
 rowwise() %%  mutate (School.distance = distHaversine(
   p1 = c(School.long, School.lat),
   p2 = c(21.7672, 58.8471), r = 3961
 ))
   return(Schools)}

 Schools - gen.schools(n.schools = n.Schools)
 # Generate Grades
 Grades - c(first.grade:last.grade)
 # Generate n.Classrooms

 Classrooms - LETTERS[1:n.Classrooms]
 # Group schools and grades

 SchGr - outer(paste0(Schools$School.ID, '-'), paste0(Grades, '-'),
 FUN=paste)#head(SchGr)
 # Group SchGr and Classrooms

 SchGrClss - outer(SchGr, paste0(Classrooms, '-'),
 FUN=paste)#head(SchGrClss)
 # These are the combination of  School-Grades-Classroom
 SchGrClssTmp - as.matrix(SchGrClss, ncol=1, nrow=length(SchGrClss) )
 SchGrClssEnd - as.data.frame(SchGrClssTmp)
 # Assign n.Teachers (2 classroom in a given school-grade)
 Allpairs - as.data.frame(t(combn(SchGrClssTmp, 2)))
 AllpairsTmp - paste(Allpairs$V1, Allpairs$V2, sep= )

 library(stringr)
 separoPairs - as.data.frame(str_split(string = AllpairsTmp, pattern =
 -))
 separoPairs - as.data.frame(t(separoPairs))
 row.names(separoPairs) - NULL
 separoPairs - separoPairs %% select(-V7)  %%  #Drops empty column
   mutate(V1=as.character(V1), V4=as.character(V4), V2=as.numeric(V2),
 V5=as.numeric(V5)) %% mutate(V4 = trimws(V4, which = both))

 separoPairs[120,]$V4#Only the rows with V1=V4 and V2=V5 are valid


 validPairs - separoPairs %% filter(V1==V4  V2==V5) %% select(V1, V2,
 V3, V6)
 # Generate n.Teachers

 gen.teachers - function(n.teachers){
   Teacher.ID - gen.names(n = n.teachers, name.order = last.first)
   Teacher.exp - runif(n = n.teachers, min = 1, max = 30)
   Teacher.Other - sample(c(0,1), replace = T, prob = c(0.5, 0.5),
 size = n.teachers)
   Teacher.RE - rnorm(n = n.teachers, mean = 0, sd = 1)
   Teachers - data.frame(Teacher.ID, Teacher.exp, Teacher.Other,
 Teacher.RE)
   return(Teachers)}
 Teachers - gen.teachers(n.teachers = n.Teachers) %%
   mutate(Teacher.ID = as.character(Teacher.ID))
 # Randomly assign n.Teachers teachers to the ValidPairs

Re: [R] Speeding up code?

2015-07-16 Thread Ignacio Martinez
Thank Jim!

This makes a huge difference. Can you explain why are data frame slower
than a matrix? Any other suggestions on how to improve the code would be
greatly appreciated.

Thanks again!

Ignacio

On Thu, Jul 16, 2015 at 1:42 PM jim holtman jholt...@gmail.com wrote:

 Actually looking at the result, you don't need the transpose; that was an
 artifact of how you were doing it before.

  xm - do.call(rbind, str_split(string = AllpairsTmp, pattern = -))
  # convert to dataframe and do transpose on matrix and not dataframe
  separoPairs - as.data.frame((xm), stringsAsFactors = FALSE)




 Jim Holtman
 Data Munger Guru

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

 On Thu, Jul 16, 2015 at 1:37 PM, jim holtman jholt...@gmail.com wrote:

 Here is one improvement.  Avoid dataframes in some of these cases.  This
 create a character matrix and then converts to a dataframe after doing the
 transpose of the matrix.  This just takes less than 10 seconds on my system:


   library(stringr)
   # create character matrix; avoid dataframes in this case
   print(proc.time())
user  system elapsed
   15.525.24  587.70
   xm - do.call(rbind, str_split(string = AllpairsTmp, pattern = -))
   # convert to dataframe and do transpose on matrix and not dataframe
   separoPairs - as.data.frame(t(xm), stringsAsFactors = FALSE)
   print(proc.time()
 +
 + )
user  system elapsed
   20.905.36  596.57
 


 Jim Holtman
 Data Munger Guru

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

 On Thu, Jul 16, 2015 at 7:56 AM, Ignacio Martinez ignaci...@gmail.com
 wrote:

 Hi Collin,

 The objective of the gen.names function is to generate N *unique *random
 names, where N is a *large *number. In my computer `gen.names(n = 5)`
 takes under a second, so is probably not the root problem in my code.
 That
 said, I would love to improve it. I'm not exactly sure how you propose to
 change it using sample. What is the object that I would be sampling? I
 would love to run a little benchmark to compare my version with yours.

 What really takes a long time to run is:

 separoPairs - as.data.frame(str_split(string = AllpairsTmp, pattern
 =
 -))

 So that and the chunk of code before that is probably where I would get
 big
 gains in speed. Sadly, I have no clue how to do it differently

 Thanks a lot for the help!!

 Ignacio


 On Wed, Jul 15, 2015 at 11:34 PM Collin Lynch cfly...@ncsu.edu wrote:

  Hi Ignacio, If I am reading your code correctly then the top while
 loop is
  essentially seeking to select a random set of names from the original
 set,
  then using unique to reduce it down, you then iterate until you have
 built
  your quota.  Ultimately this results in a very inefficient attempt at
  sampling without replacement.  Why not just sample without replacement
  rather than loop iteratively and use unique?  Or if the set of possible
  names are short enough why not just randomize it and then pull the
 first n
  items off?
 
  Best,
  Collin.
 
  On Wed, Jul 15, 2015 at 11:15 PM, Ignacio Martinez 
 ignaci...@gmail.com
  wrote:
 
  Hi R-Help!
 
  I'm hoping that some of you may give me some tips that could make my
 code
 
  more efficient. More precisely, I would like to make the answer to my
  stakoverflow
  
 
 http://stackoverflow.com/questions/31137940/randomly-assign-teachers-to-classrooms-imposing-restrictions

  
 
 
  question more efficient.
 
  This is the code:
 
  library(dplyr)
  library(randomNames)
  library(geosphere)
 
  set.seed(7142015)# Define Parameters
 
 
  n.Schools - 20
  first.grade-3
  last.grade-5
  n.Grades -last.grade-first.grade+1
  n.Classrooms - 20 # THIS IS WHAT I WANTED TO BE ABLE TO CHANGE
  n.Teachers - (n.Schools*n.Grades*n.Classrooms)/2 #Two classrooms per
  teacher
  # Define Random names function:
  gen.names - function(n, which.names = both, name.order =
 last.first){
names - unique(randomNames(n=n, which.names = which.names,
  name.order = name.order))
need - n - length(names)
while(need0){
  names - unique(c(randomNames(n=need, which.names = which.names,
  name.order = name.order), names))
  need - n - length(names)
}
return(names)}
  # Generate n.Schools names
  gen.schools - function(n.schools) {
School.ID -
  paste0(gen.names(n = n.schools, which.names = last), ' School')
School.long - rnorm(n = n.schools, mean = 21.7672, sd = 0.025)
School.lat - rnorm(n = n.schools, mean = 58.8471, sd = 0.025)
School.RE - rnorm(n = n.schools, mean = 0, sd = 1)
Schools -
  data.frame(School.ID, School.lat, School.long, School.RE) %%
  mutate(School.ID = as.character(School.ID)) %%
  rowwise() %%  mutate (School.distance = distHaversine(
p1 = c(School.long, School.lat),
p2 = c(21.7672, 58.8471), r = 3961
  ))
return(Schools)}
 
  Schools - gen.schools(n.schools = n.Schools

[R] Speeding up code?

2015-07-15 Thread Ignacio Martinez
Hi R-Help!

I'm hoping that some of you may give me some tips that could make my code
more efficient. More precisely, I would like to make the answer to my
stakoverflow
http://stackoverflow.com/questions/31137940/randomly-assign-teachers-to-classrooms-imposing-restrictions
question more efficient.

This is the code:

library(dplyr)
library(randomNames)
library(geosphere)
set.seed(7142015)# Define Parameters
n.Schools - 20
first.grade-3
last.grade-5
n.Grades -last.grade-first.grade+1
n.Classrooms - 20 # THIS IS WHAT I WANTED TO BE ABLE TO CHANGE
n.Teachers - (n.Schools*n.Grades*n.Classrooms)/2 #Two classrooms per teacher
# Define Random names function:
gen.names - function(n, which.names = both, name.order = last.first){
  names - unique(randomNames(n=n, which.names = which.names,
name.order = name.order))
  need - n - length(names)
  while(need0){
names - unique(c(randomNames(n=need, which.names = which.names,
name.order = name.order), names))
need - n - length(names)
  }
  return(names)}
# Generate n.Schools names
gen.schools - function(n.schools) {
  School.ID -
paste0(gen.names(n = n.schools, which.names = last), ' School')
  School.long - rnorm(n = n.schools, mean = 21.7672, sd = 0.025)
  School.lat - rnorm(n = n.schools, mean = 58.8471, sd = 0.025)
  School.RE - rnorm(n = n.schools, mean = 0, sd = 1)
  Schools -
data.frame(School.ID, School.lat, School.long, School.RE) %%
mutate(School.ID = as.character(School.ID)) %%
rowwise() %%  mutate (School.distance = distHaversine(
  p1 = c(School.long, School.lat),
  p2 = c(21.7672, 58.8471), r = 3961
))
  return(Schools)}

Schools - gen.schools(n.schools = n.Schools)
# Generate Grades
Grades - c(first.grade:last.grade)
# Generate n.Classrooms

Classrooms - LETTERS[1:n.Classrooms]
# Group schools and grades

SchGr - outer(paste0(Schools$School.ID, '-'), paste0(Grades, '-'),
FUN=paste)#head(SchGr)
# Group SchGr and Classrooms

SchGrClss - outer(SchGr, paste0(Classrooms, '-'), FUN=paste)#head(SchGrClss)
# These are the combination of  School-Grades-Classroom
SchGrClssTmp - as.matrix(SchGrClss, ncol=1, nrow=length(SchGrClss) )
SchGrClssEnd - as.data.frame(SchGrClssTmp)
# Assign n.Teachers (2 classroom in a given school-grade)
Allpairs - as.data.frame(t(combn(SchGrClssTmp, 2)))
AllpairsTmp - paste(Allpairs$V1, Allpairs$V2, sep= )

library(stringr)
separoPairs - as.data.frame(str_split(string = AllpairsTmp, pattern = -))
separoPairs - as.data.frame(t(separoPairs))
row.names(separoPairs) - NULL
separoPairs - separoPairs %% select(-V7)  %%  #Drops empty column
  mutate(V1=as.character(V1), V4=as.character(V4), V2=as.numeric(V2),
V5=as.numeric(V5)) %% mutate(V4 = trimws(V4, which = both))

separoPairs[120,]$V4#Only the rows with V1=V4 and V2=V5 are valid
validPairs - separoPairs %% filter(V1==V4  V2==V5) %% select(V1, V2, V3, V6)
# Generate n.Teachers

gen.teachers - function(n.teachers){
  Teacher.ID - gen.names(n = n.teachers, name.order = last.first)
  Teacher.exp - runif(n = n.teachers, min = 1, max = 30)
  Teacher.Other - sample(c(0,1), replace = T, prob = c(0.5, 0.5),
size = n.teachers)
  Teacher.RE - rnorm(n = n.teachers, mean = 0, sd = 1)
  Teachers - data.frame(Teacher.ID, Teacher.exp, Teacher.Other, Teacher.RE)
  return(Teachers)}
Teachers - gen.teachers(n.teachers = n.Teachers) %%
  mutate(Teacher.ID = as.character(Teacher.ID))
# Randomly assign n.Teachers teachers to the ValidPairs
TmpAssignments - validPairs[sample(1:nrow(validPairs), n.Teachers), ]
Assignments - cbind.data.frame(Teachers$Teacher.ID, TmpAssignments)
names(Assignments) - c(Teacher.ID, School.ID, Grade, Class_1,
Class_2)
# Tidy Data
library(tidyr)
TeacherClassroom - Assignments %%
  gather(x, Classroom, Class_1,Class_2) %%
  select(-x) %%
  mutate(Teacher.ID = as.character(Teacher.ID))
# Merge
DF_Classrooms - TeacherClassroom %% full_join(Teachers,
by=Teacher.ID) %% full_join(Schools, by=School.ID)
rm(list=setdiff(ls(), DF_Classrooms)) # Clean the work space!

*I want to end up with the same*  'DF_Classrooms *data frame* but getting
there in a more efficient way. In particular, when is use n.Classrooms -4 the
code run fast, but *if I increase it to something like 20 it is painfully
slow.*

Thanks!!!

[[alternative HTML version deleted]]

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


Re: [R-es] Crear datos aleatorios con restriciones

2015-07-14 Thread Ignacio Martinez
Genial Carlos! Tu codigo produce lo que quiero!

Estoy tratando de entender cada paso y hacer algunos cambios. Mi problema
es con como usar `str_plit_fixed`. Con tu codigo tengo eso:

 separoPairs - as.data.frame(str_split_fixed(AllpairsTmp,  , 6))

head(separoPairs)

  V1 V2 V3 V4 V5 V6
1 e1 g1 c1 e2 g1 c1
2 e1 g1 c1 e3 g1 c1
3 e1 g1 c1 e4 g1 c1
4 e1 g1 c1 e5 g1 c1
5 e1 g1 c1 e6 g1 c1
6 e1 g1 c1 e7 g1 c1


V1 y V4 son el nombre de las escuelas, V2 y V5 del grado y V3 y V6 de la
division. Yo hice unos cambios para tener datos un poco mas complejos, pero
como resultado inintencional no puedo producir `separoPairs` Esto es lo que
mi codigo produce:

 head(separoPairs)  V1 V2 V3V4 V5 V6
1 Aslamy School  3 grade  A  Maruyama School 3 grade A
2 Aslamy School  3 grade  A Smith School 3 grade A
3 Aslamy School  3 grade  A   Linares School 3 grade A
4 Aslamy School  3 grade  A   Dieyleh School 3 grade A
5 Aslamy School  3 grade  A Hernandez School 3 grade A
6 Aslamy School  3 grade  A   Padgett School 3 grade A


Se puede arreglar? Este es mi codigo

library(dplyr)
library(randomNames)
library(geosphere)
set.seed(7142015)
# Define Parameters
n.Schools - 20
first.grade-3
last.grade-5
n.Grades -last.grade-first.grade+1
n.Classrooms - 4
n.Teachers - (n.Schools*n.Grades*n.Classrooms)/2 #Two classrooms per
teacher

# Define Random names function:
gen.names - function(n, which.names = both, name.order = last.first){
  names - unique(randomNames(n=n, which.names = which.names, name.order =
name.order))
  need - n - length(names)
  while(need0){
names - unique(c(randomNames(n=need, which.names = which.names,
name.order = name.order), names))
need - n - length(names)
  }
  return(names)
}

# Generate n.Schools names
gen.schools - function(n.schools) {
  School.ID -
paste0(gen.names(n = n.schools, which.names = last), ' School')
  School.long - rnorm(n = n.schools, mean = 21.7672, sd = 0.025)
  School.lat - rnorm(n = n.schools, mean = 58.8471, sd = 0.025)
  School.RE - rnorm(n = n.schools, mean = 0, sd = 1)
  Schools -
data.frame(School.ID, School.lat, School.long, School.RE) %%
mutate(School.ID = as.character(School.ID)) %%
rowwise() %%  mutate (School.distance = distHaversine(
  p1 = c(School.long, School.lat),
  p2 = c(21.7672, 58.8471), r = 3961
))
  return(Schools)
}

Schools - gen.schools(n.schools = n.Schools)

# Generate Grades
Grades - c(first.grade:last.grade)

# Generate n.Classrooms

Classrooms - LETTERS[1:n.Classrooms]

# Group schools and grades

SchGr - outer(Schools$School.ID, Grades, 'grade', FUN=paste)


# Group SchGr and Classrooms

SchGrClss - outer(SchGr, Classrooms, FUN=paste)

# These are the combination of  School-Grades-Classroom
SchGrClssTmp - as.matrix(SchGrClss, ncol=1, nrow=length(SchGrClss) )
SchGrClssEnd - as.data.frame(SchGrClssTmp)

# Assign n.Teachers (2 classroom in a given school-grade)
Allpairs - as.data.frame(t(combn(SchGrClssTmp, 2)))
AllpairsTmp - paste(Allpairs$V1, Allpairs$V2, sep= )

library(stringr)
separoPairs - as.data.frame(str_split_fixed(AllpairsTmp,  , 6))
head(separoPairs)

Muchas gracias! Estoy aprendiendo un monto gracias a vos!

Ignacio





On Tue, Jul 14, 2015 at 3:31 AM Carlos Ortega c...@qualityexcellence.es
wrote:

 OK.
 Bueno, para esa última parte para tener un data.frame con toda la
 información, ya filtrada y con los datos de los profesores puedes hacer
 esto:

 #--

 #Si a los validPairs tengo que asignar T profesores
 t - 10
 teachers - data.frame(
Name=sample(paste(Prof_,1:t, sep=),t)
   ,Speciality=sample(paste(Spec_,1:t, sep=),t)
   ,Age=sample(25:60,t)
   )

 placesEnd - validPairs[sample(1:nrow(validPairs), t), ]
 row.names(placesEnd) - NULL
 placesEndRed - placesEnd[,c(1,2,3,6)]
 names(placesEndRed) - c(School, Grade, Class_1, Class_2)
 endAssig - cbind.data.frame(placesEndRed, teachers)
 endAssig

 #--

 Que produce este tipo de resultado:

  endAssig
School Grade Class_1 Class_2Name Speciality Age
 1 e11g2  c3 c18  Prof_2 Spec_5  39
 2 e11g2  c5 c16  Prof_8 Spec_1  49
 3 e12g1  c3 c17  Prof_1Spec_10  36
 4  e2g2 c15 c17 Prof_10 Spec_9  29
 5  e1g3  c9 c15  Prof_3 Spec_6  55
 6  e6g3  c2 c18  Prof_6 Spec_8  42
 7 e17g2  c9 c14  Prof_4 Spec_3  27
 8 e18g3  c2 c12  Prof_7 Spec_2  53
 9 e13g1 c10 c20  Prof_9 Spec_4  58
 10e18g2  c4 c19  Prof_5 Spec_7  59

 Saludos,
 Carlos Ortega
 www.qualityexcellence.es


 El 14 de julio de 2015, 1:00, Ignacio Martinez ignaci...@gmail.com
 escribió:

 Perdon por no se lo suficientemente claro :(

 Tu codigo produce `validPairs` que tiene 7 variables y 360 observaciones.
 Donde

Re: [R-es] Crear datos aleatorios con restriciones

2015-07-14 Thread Ignacio Martinez
Este codigo resuelve mi problema. Estoy usando `str_split` y como separador
'- '. Tambien tengo que usar 'trimws'. Supongo que se podria limpiar el
codigo para hacerlo mas eficiente, pero todavia no se me ocurrio como. *Muchas
gracias  Carlos!*


library(dplyr)
library(randomNames)
library(geosphere)
set.seed(7142015)
# Define Parameters
n.Schools - 20
first.grade-3
last.grade-5
n.Grades -last.grade-first.grade+1
n.Classrooms - 4
n.Teachers - (n.Schools*n.Grades*n.Classrooms)/2 #Two classrooms per
teacher

# Define Random names function:
gen.names - function(n, which.names = both, name.order = last.first){
  names - unique(randomNames(n=n, which.names = which.names, name.order =
name.order))
  need - n - length(names)
  while(need0){
names - unique(c(randomNames(n=need, which.names = which.names,
name.order = name.order), names))
need - n - length(names)
  }
  return(names)
}

# Generate n.Schools names
gen.schools - function(n.schools) {
  School.ID -
paste0(gen.names(n = n.schools, which.names = last), ' School')
  School.long - rnorm(n = n.schools, mean = 21.7672, sd = 0.025)
  School.lat - rnorm(n = n.schools, mean = 58.8471, sd = 0.025)
  School.RE - rnorm(n = n.schools, mean = 0, sd = 1)
  Schools -
data.frame(School.ID, School.lat, School.long, School.RE) %%
mutate(School.ID = as.character(School.ID)) %%
rowwise() %%  mutate (School.distance = distHaversine(
  p1 = c(School.long, School.lat),
  p2 = c(21.7672, 58.8471), r = 3961
))
  return(Schools)
}

Schools - gen.schools(n.schools = n.Schools)

# Generate Grades
Grades - c(first.grade:last.grade)

# Generate n.Classrooms

Classrooms - LETTERS[1:n.Classrooms]

# Group schools and grades

SchGr - outer(paste0(Schools$School.ID, '-'), paste0(Grades, '-'),
FUN=paste)
#head(SchGr)

# Group SchGr and Classrooms

SchGrClss - outer(SchGr, paste0(Classrooms, '-'), FUN=paste)
#head(SchGrClss)

# These are the combination of  School-Grades-Classroom
SchGrClssTmp - as.matrix(SchGrClss, ncol=1, nrow=length(SchGrClss) )
SchGrClssEnd - as.data.frame(SchGrClssTmp)

# Assign n.Teachers (2 classroom in a given school-grade)
Allpairs - as.data.frame(t(combn(SchGrClssTmp, 2)))
AllpairsTmp - paste(Allpairs$V1, Allpairs$V2, sep= )

library(stringr)
separoPairs - as.data.frame(str_split(string = AllpairsTmp, pattern = -))
separoPairs - as.data.frame(t(separoPairs))
row.names(separoPairs) - NULL
separoPairs - separoPairs %% select(-V7)  %%  #Drops empty column
  mutate(V1=as.character(V1), V4=as.character(V4), V2=as.numeric(V2),
V5=as.numeric(V5)) %% mutate(V4 = trimws(V4, which = both))

separoPairs[120,]$V4
#Only the rows with V1=V4 and V2=V5 are valid
validPairs - separoPairs %% filter(V1==V4  V2==V5) %% select(V1, V2,
V3, V6)

# Generate n.Teachers

gen.teachers - function(n.teachers){
  Teacher.ID - gen.names(n = n.teachers, name.order = last.first)
  Teacher.exp - runif(n = n.teachers, min = 1, max = 30)
  Teacher.Other - sample(c(0,1), replace = T, prob = c(0.5, 0.5), size =
n.teachers)
  Teacher.RE - rnorm(n = n.teachers, mean = 0, sd = 1)
  Teachers - data.frame(Teacher.ID, Teacher.exp, Teacher.Other, Teacher.RE)
  return(Teachers)
}
Teachers - gen.teachers(n.teachers = n.Teachers) %%
  mutate(Teacher.ID = as.character(Teacher.ID))

# Randomly assign n.Teachers teachers to the ValidPairs
TmpAssignments - validPairs[sample(1:nrow(validPairs), n.Teachers), ]
Assignments - cbind.data.frame(Teachers$Teacher.ID, TmpAssignments)
names(Assignments) - c(Teacher.ID, School.ID, Grade, Class_1,
Class_2)

# Tidy Data
library(tidyr)
TeacherClassroom - Assignments %%
  gather(x, Classroom, Class_1,Class_2) %%
  select(-x) %%
  mutate(Teacher.ID = as.character(Teacher.ID))

# Merge
DF_Classrooms - TeacherClassroom %% full_join(Teachers, by=Teacher.ID)
%% full_join(Schools, by=School.ID)

On Tue, Jul 14, 2015 at 10:35 AM Ignacio Martinez ignaci...@gmail.com
wrote:

 Genial Carlos! Tu codigo produce lo que quiero!

 Estoy tratando de entender cada paso y hacer algunos cambios. Mi problema
 es con como usar `str_plit_fixed`. Con tu codigo tengo eso:

  separoPairs - as.data.frame(str_split_fixed(AllpairsTmp,  , 6))

 head(separoPairs)

   V1 V2 V3 V4 V5 V6
 1 e1 g1 c1 e2 g1 c1
 2 e1 g1 c1 e3 g1 c1
 3 e1 g1 c1 e4 g1 c1
 4 e1 g1 c1 e5 g1 c1
 5 e1 g1 c1 e6 g1 c1
 6 e1 g1 c1 e7 g1 c1


 V1 y V4 son el nombre de las escuelas, V2 y V5 del grado y V3 y V6 de la
 division. Yo hice unos cambios para tener datos un poco mas complejos, pero
 como resultado inintencional no puedo producir `separoPairs` Esto es lo que
 mi codigo produce:

  head(separoPairs)  V1 V2 V3V4 V5 V6
 1 Aslamy School  3 grade  A  Maruyama School 3 grade A
 2 Aslamy School  3 grade  A Smith School 3 grade A
 3 Aslamy School  3 grade  A   Linares School 3 grade A
 4 Aslamy School  3 grade  A   Dieyleh School 3 grade A
 5 Aslamy School  3 grade  A Hernandez School 3 grade A
 6 Aslamy School  3 grade  A   Padgett School 3 grade A


 Se puede

[R] ivreg with fixed effect in R?

2013-10-19 Thread Ignacio Martinez
I want to estimate the following fixed effect model:
y_i,t = alpha_i + beta_1 x1_t + beta_2 x2_i,tx2_i,t = gamma_i + gamma_1
x1_t + gamma_2 Z1_i + gamma_3 Z2_i
I can use ivreg from AER to do the iv regression.
fm - ivreg(y_i,t ~  x1_t + x2_i,t |x1_t + Z1_i + Z2_i,
data = DataSet)
But, I'm not sure how can I add the fixed effects.

Thanks!

[[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] ivreg with fixed effect in R?

2013-10-19 Thread Ignacio Martinez
Thanks.

One more question. When I do summary to see the coefficients how can i tell
R to print just some of the coefficients?

Thanks again


On Sat, Oct 19, 2013 at 7:29 AM, Achim Zeileis achim.zeil...@uibk.ac.atwrote:

 On Sat, 19 Oct 2013, Ignacio Martinez wrote:

  I want to estimate the following fixed effect model:
 y_i,t = alpha_i + beta_1 x1_t + beta_2 x2_i,tx2_i,t = gamma_i + gamma_1
 x1_t + gamma_2 Z1_i + gamma_3 Z2_i
 I can use ivreg from AER to do the iv regression.
 fm - ivreg(y_i,t ~  x1_t + x2_i,t |x1_t + Z1_i + Z2_i,
data = DataSet)
 But, I'm not sure how can I add the fixed effects.


 You can simply add a factor coding the variable with respect to which you
 want to compute fixed effects. In help(Fatalities, package = AER) there
 is an example (starting with pp. 360) that shows how to do this for OLS.
 In principle, the same approach can be used for IV.

  Thanks!

 [[alternative HTML version deleted]]

 __**
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/**
 posting-guide.html 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] lapply to multivariate function?

2013-09-01 Thread Ignacio Martinez
I have a Data Frame that contains, between other things, the following
fields: userX, Time1, Time2, Time3. The number of observations is 2000.

I have a function that has as inputs userX, Time1, Time2, Time3 and return
a data frame with 1 observation and 19 variables.

I want to apply that function to all the observations of the first data
frame to make a new data frame with 2000 observations and 19 variables.

I thought about using lapply, but if I understand correctly, it only takes
one variable.

Can somebody point me in the right direction?

Thanks!

[[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] lapply to multivariate function?

2013-09-01 Thread Ignacio Martinez
,






 28L, 936L, 957L,
988L), class = data.frame)



But with over 2000 observation.

And this is the other data frame

structure(list(anon_ID = c(exampleID1, exampleID2, exampleID3 ),
maxGrade = c(10, 5, 10), firstGrade = c(10, 5, 8), lastGrade = c(10,
5, 10), total_submissions = c(1L, 1L, 3L), Time1 =
structure(c(1361993741, 1362356090, 1362357401), class = c(POSIXct,
POSIXt), tzone = ), TimeM = structure(c(1361993741, 1362356090,
1362492744), class = c(POSIXct, POSIXt), tzone = ), TimeL =
structure(c(1361993741, 1362356090, 1362492744), class = c(POSIXct,
POSIXt), tzone = )), .Names = c(anon_ID, maxGrade,
firstGrade, lastGrade, total_submissions, Time1, TimeM,
TimeL), row.names = c(NA, 3L), class = data.frame)


But with a lot more observations.


What I want to do is to call  function (userX, Time1, Time2, Time3)
for all the user in the second data frame where Time1=Time1,
Time2=TimeM, Time3=TimeL


I hope that is more clear.


Thanks a lot for all the help!



On Sun, Sep 1, 2013 at 11:33 AM, Bert Gunter gunter.ber...@gene.com wrote:

 Oh, another possibility is ?mapply, which I should have pointed out in my
 previous reply. Sorry.

 -- Bert


 On Sun, Sep 1, 2013 at 8:30 AM, Bert Gunter bgun...@gene.com wrote:

 Rui et.al.:

 But apply will not work if the data frame has columns of different
 classes/types, as appears to be the case here. Viz, from ?apply:

 If X is not an array but an object of a class with a non-null 
 dimhttp://127.0.0.1:12824/help/library/base/help/dim
  value (such as a data frame),apply attempts to coerce it to an array via
  as.matrix if it is two-dimensional (e.g., a data frame) or via as.array.
 

 Simply looping by rows (via for() ) appears to be the simplest and
 probably fastest solution. There are other ways via tapply() and friends,
 but these are also essentially loops and are likely to incur some
 additional overhead.

 All assuming I understand what the OP has requested, of course.

 Cheers,

 Bert


 On Sun, Sep 1, 2013 at 7:31 AM, Rui Barradas ruipbarra...@sapo.ptwrote:

 Hello,

 Maybe you need apply, not lapply. It seems you want to apply() a
 function to the first dimension of your data.frame, something like

 apply(dat, 1, fun)  #apply by rows


 Hope this helps,

 Rui Barradas

 Em 01-09-2013 15:00, Ignacio Martinez escreveu:

 I have a Data Frame that contains, between other things, the following
 fields: userX, Time1, Time2, Time3. The number of observations is 2000.

 I have a function that has as inputs userX, Time1, Time2, Time3 and
 return
 a data frame with 1 observation and 19 variables.

 I want to apply that function to all the observations of the first data
 frame to make a new data frame with 2000 observations and 19 variables.

 I thought about using lapply, but if I understand correctly, it only
 takes
 one variable.

 Can somebody point me in the right direction?

 Thanks!

 [[alternative HTML version deleted]]

 __**
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/**
 posting-guide.html 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-helphttps://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/**
 posting-guide.html http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.




 --

 Bert Gunter
 Genentech Nonclinical Biostatistics

 Internal Contact Info:
 Phone: 467-7374
 Website:

 http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm





 --

 Bert Gunter
 Genentech Nonclinical Biostatistics

 Internal Contact Info:
 Phone: 467-7374
 Website:

 http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



[[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] lapply to multivariate function?

2013-09-01 Thread Ignacio Martinez
, 10), firstGrade = c(10, 5, 8), lastGrade = c(10, 5,
10), total_submissions = c(1L, 1L, 3L), Time1 = structure(c(1361993741,
1362356090, 1362357401), class = c(POSIXct, POSIXt), tzone = ), TimeM
= structure(c(1361993741, 1362356090, 1362492744), class = c(POSIXct,
POSIXt), tzone = ), TimeL = structure(c(1361993741, 1362356090,
1362492744), class = c(POSIXct, POSIXt), tzone = )), .Names =
c(anon_ID, maxGrade, firstGrade, lastGrade, total_submissions,
Time1, TimeM, TimeL), row.names = c(NA, 3L), class = data.frame)

library(foreach)
library(doMC)
registerDoMC(2)  #change the 2 to your number of CPU cores

n - nrow(data)
res - list(vector, n)
foreach(i=1:n, .verbose=FALSE, .combine=rbind) %do% {
  res[[i]] - with(data, DataVideoActionT(anon_ID[i], Time1[i], TimeM[i],
TimeL[i]))
}
test-do.call(rbind, res)

I have 2 questions.

1. How can I make foreach not print to the console?

2. I want to run this in parallel, I i change the %do% for %dopar% the code
stop working. Instead of getting test with 3 observations and 19 variables
I get a 2x1 character matrix


Thanks!



On Sun, Sep 1, 2013 at 3:00 PM, Rui Barradas ruipbarra...@sapo.pt wrote:

 Hello,

 Your example doesn't really run, but for what I've seen, if your second
 data frame is named dat2, something along the lines of

 n - nrow(dat2)
 res - list(vector, n)
 for(i in 1:n){
 res[[i]] - with(dat2, DataVideoActionT(anon_ID[i], Time1[i],
 TimeM[i], TimeL[i]))
 }

 do.call(rbind, res)


 Rui Barradas

 Em 01-09-2013 17:40, Ignacio Martinez escreveu:

 I hope this reproduceble example helps understand what I'm trying to do.

 This is the function:

 # Make Data Frame for video actions between given times for user X
 DataVideoActionT - function (userX, Time1, Time2, Time3){
#Get data for user X
videoActionsX-subset(**videoLectureActions, username==userX)
#Time1 = before first attempt
videoActionsX_T1-subset(**videoActionsX, eventTimestampTime1)
#Time2 = before best attemp
videoActionsX_T2-subset(**videoActionsX, eventTimestampTime2 
 eventTimestampTime1)
#Time3= before last attemp
videoActionsX_T3-subset(**videoActionsX, eventTimestampTime3 
 eventTimestampTime1)

error1 = sum(videoActionsX_T1$type== error )
pause1 = sum(videoActionsX_T1$type== pause )
play1 = sum(videoActionsX_T1$type== play )
ratechange1 = sum(videoActionsX_T1$type== ratechange )
seeked1 = sum(videoActionsX_T1$type== seeked )
stalled1 = sum(videoActionsX_T1$type== stalled )

error2 = sum(videoActionsX_T2$type== error )
pause2 = sum(videoActionsX_T2$type== pause )
play2 = sum(videoActionsX_T2$type== play )
ratechange2 = sum(videoActionsX_T2$type== ratechange )
seeked2 = sum(videoActionsX_T2$type== seeked )
stalled2 = sum(videoActionsX_T2$type== stalled )

error3 = sum(videoActionsX_T3$type== error )
pause3 = sum(videoActionsX_T3$type== pause )
play3 = sum(videoActionsX_T3$type== play )
ratechange3 = sum(videoActionsX_T3$type== ratechange )
seeked3 = sum(videoActionsX_T3$type== seeked )
stalled3 = sum(videoActionsX_T3$type== stalled )

data-data.frame(anon_ID=**userX,
 error1 = error1,
 pause1 = pause1,
 play1 = play1,
 ratechange1 = ratechange1,
 seeked1=seeked1,
 stalled1=stalled1,
 error2 = error2,
 pause2 = pause2,
 play2 = play2,
 ratechange2 = ratechange2,
 seeked2 =seeked2,
 stalled2 = stalled2,
 error3 = error3,
 pause3 = pause3,
 play3 = play3,
 ratechange3 = ratechange3,
 seeked3 = seeked3,
 stalled3 = stalled3)
return(data)
 }

 This is the videoActionsX  dataframe:

 structure(list(username = c(exampleID1, exampleID1, exampleID1,
  exampleID2, exampleID2, exampleID2,
 exampleID3, exampleID3,
  exampleID3, exampleID3), currentTime =
 c(103.701247, 103.701247,

   107.543877, 107.543877, 116.456507, 116.456507, 119.987188,

   177.816693, 183.417124, 183.417124), playbackRate = c(null,

   null, null,
 null, null, null, null, null, null,

   null), pause =
 c(true, false, true, false, true,


 false, true, false, true, false), error = c(null,


null, null,
 null, null, null, null, null, null,


null),
 networkState
 = c(1, 1, 1, 1, 1, 1, 1,



  1, 1, 1), readyState = c(4, 4, 4, 4, 4, 4,



 4, 4, 4, 4), lectureID =
 c(exampleLectureID1, exampleLectureID1,




 exampleLectureID1, exampleLectureID1

[R] [SQL]

2013-04-25 Thread Ignacio Martinez
Hi,

The data for my new project are in a bunch of .sql files, instead of the
clasic csv files that I'm used to work with.

Could someone explain to me how to read these files into R?

Thanks,

-Ignacio

[[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] Finding a max

2012-11-21 Thread Ignacio Martinez
My data looks like this:

X Y1(X) Y2(X)

i want to find the values of x that maximize Y1 and Y2.
Right now I'm getting the answer but I would like to know if there is a
more efficient/elegant way of doing this.

This code reproduces what I'm doing:
[code]
prop3-structure(list(effort = c(0, 0.008989899, 0.017979798, 0.026969697,
  0.035959596, 0.044949495, 0.053939394,
0.062929293, 0.071919192,
  0.080909091, 0.08989899, 0.09889,
0.107878788, 0.116868687,
  0.125858586, 0.134848485, 0.143838384,
0.152828283, 0.161818182,
  0.170808081, 0.17979798, 0.188787879,
0.19778, 0.206767677,
  0.215757576, 0.224747475, 0.233737374,
0.242727273, 0.251717172,
  0.260707071, 0.26969697, 0.278686869,
0.287676768, 0.29667,
  0.305656566, 0.314646465, 0.323636364,
0.332626263, 0.341616162,
  0.350606061, 0.35959596, 0.368585859,
0.377575758, 0.386565657,
  0.39556, 0.404545455, 0.413535354,
0.422525253, 0.431515152,
  0.440505051, 0.44949495, 0.458484849,
0.467474748, 0.476464647,
  0.485454546, 0.49445, 0.503434344,
0.512424243, 0.521414142,
  0.530404041, 0.53939394, 0.548383839,
0.557373738, 0.566363637,
  0.575353536, 0.584343435, 0.59334,
0.602323233, 0.611313132,
  0.620303031, 0.62929293, 0.638282829,
0.647272728, 0.656262627,
  0.665252526, 0.674242425, 0.683232324,
0.69223, 0.701212122,
  0.710202021, 0.71919192, 0.728181819,
0.737171718, 0.746161617,
  0.755151516, 0.764141415, 0.773131314,
0.782121213, 0.79112,
  0.800101011, 0.80909091, 0.818080809,
0.827070708, 0.836060607,
  0.845050506, 0.854040405, 0.863030304,
0.872020203, 0.881010102,
  0.89001), Low = c(7118.22889879,
7198.74588723, 7202.19756567,
7205.63654441,
7209.06211889, 7212.47354473, 7215.87003521, 7219.25075859,
7222.61483517,
7225.96133418, 7229.28927041, 7232.59760064, 7235.88521975,
7239.15095652,
7242.39356918, 7245.61174055, 7248.8040728, 7251.96908185,
7255.10519126,
7258.21072566, 7261.28390362, 7264.32282997, 7267.32548746,
7270.28972767,
7273.21326125, 7276.0936472, 7278.92828132, 7281.7143836,
7284.44898447,
7287.12890981, 7289.75076469, 7292.31091545, 7294.80547025,
7297.23025772,
7299.5808036, 7301.85230504, 7304.03960253, 7306.13714891,
7308.13897531,
7310.03865366, 7311.82925527, 7313.50330516, 7315.05273141,
7316.46880922,
7317.74209876, 7318.86237619, 7319.81855692, 7320.59861012,
7321.18946333,
7321.57689588, 7321.74541961, 7321.678145, 7321.35663099,
7320.76071577,
7319.86832617, 7318.65526223, 7317.09495336, 7315.15818179,
7312.81276812,
7310.02321309, 7306.7502885, 7302.95056882, 7298.57589369,
7293.57274947,
7287.8817, 7281.43583878, 7274.16127505, 7265.97457404,
7256.78217653,
7246.47872816, 7234.94528475, 7222.04719437, 7207.63158811,
7191.52439555,
7173.52677949, 7153.41085818, 7130.91454853, 7105.73531868,
7077.52257998,
7045.86837049, 7010.29587987, 6970.24522727, 6925.05571843,
6873.94355449,
6815.97361626, 6750.02346243, 6674.73699741, 6588.46429282,
6489.18264441,
6374.39189638, 6240.97402156, 6085.00235313, 5901.47881048,
5683.96641688,
5424.06674216, 5110.66298423, 4728.80081891, 4257.99514316,
3669.60082983,
2922.6078062), High = c(7118.22889879, 7198.73069287,

   7202.13567036, 7205.49469474, 7208.80521654, 7212.06454229,
7215.26982532,

   7218.41805599, 7221.50605116, 7224.53044288, 7227.48766626,
7230.37394647,

   7233.18528464, 7235.91744284, 7238.56592778, 7241.12597328,
7243.59252142,

   7245.96020213, 7248.22331114, 7250.3757862, 7252.41118136,
7254.32263905,

   7256.1028599, 7257.74407005, 7259.23798555, 

Re: [R] Finding a max

2012-11-21 Thread Ignacio Martinez
Thanks a lot!


On Wed, Nov 21, 2012 at 12:51 PM, Rui Barradas ruipbarra...@sapo.pt wrote:

 Hello,

 You're complicating what is simple:


 prop3$effort[which.max(prop3$**Low)]  # First maximum of Low
 prop3$effort[which.max(prop3$**High)] # Ditto, of High

 which.max(prop3$Low)   # Row number that maximizes Low
 which.max(prop3$High)  # Row number that maximizes High


 Hope this helps,

 Rui Barradas

 Em 21-11-2012 17:10, Ignacio Martinez escreveu:

 My data looks like this:

 X Y1(X) Y2(X)

 i want to find the values of x that maximize Y1 and Y2.
 Right now I'm getting the answer but I would like to know if there is a
 more efficient/elegant way of doing this.

 This code reproduces what I'm doing:
 [code]
 prop3-structure(list(effort = c(0, 0.008989899, 0.017979798, 0.026969697,
0.035959596, 0.044949495, 0.053939394,
 0.062929293, 0.071919192,
0.080909091, 0.08989899, 0.09889,
 0.107878788, 0.116868687,
0.125858586, 0.134848485, 0.143838384,
 0.152828283, 0.161818182,
0.170808081, 0.17979798, 0.188787879,
 0.19778, 0.206767677,
0.215757576, 0.224747475, 0.233737374,
 0.242727273, 0.251717172,
0.260707071, 0.26969697, 0.278686869,
 0.287676768, 0.29667,
0.305656566, 0.314646465, 0.323636364,
 0.332626263, 0.341616162,
0.350606061, 0.35959596, 0.368585859,
 0.377575758, 0.386565657,
0.39556, 0.404545455, 0.413535354,
 0.422525253, 0.431515152,
0.440505051, 0.44949495, 0.458484849,
 0.467474748, 0.476464647,
0.485454546, 0.49445, 0.503434344,
 0.512424243, 0.521414142,
0.530404041, 0.53939394, 0.548383839,
 0.557373738, 0.566363637,
0.575353536, 0.584343435, 0.59334,
 0.602323233, 0.611313132,
0.620303031, 0.62929293, 0.638282829,
 0.647272728, 0.656262627,
0.665252526, 0.674242425, 0.683232324,
 0.69223, 0.701212122,
0.710202021, 0.71919192, 0.728181819,
 0.737171718, 0.746161617,
0.755151516, 0.764141415, 0.773131314,
 0.782121213, 0.79112,
0.800101011, 0.80909091, 0.818080809,
 0.827070708, 0.836060607,
0.845050506, 0.854040405, 0.863030304,
 0.872020203, 0.881010102,
0.89001), Low = c(7118.22889879,
 7198.74588723, 7202.19756567,
  7205.63654441,
 7209.06211889, 7212.47354473, 7215.87003521, 7219.25075859,
  7222.61483517,
 7225.96133418, 7229.28927041, 7232.59760064, 7235.88521975,
  7239.15095652,
 7242.39356918, 7245.61174055, 7248.8040728, 7251.96908185,
  7255.10519126,
 7258.21072566, 7261.28390362, 7264.32282997, 7267.32548746,
  7270.28972767,
 7273.21326125, 7276.0936472, 7278.92828132, 7281.7143836,
  7284.44898447,
 7287.12890981, 7289.75076469, 7292.31091545, 7294.80547025,
  7297.23025772,
 7299.5808036, 7301.85230504, 7304.03960253, 7306.13714891,
  7308.13897531,
 7310.03865366, 7311.82925527, 7313.50330516, 7315.05273141,
  7316.46880922,
 7317.74209876, 7318.86237619, 7319.81855692, 7320.59861012,
  7321.18946333,
 7321.57689588, 7321.74541961, 7321.678145, 7321.35663099,
  7320.76071577,
 7319.86832617, 7318.65526223, 7317.09495336, 7315.15818179,
  7312.81276812,
 7310.02321309, 7306.7502885, 7302.95056882, 7298.57589369,
  7293.57274947,
 7287.8817, 7281.43583878, 7274.16127505, 7265.97457404,
  7256.78217653,
 7246.47872816, 7234.94528475, 7222.04719437, 7207.63158811,
  7191.52439555,
 7173.52677949, 7153.41085818, 7130.91454853, 7105.73531868,
  7077.52257998,
 7045.86837049, 7010.29587987, 6970.24522727, 6925.05571843,
  6873.94355449,
 6815.97361626, 6750.02346243, 6674.73699741, 6588.46429282,
  6489.18264441,
 6374.39189638, 6240.97402156, 6085.00235313, 5901.47881048,
  5683.96641688

Re: [R] Plot 3 lines in one graph

2012-11-06 Thread Ignacio Martinez
Hi David.

Using the two points to compute slopes and intercepts for the lines and
then plotting is exactly what I want to do.


Thanks!


On Tue, Nov 6, 2012 at 12:16 AM, David L Carlson dcarl...@tamu.edu wrote:

 matplot works just fine, but you only have two data points, at -4800 and at
 -2800. You chop the x axis at -4500 so that the first point is outside the
 graph window and extend it to -100 which is far beyond your point at -2800.
 If you want to project the lines, you will have to add points.

  x - read.table(text=V1   V2   V3  V41
 +  -4800 25195.73 7415.219 7264.282
 + -2800 15195.73 5415.219 7264.28,
 +  header=TRUE)
  x
  V1   V2   V3  V41
 1 -4800 25195.73 7415.219 7264.282
 2 -2800 15195.73 5415.219 7264.280

 Did you want to use the two points to compute a slopes and intercepts for
 lines and then plot those lines?

 --
 David L Carlson
 Associate Professor of Anthropology
 Texas AM University
 College Station, TX 77843-4352



  -Original Message-
  From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
  project.org] On Behalf Of Peter Alspach
  Sent: Monday, November 05, 2012 10:41 PM
  To: Ignacio Martinez; r-help
  Subject: Re: [R] Plot 3 lines in one graph
 
  Tena koe Ignacio
 
  I cannot follow you example (you might care to read the posting guide,
  link at end, to help you in this regard).  However, the usual way to
  plot three lines in one graph is to use lines.  For example,
 
  yourData - data.frame(x=1:2, y1=runif(2), y2=runif(2), y3=runif(2))
  with(yourData, plot(x, y1, ylim=range(unlist(yourData[,-1])),
  type='l'))
  with(yourData, lines(x, y2, col='red3'))
  with(yourData, lines(x, y3, col='blue2', lty='dashed'))
 
  I hope this is of some help ...
 
  Peter Alspach
 
  -Original Message-
  From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
  project.org] On Behalf Of Ignacio Martinez
  Sent: Tuesday, 6 November 2012 12:10 p.m.
  To: r-help
  Subject: [R] Plot 3 lines in one graph
 
  I'm new with R. I want to plot 3 lines in one graph. This is my data:
 
  print(x)
   V1   V2   V3  V41 -4800 25195.73 7415.219 7264.282
  -2800 15195.73 5415.219 7264.28
 
  I tried using matplot, but I cannot get exactly what I want. This is
  what I get, and this is my code:
 
  matplot(x[,1],x[,-1],type='b', xlab = epsilon_h,
  ylab = Value2, xlim= range(-4500,-100),
  col = c(blue,green,red), pch=1:3)
  ex12 - expression(V(h == 40),
 V(h==20),
 V(h==0))
  legend(topright, ex12, col = c(blue,green,red), pch=1:3)
 
  I would like to make the lines extend so I can see the intersections.
 
 
  The other, fancier and better looking, option that i found is ggplot2.
  But for what I understand from the
  examplehttp://wiki.stdout.org/rcookbook/Graphs/Scatterplots%20(ggplot2
  )/I
  would need to reshape my data to something like this
 
   id   x   y  1 1 -4800   25195.73
  2 1 -2800   15195.733 2 -4800   7415.2194 2
  -2800   5415.2195 3 -4800   7264.286 3 -2800   7264.28
 
  Thanks a lot for the help!
 
  -Ignacio
 
[[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.
 
  The contents of this e-mail are confidential and may be
  ...{{dropped:14}}
 
  __
  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] Plot 3 lines in one graph

2012-11-06 Thread Ignacio Martinez
Thanks a lot!


On Tue, Nov 6, 2012 at 9:31 AM, David L Carlson dcarl...@tamu.edu wrote:

 Try this

 ** **

 x - read.table(text=V1   V2   V3  V41

   -4800 25195.73 7415.219 7264.282

 -2800 15195.73 5415.219 7264.28,

   header=TRUE)

 x

 slopes - apply(x[1:2,2:4], 2, diff)/diff(x[,1])

 inters - x[1,2:4]-slopes*x[1,1]

 x[3,] - cbind(-100, inters+slopes*-100)

 matplot(x[,1], x[,2:4], type=b)

 ** **

 ** **

 --

 David L Carlson

 Associate Professor of Anthropology

 Texas AM University

 College Station, TX 77843-4352

 ** **

 *From:* Ignacio Martinez [mailto:ignaci...@gmail.com]
 *Sent:* Tuesday, November 06, 2012 6:22 AM
 *To:* dcarl...@tamu.edu
 *Cc:* Peter Alspach; r-help

 *Subject:* Re: [R] Plot 3 lines in one graph

 ** **

 Hi David.

 ** **

 Using the two points to compute slopes and intercepts for the lines and
 then plotting is exactly what I want to do.

 ** **

 ** **

 Thanks!

 ** **

 On Tue, Nov 6, 2012 at 12:16 AM, David L Carlson dcarl...@tamu.edu
 wrote:

 matplot works just fine, but you only have two data points, at -4800 and at
 -2800. You chop the x axis at -4500 so that the first point is outside the
 graph window and extend it to -100 which is far beyond your point at -2800.
 If you want to project the lines, you will have to add points.

  x - read.table(text=V1   V2   V3  V41
 +  -4800 25195.73 7415.219 7264.282
 + -2800 15195.73 5415.219 7264.28,
 +  header=TRUE)
  x
  V1   V2   V3  V41
 1 -4800 25195.73 7415.219 7264.282
 2 -2800 15195.73 5415.219 7264.280

 Did you want to use the two points to compute a slopes and intercepts for
 lines and then plot those lines?

 --
 David L Carlson
 Associate Professor of Anthropology
 Texas AM University
 College Station, TX 77843-4352




  -Original Message-
  From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-

  project.org] On Behalf Of Peter Alspach
  Sent: Monday, November 05, 2012 10:41 PM
  To: Ignacio Martinez; r-help
  Subject: Re: [R] Plot 3 lines in one graph
 
  Tena koe Ignacio
 
  I cannot follow you example (you might care to read the posting guide,
  link at end, to help you in this regard).  However, the usual way to
  plot three lines in one graph is to use lines.  For example,
 
  yourData - data.frame(x=1:2, y1=runif(2), y2=runif(2), y3=runif(2))
  with(yourData, plot(x, y1, ylim=range(unlist(yourData[,-1])),
  type='l'))
  with(yourData, lines(x, y2, col='red3'))
  with(yourData, lines(x, y3, col='blue2', lty='dashed'))
 
  I hope this is of some help ...
 
  Peter Alspach
 
  -Original Message-
  From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
  project.org] On Behalf Of Ignacio Martinez
  Sent: Tuesday, 6 November 2012 12:10 p.m.
  To: r-help
  Subject: [R] Plot 3 lines in one graph
 
  I'm new with R. I want to plot 3 lines in one graph. This is my data:
 
  print(x)
   V1   V2   V3  V41 -4800 25195.73 7415.219 7264.282
  -2800 15195.73 5415.219 7264.28
 
  I tried using matplot, but I cannot get exactly what I want. This is
  what I get, and this is my code:
 
  matplot(x[,1],x[,-1],type='b', xlab = epsilon_h,
  ylab = Value2, xlim= range(-4500,-100),
  col = c(blue,green,red), pch=1:3)
  ex12 - expression(V(h == 40),
 V(h==20),
 V(h==0))
  legend(topright, ex12, col = c(blue,green,red), pch=1:3)
 
  I would like to make the lines extend so I can see the intersections.
 
 
  The other, fancier and better looking, option that i found is ggplot2.
  But for what I understand from the
  examplehttp://wiki.stdout.org/rcookbook/Graphs/Scatterplots%20(ggplot2
  )/I
  would need to reshape my data to something like this
 
   id   x   y  1 1 -4800   25195.73
  2 1 -2800   15195.733 2 -4800   7415.2194 2
  -2800   5415.2195 3 -4800   7264.286 3 -2800   7264.28
 
  Thanks a lot for the help!
 
  -Ignacio
 
[[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.
 
  The contents of this e-mail are confidential and may be

  ...{{dropped:14}}

 
  __
  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

[R] Plot 3 lines in one graph

2012-11-05 Thread Ignacio Martinez
I'm new with R. I want to plot 3 lines in one graph. This is my data:

print(x)
 V1   V2   V3  V41 -4800 25195.73 7415.219 7264.282
-2800 15195.73 5415.219 7264.28

I tried using matplot, but I cannot get exactly what I want. This is what I
get, and this is my code:

matplot(x[,1],x[,-1],type='b', xlab = epsilon_h,
ylab = Value2, xlim= range(-4500,-100),
col = c(blue,green,red), pch=1:3)
ex12 - expression(V(h == 40),
   V(h==20),
   V(h==0))
legend(topright, ex12, col = c(blue,green,red), pch=1:3)

I would like to make the lines extend so I can see the intersections.


The other, fancier and better looking, option that i found is ggplot2. But
for what I understand from the
examplehttp://wiki.stdout.org/rcookbook/Graphs/Scatterplots%20(ggplot2)/I
would need to reshape my data to something like this

 id   x   y  1 1 -4800   25195.73
2 1 -2800   15195.733 2 -4800   7415.2194 2
-2800   5415.2195 3 -4800   7264.286 3 -2800   7264.28

Thanks a lot for the help!

-Ignacio

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