Re: [R] Loop over columns of dataframe and change values condtionally

2021-09-02 Thread Rui Barradas

Hello,

In the particular case you have, to change to NA based on condition, use 
`is.na<-`.


Here is some test data, 3 times the same df.


set.seed(2021)
df3 <- df2 <- df1 <- data.frame(
  x = c(0, 0, 1, 2, 3),
  y = c(1, 2, 3, 0, 0),
  z = rbinom(5, 1, prob = c(0.25, 0.75)),
  a = letters[1:5]
)


# change all columns
is.na(df1) <- df1 == 0
df1

# only one column
is.na(df2[, 2]) <- df2[, 2] == 0
df2

# change several columns given by an index
is.na(df3[c(1, 3)]) <- df3[c(1, 3)] == 0
df3


Hope this helps,

Rui Barradas


Às 14:35 de 02/09/21, Luigi Marongiu escreveu:

Hello,
it is possible to select the columns of a dataframe in sequence with:
```
for(i in 1:ncol(df)) {
   df[ , i]
}
# or
for(i in 1:ncol(df)) {
   df[ i]
}
```
And change all values with, for instance:
```
for(i in 1:ncol(df)) {
   df[ , i] <- df[ , i] + 10
}
```
Is it possible to apply a condition? What would be the syntax?
For instance, to change all 0s in a column to NA would `df[i][df[i ==
0] = NA` be right?
Thank you




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


Re: [R] Loop over columns of dataframe and change values condtionally

2021-09-02 Thread PIKAL Petr
Hi

you could operate with whole data frame (sometimes)
head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1  5.1 3.5  1.4 0.2  setosa
2  4.9 3.0  1.4 0.2  setosa
3  4.7 3.2  1.3 0.2  setosa
4  4.6 3.1  1.5 0.2  setosa
5  5.0 3.6  1.4 0.2  setosa
6  5.4 3.9  1.7 0.4  setosa

chenge all

> head(iris[,1:4]+10) 
  Sepal.Length Sepal.Width Petal.Length Petal.Width
1 15.113.5 11.410.2
2 14.913.0 11.410.2
3 14.713.2 11.310.2
4 14.613.1 11.510.2
5 15.013.6 11.410.2
6 15.413.9 11.710.4

change only some
> iris[,1:4][iris[,1:4]<2] <- iris[,1:4][iris[,1:4]<2]+10
> head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1  5.1 3.5 11.410.2  setosa
2  4.9 3.0 11.410.2  setosa
3  4.7 3.2 11.310.2  setosa
4  4.6 3.1 11.510.2  setosa
5  5.0 3.6 11.410.2  setosa
6  5.4 3.9 11.710.4  setosa


Cheers
Petr


> -Original Message-
> From: R-help  On Behalf Of Luigi Marongiu
> Sent: Thursday, September 2, 2021 3:35 PM
> To: r-help 
> Subject: [R] Loop over columns of dataframe and change values condtionally
> 
> Hello,
> it is possible to select the columns of a dataframe in sequence with:
> ```
> for(i in 1:ncol(df)) {
>   df[ , i]
> }
> # or
> for(i in 1:ncol(df)) {
>   df[ i]
> }
> ```
> And change all values with, for instance:
> ```
> for(i in 1:ncol(df)) {
>   df[ , i] <- df[ , i] + 10
> }
> ```
> Is it possible to apply a condition? What would be the syntax?
> For instance, to change all 0s in a column to NA would `df[i][df[i == 0] =
NA`
> be right?
> Thank you
> 
> 
> --
> Best regards,
> Luigi
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
__
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] Loop for two columns and 154 rows

2020-09-16 Thread PIKAL Petr
Hi

 

It is recommended to keep your emails in Rhelp, there are others who could give 
you answer.

 

If I understand correctly you want to take 2 lines in your data frame and test 
them if they are same but just reversed.

 

One option could be to split the whole data frame in chunks. Based on your 
example

 

test <- structure(list(V1 = c("G1", "G2", "G3", "G1", "G20", "G21"), 

V2 = c("G2", "G1", "G1", "G3", "G21", "G16")), class = "data.frame", 
row.names = c(NA, -6L))

test.l <- split(test, rep(1:3, each=2))

res <- rep(NA, length(test.l))

for (i in 1:length(test.l)) res[i] <- 
prod(as.numeric(factor(unlist(test.l[[i]]

test$column3 <- rep(res==4, each=2)*1

 

Maybe some clever matrix operations could do it in more elegant way.

 

Cheers

Petr

 

S pozdravem | Best Regards

RNDr. Petr PIKAL
Vedoucí Výzkumu a vývoje | Research Manager

PRECHEZA a.s.
nábř. Dr. Edvarda Beneše 1170/24 | 750 02 Přerov | Czech Republic
Tel: +420 581 252 256 | GSM: +420 724 008 364
 <mailto:petr.pi...@precheza.cz> petr.pi...@precheza.cz |  
<https://www.precheza.cz/> www.precheza.cz

Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních 
partnerů PRECHEZA a.s. jsou zveřejněny na:  
<https://www.precheza.cz/zasady-ochrany-osobnich-udaju/> 
https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about 
processing and protection of business partner’s personal data are available on 
website:  <https://www.precheza.cz/en/personal-data-protection-principles/> 
https://www.precheza.cz/en/personal-data-protection-principles/

Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a 
podléhají tomuto právně závaznému prohlášení o vyloučení odpovědnosti:  
<https://www.precheza.cz/01-dovetek/> https://www.precheza.cz/01-dovetek/ | 
This email and any documents attached to it may be confidential and are subject 
to the legally binding disclaimer:  <https://www.precheza.cz/en/01-disclaimer/> 
https://www.precheza.cz/en/01-disclaimer/

 

From: Hesham A. AL-bukhaiti  
Sent: Tuesday, September 15, 2020 1:48 PM
To: PIKAL Petr 
Subject: Re: [R] Loop for two columns and 154 rows

 


thanks petr v much 
i attached my problem in word and my data 
i want take all elements them have relations not just G1 and G2 this just 
example test (G1 AND G2) i want make loop to take all elements:

G1 G2 1 
G2 G1 1
G3 G1 1
G1 G3 1
G20 G21 0
G21 G16 0 
(notice (G1 and G2 have relation and G3 and G1 also have but G20 ,G21,G16 do 
not have this relations we put 0 ,

(i have G1 to G154) them make 23400 relation i want to know wich forn theses 
relations are true (1) or not (0)

i hope you can help me.

On Tuesday, September 15, 2020, 02:54:27 AM PDT, PIKAL Petr 
mailto:petr.pi...@precheza.cz> > wrote: 

 

 

Sorry, forgot to copy to r help.

Petr


> -Original Message-
> From: PIKAL Petr
> Sent: Tuesday, September 15, 2020 11:53 AM
> To: 'Hesham A. AL-bukhaiti' mailto:hesham...@yahoo.com> 
> >
> Subject: RE: [R] Loop for two columns and 154 rows
> 
> Hi
> 
> Your mail is unreadable, post in plain text not HTML.
> 
> If I deciphered it correcttly you want all values which have G1 in column 1 
> and
> G2 in column 2 or G2 in column 1 and G1 in column to produce 1 all other
> produce 0
> 
> So if your data frame is named truth
> 
> truth$column3 <- ((truth[,1] =="G1" & truth[,2] =="G2") | (truth[,2] =="G1" &
> truth[,1] =="G2")) * 1
> 
> Cheers
> Petr
> 
> > -Original Message-
> > From: R-help  > <mailto:r-help-boun...@r-project.org> > On Behalf Of Hesham A. AL-
> > bukhaiti via R-help
> > Sent: Tuesday, September 15, 2020 11:01 AM
> > To: r-help@r-project.org <mailto:r-help@r-project.org> 
> > Subject: [R] Loop for two columns and 154 rows
> >
> >  Dears in R :i have this code in R:
> > # this for do not work true (i tried )out<-read.csv("outbr.csv") truth<-
> > out[,seq(1,2)]truth<-
> > cbind(as.character(truth[,1]),as.character(truth[,2])
> ,as.data.frame(rep(
> > 0,,dim(out)[1])));for (j in 1:2) {  for (i in 1:20) {truth[(truth[,1]== 
> > truth[j,i] &
> > truth[,2]== truth[j,i+1]) |(truth[,1]== truth[j+1,i] & truth[,2]==
> > truth[j+1,i+1]),3]<-1  } }
> > #truth<-out[,seq(1,2)]#truth<-
> > cbind(as.character(truth[,1]),as.character(truth[,2])  #
> ,as.data.frame(rep
> > (0,,dim(out)[1])));#truth[(truth[,1]=="G2" & truth[,2]=="G1") |
> (truth[,1]=="G1"
> > & truth[,2]=="G2"),3]<-1
> >
> ###

Re: [R] Loop inside dplyr::mutate

2020-05-09 Thread Richard M. Heiberger
## I start with sim_data_wide

sim_data_wide <- tidyr::spread(sim_data, quarter, pd)

## and calculate wide
wide1 <- with(sim_data_wide, cbind(PC_1 = P_1,
   PC_2 = 1-(1-P_1)*(1-P_2),
   PC_3 = 1-(1-P_1)*(1-P_2)*(1-P_3),
   PC_4 = 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4),
   PC_5 = 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5),
   PC_6 =
1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6),
   PC_7 =
1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6)*(1-P_7),
   PC_8 =
1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6)*(1-P_7)*(1-P_8),
   PC_9 =
1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6)*(1-P_7)*(1-P_8)*(1-P_9),
   PC_10 =
1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6)*(1-P_7)*(1-P_8)*(1-P_9)*(1-P_10)
  )
)

## this simpler sequence gets the same value

A <- 1-sim_data_wide[,2:11]
B <- t(apply(A, 1, cumprod)[-1,])
wide2 <- cbind(sim_data_wide[,2], 1-B)
dimnames(wide2)[[2]] <- paste0("PC_", 1:10)

all.equal(wide1, wide2)


On Sat, May 9, 2020 at 9:28 PM Jeff Newmiller 
wrote:

> Does this help?
>
> sim_wide2 <- (
> sim_data
> %>% arrange( borrower_id, quarter )
> %>% group_by( borrower_id )
> %>% mutate( cumpd = 1 - cumprod( 1 - pd ) )
> %>% ungroup()
> %>% mutate( qlbl = paste0( "PC_", quarter ) )
> %>% select( borrower_id, qlbl, cumpd )
> %>% spread( qlbl, cumpd )
> )
>
> On May 9, 2020 4:45:40 PM PDT, Axel Urbiz  wrote:
> >Hello,
> >
> >Is there a less verbose approach to obtaining the PC_i variables inside
> >the mutate?
> >
> >library(tidyverse)
> >sim_data <- data.frame(borrower_id = sort(rep(1:10, 20)),
> >   quarter = rep(1:20, 10),
> > pd = runif(length(rep(1:20, 10 # conditional probs
> >
> >sim_data_wide <- tidyr::spread(sim_data, quarter, pd)
> >colnames(sim_data_wide)[-1] <- paste0("P_",
> >colnames(sim_data_wide)[-1])
> >
> ># Compute cumulative probs
> >sim_data_wide <- sim_data_wide %>%
> >  mutate(PC_1 = P_1,
> > PC_2 = 1-(1-P_1)*(1-P_2),
> > PC_3 = 1-(1-P_1)*(1-P_2)*(1-P_3),
> > PC_4 = 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4),
> >  PC_5 = 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5),
> >  PC_6 = 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6),
> >  PC_7 = 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6)*(1-P_7),
> >PC_8 =
> >1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6)*(1-P_7)*(1-P_8),
> >PC_9 =
> >1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6)*(1-P_7)*(1-P_8)*(1-P_9),
> >PC_10 =
>
> >1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6)*(1-P_7)*(1-P_8)*(1-P_9)*(1-P_10)
> >)
> >
> >
> >Thanks,
> >Axel.
> >   [[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.
>
> --
> Sent from my phone. Please excuse my brevity.
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[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] Loop inside dplyr::mutate

2020-05-09 Thread Jeff Newmiller
Does this help?

sim_wide2 <- (
sim_data
%>% arrange( borrower_id, quarter )
%>% group_by( borrower_id )
%>% mutate( cumpd = 1 - cumprod( 1 - pd ) )
%>% ungroup()
%>% mutate( qlbl = paste0( "PC_", quarter ) )
%>% select( borrower_id, qlbl, cumpd )
%>% spread( qlbl, cumpd )
)

On May 9, 2020 4:45:40 PM PDT, Axel Urbiz  wrote:
>Hello, 
>
>Is there a less verbose approach to obtaining the PC_i variables inside
>the mutate?
>
>library(tidyverse)
>sim_data <- data.frame(borrower_id = sort(rep(1:10, 20)),
>   quarter = rep(1:20, 10),
> pd = runif(length(rep(1:20, 10 # conditional probs
> 
>sim_data_wide <- tidyr::spread(sim_data, quarter, pd)  
>colnames(sim_data_wide)[-1] <- paste0("P_",
>colnames(sim_data_wide)[-1])
>  
># Compute cumulative probs
>sim_data_wide <- sim_data_wide %>%
>  mutate(PC_1 = P_1,
> PC_2 = 1-(1-P_1)*(1-P_2),
> PC_3 = 1-(1-P_1)*(1-P_2)*(1-P_3),
> PC_4 = 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4),
>  PC_5 = 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5),
>  PC_6 = 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6),
>  PC_7 = 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6)*(1-P_7),
>PC_8 =
>1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6)*(1-P_7)*(1-P_8),
>PC_9 =
>1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6)*(1-P_7)*(1-P_8)*(1-P_9),
>PC_10 =
>1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6)*(1-P_7)*(1-P_8)*(1-P_9)*(1-P_10)
>)
>
>
>Thanks,
>Axel.
>   [[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.

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

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


Re: [R] Loop inside dplyr::mutate

2020-05-09 Thread Fox, John
Dear Axel,

Assuming that you're not wedded to using mutate():

> D1 <- 1 - as.matrix(sim_data_wide[, 2:11])
> D2 <- matrix(0, 10, 10)
> colnames(D2) <- paste0("PC_", 1:10)
> for (i in 1:10) D2[, i] <- 1 - apply(D1[, 1:i, drop=FALSE], 1, prod)
> all.equal(D2, as.matrix(sim_data_wide[, 22:31]))
[1] TRUE 

I hope this helps,
 John

> On May 9, 2020, at 7:45 PM, Axel Urbiz  wrote:
> 
> Hello, 
> 
> Is there a less verbose approach to obtaining the PC_i variables inside the 
> mutate?
> 
> library(tidyverse)
> sim_data <- data.frame(borrower_id = sort(rep(1:10, 20)),
>   quarter = rep(1:20, 10),
>   pd = runif(length(rep(1:20, 10 # conditional probs
> 
> sim_data_wide <- tidyr::spread(sim_data, quarter, pd)  
> colnames(sim_data_wide)[-1] <- paste0("P_", colnames(sim_data_wide)[-1])
> 
> # Compute cumulative probs
> sim_data_wide <- sim_data_wide %>%
>  mutate(PC_1 = P_1,
> PC_2 = 1-(1-P_1)*(1-P_2),
> PC_3 = 1-(1-P_1)*(1-P_2)*(1-P_3),
> PC_4 = 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4),
> PC_5 = 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5),
> PC_6 = 
> 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6),
> PC_7 = 
> 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6)*(1-P_7),
> PC_8 = 
> 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6)*(1-P_7)*(1-P_8),
> PC_9 = 
> 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6)*(1-P_7)*(1-P_8)*(1-P_9),
> PC_10 = 
> 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6)*(1-P_7)*(1-P_8)*(1-P_9)*(1-P_10)
>)
> 
> 
> Thanks,
> Axel.
>   [[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] Loop With Dates

2019-09-24 Thread Greg Snow
Just to add one more option (which is best probably depends on if all
the same dates are together in adjacent rows, if an earlier date can
come later in the data frame, and other things):

df$count <- cumsum(!duplicated(df$Date))

Skill a cumsum of logicals, just a different way of getting the logicals.

On Sun, Sep 22, 2019 at 5:20 AM Richard O'Keefe  wrote:
>
> Is this what you're after?
>
> > df <- data.frame(
> + Date = as.Date(c("2018-03-29", "2018-03-29", "2018-03-29",
> +  "2018-03-30", "2018-03-30", "2018- ..." ...
> [TRUNCATED]
>
> > df$count <- cumsum(c(TRUE, diff(df$Date) > 0))
> > df
> Date count
> 1 2018-03-29 1
> 2 2018-03-29 1
> 3 2018-03-29 1
> 4 2018-03-30 2
> 5 2018-03-30 2
> 6 2018-03-30 2
> 7 2018-03-31 3
> 8 2018-03-31 3
> 9 2018-03-31 3
>
> No extra libraries needed.  Whenever you want a vector that counts something,
> cumsum of a logical vector is a good thing to try.
>
> On Sat, 21 Sep 2019 at 05:47, Phillip Heinrich  wrote:
> >
> > With the data snippet below I’m trying to increment the “count” vector by 
> > one each time the date changes.
> >
> >  Date count
> > 1   2018-03-29 1
> > 2   2018-03-29 1
> > 3   2018-03-29 1
> > 81  2018-03-30 1
> > 82  2018-03-30 1
> > 83  2018-03-30 1
> > 165 2018-03-31 1
> > 166 2018-03-31 1
> > 167 2018-03-31 1
> >
> >
> > >
> >
> >
> >
> > I can get count to change when the date changes with the following code:
> >
> > test2 <- transform(test2,
> > +   count = ifelse(Date == lag(Date,1),count,count+1))
> > > test2
> >   Date count
> > 1   2018-03-29NA
> > 2   2018-03-29 1
> > 3   2018-03-29 1
> > 81  2018-03-30 2
> > 82  2018-03-30 1
> > 83  2018-03-30 1
> > 165 2018-03-31 2
> > 166 2018-03-31 1
> > 167 2018-03-31 1
> >
> >
> >
> >
> >
> >
> >
> > ...but I want all three March 30 rows to have a count of 2 and the March 31 
> > rows to be equal to 3.  Any suggestions?
> >
> > 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.
>
> __
> 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.



-- 
Gregory (Greg) L. Snow Ph.D.
538...@gmail.com

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


Re: [R] Loop With Dates

2019-09-22 Thread Richard O'Keefe
Is this what you're after?

> df <- data.frame(
+ Date = as.Date(c("2018-03-29", "2018-03-29", "2018-03-29",
+  "2018-03-30", "2018-03-30", "2018- ..." ...
[TRUNCATED]

> df$count <- cumsum(c(TRUE, diff(df$Date) > 0))
> df
Date count
1 2018-03-29 1
2 2018-03-29 1
3 2018-03-29 1
4 2018-03-30 2
5 2018-03-30 2
6 2018-03-30 2
7 2018-03-31 3
8 2018-03-31 3
9 2018-03-31 3

No extra libraries needed.  Whenever you want a vector that counts something,
cumsum of a logical vector is a good thing to try.

On Sat, 21 Sep 2019 at 05:47, Phillip Heinrich  wrote:
>
> With the data snippet below I’m trying to increment the “count” vector by one 
> each time the date changes.
>
>  Date count
> 1   2018-03-29 1
> 2   2018-03-29 1
> 3   2018-03-29 1
> 81  2018-03-30 1
> 82  2018-03-30 1
> 83  2018-03-30 1
> 165 2018-03-31 1
> 166 2018-03-31 1
> 167 2018-03-31 1
>
>
> >
>
>
>
> I can get count to change when the date changes with the following code:
>
> test2 <- transform(test2,
> +   count = ifelse(Date == lag(Date,1),count,count+1))
> > test2
>   Date count
> 1   2018-03-29NA
> 2   2018-03-29 1
> 3   2018-03-29 1
> 81  2018-03-30 2
> 82  2018-03-30 1
> 83  2018-03-30 1
> 165 2018-03-31 2
> 166 2018-03-31 1
> 167 2018-03-31 1
>
>
>
>
>
>
>
> ...but I want all three March 30 rows to have a count of 2 and the March 31 
> rows to be equal to 3.  Any suggestions?
>
> 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.

__
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] Loop With Dates

2019-09-21 Thread Jim Lemon
Hi Phillip,
While I really like Ana's solution, this might also help:

phdf<-read.table(text="Date count
  2018-03-29 1
  2018-03-29 1
  2018-03-29 1
  2018-03-30 1
  2018-03-30 1
  2018-03-30 1
  2018-03-31 1
  2018-03-31 1
  2018-03-31 1",
  header=TRUE,stringsAsFactors=FALSE)
phdf$Date<-as.Date(phdf$Date,"%Y-%m-%d")
incflag<-diff(phdf$Date)>0
phdf$count<-c(NA,cumsum(incflag)+1)

Jim

On Sat, Sep 21, 2019 at 3:47 AM Phillip Heinrich  wrote:
>
> With the data snippet below I’m trying to increment the “count” vector by one 
> each time the date changes.
>
>  Date count
> 1   2018-03-29 1
> 2   2018-03-29 1
> 3   2018-03-29 1
> 81  2018-03-30 1
> 82  2018-03-30 1
> 83  2018-03-30 1
> 165 2018-03-31 1
> 166 2018-03-31 1
> 167 2018-03-31 1
>
>
> >
>
>
>
> I can get count to change when the date changes with the following code:
>
> test2 <- transform(test2,
> +   count = ifelse(Date == lag(Date,1),count,count+1))
> > test2
>   Date count
> 1   2018-03-29NA
> 2   2018-03-29 1
> 3   2018-03-29 1
> 81  2018-03-30 2
> 82  2018-03-30 1
> 83  2018-03-30 1
> 165 2018-03-31 2
> 166 2018-03-31 1
> 167 2018-03-31 1
>
>
>
>
>
>
>
> ...but I want all three March 30 rows to have a count of 2 and the March 31 
> rows to be equal to 3.  Any suggestions?
>
> 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.

__
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] Loop With Dates

2019-09-20 Thread Rui Barradas

Hello,

Maybe I am not understanding but isn't this what you have asked in your 
previous question and my 2nd post (adapted) does?

If not, where does it fail?

Hope this helps,

Rui Barradas

Às 18:46 de 20/09/19, Phillip Heinrich escreveu:

With the data snippet below I’m trying to increment the “count” vector by one 
each time the date changes.

  Date count
1   2018-03-29 1
2   2018-03-29 1
3   2018-03-29 1
81  2018-03-30 1
82  2018-03-30 1
83  2018-03-30 1
165 2018-03-31 1
166 2018-03-31 1
167 2018-03-31 1
  
  
 >
  



I can get count to change when the date changes with the following code:

test2 <- transform(test2,
+   count = ifelse(Date == lag(Date,1),count,count+1))

test2

   Date count
1   2018-03-29NA
2   2018-03-29 1
3   2018-03-29 1
81  2018-03-30 2
82  2018-03-30 1
83  2018-03-30 1
165 2018-03-31 2
166 2018-03-31 1
167 2018-03-31 1
  
  


  



...but I want all three March 30 rows to have a count of 2 and the March 31 
rows to be equal to 3.  Any suggestions?

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.



__
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] Loop With Dates

2019-09-20 Thread Ana PGG
Hi Phillip,

This can be done in several ways as most things in programming. Here is one 
posible solution:

dates <- c("2018-03-29", "2018-03-29", "2018-03-29", 
   "2018-03-30", "2018-03-30", "2018-03-30", 
   "2018-03-31", "2018-03-31", "2018-03-31")
dates <- as.data.frame(as.Date(dates))
library(zoo)
dates <- zoo(dates)
colnames(dates) <- "dates"
dates$lag <- lag(dates, -1, na.pad = TRUE)
dates[1, 2] <- dates[1, 1]
dates$count <- cumsum(!(dates$dates == dates$lag)) + 1
dates$lag <- NULL

> dates
  dates.object count
1 2018-03-29   1
2 2018-03-29   1
3 2018-03-29   1
4 2018-03-30   2
5 2018-03-30   2
6 2018-03-30   2
7 2018-03-31   3
8 2018-03-31   3
9 2018-03-31   3


De: Phillip Heinrich
Enviado: viernes, 20 de septiembre de 2019 19:47
Para: r-help
Asunto: [R] Loop With Dates

With the data snippet below I’m trying to increment the “count” vector by one 
each time the date changes.  

 Date count
1   2018-03-29 1
2   2018-03-29 1
3   2018-03-29 1
81  2018-03-30 1
82  2018-03-30 1
83  2018-03-30 1
165 2018-03-31 1
166 2018-03-31 1
167 2018-03-31 1
 
 
>  
 


I can get count to change when the date changes with the following code:

test2 <- transform(test2,
+   count = ifelse(Date == lag(Date,1),count,count+1))
> test2
  Date count
1   2018-03-29NA
2   2018-03-29 1
3   2018-03-29 1
81  2018-03-30 2
82  2018-03-30 1
83  2018-03-30 1
165 2018-03-31 2
166 2018-03-31 1
167 2018-03-31 1
 
 
   

 


...but I want all three March 30 rows to have a count of 2 and the March 31 
rows to be equal to 3.  Any suggestions?

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.


[[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] Loop Repetition

2019-08-06 Thread Tolulope Adeagbo
Wow...Great one BOB...Gracias, Merci.

On Tue, 6 Aug 2019, 10:46 Bob O'Hara,  wrote:

> For a start, try this:
>
> for(i in 1:5) {
>   x <- runif(4,0,1)
> }
>
> Which will do what you want, but will over-write x each time (so isn't
> very good). Better (if you want to use the random numbers outside the
> loop) is this:
>
> x <- matrix(NA, nrow=5, ncol=4)
> for(i in 1:5) {
>   x[i,] <- runif(4,0,1)
> }
>
> But better than this is not to use a loop at all, but to use R's
> vectorisation:
>
> x <- matrix(runif(20,0,1), nrow=5, ncol=4)
>
> i.e. generate a vector of random numbers (the runif()), and then put
> that into a matrix (the matrix()).
>
> Oh, and you could also do this:
> replicate(5, runif(4, 0,1))
> which is slightly odd here, but if you want to use the random numbers
> to do something, you can do all of it in a function, e.g.
>
> CalcMean <- function(n=4) {
>   x <- runif(n, 0, 1)
>   mean(x)
> }
> replicate(5, CalcMean(n=4))
>
> Using a function makes code writing a lot easier, as you can write and
> debug the function on its own, and then use replicate() to run the
> loop (there are also functions like vapply() and apply() if you need
> to pass different arguments into the function for different
> iterations).
>
> Bob
>
>
>
>
> On Tue, 6 Aug 2019 at 11:28, Tolulope Adeagbo 
> wrote:
> >
> > Thanks guys, I've tried all you're suggesting,  both for (x in 1:5) and
> > break, but I cant seem to ascertain when the loop has generated a vector
> of
> > 4 random numbers  5 times.
> >
> > On Tue, 6 Aug 2019, 10:09 Jim Lemon,  wrote:
> >
> > > Hi Tolulope,
> > > The "in" operator steps through each element of the vector on the
> > > right. You only have one element. Therefore you probably want:
> > >
> > > for(x in 1:5)
> > > ...
> > >
> > > Jim
> > >
> > > Jim
> > >
> > > On Tue, Aug 6, 2019 at 6:54 PM Tolulope Adeagbo
> > >  wrote:
> > > >
> > > > Hey guys,
> > > >
> > > > I'm trying to write a loop that will repeat an action for a
> stipulated
> > > > number of times. I have written some code but i think i'm missing
> > > something.
> > > >
> > > > for (x in 5) {
> > > >
> > > >   repeat{
> > > >
> > > > x = runif(1:4, min = 0, max = 1)
> > > >
> > > >
> > > > print(x)
> > > >
> > > >
> > > > if (x== var_1[5]){
> > > >
> > > >   print("done")
> > > > }
> > > >
> > > > print(x)}
> > > > }
> > > >
> > > > The goal is to generate the random number 5 times.
> > > >
> > > > Please help
> > > >
> > > > [[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.
>
>
>
> --
> Bob O'Hara
> Institutt for matematiske fag
> NTNU
> 7491 Trondheim
> Norway
>
> Mobile: +47 915 54 416
> Journal of Negative Results - EEB: www.jnr-eeb.org
>

[[alternative HTML version deleted]]

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


Re: [R] Loop Repetition

2019-08-06 Thread Bob O'Hara
For a start, try this:

for(i in 1:5) {
  x <- runif(4,0,1)
}

Which will do what you want, but will over-write x each time (so isn't
very good). Better (if you want to use the random numbers outside the
loop) is this:

x <- matrix(NA, nrow=5, ncol=4)
for(i in 1:5) {
  x[i,] <- runif(4,0,1)
}

But better than this is not to use a loop at all, but to use R's vectorisation:

x <- matrix(runif(20,0,1), nrow=5, ncol=4)

i.e. generate a vector of random numbers (the runif()), and then put
that into a matrix (the matrix()).

Oh, and you could also do this:
replicate(5, runif(4, 0,1))
which is slightly odd here, but if you want to use the random numbers
to do something, you can do all of it in a function, e.g.

CalcMean <- function(n=4) {
  x <- runif(n, 0, 1)
  mean(x)
}
replicate(5, CalcMean(n=4))

Using a function makes code writing a lot easier, as you can write and
debug the function on its own, and then use replicate() to run the
loop (there are also functions like vapply() and apply() if you need
to pass different arguments into the function for different
iterations).

Bob




On Tue, 6 Aug 2019 at 11:28, Tolulope Adeagbo  wrote:
>
> Thanks guys, I've tried all you're suggesting,  both for (x in 1:5) and
> break, but I cant seem to ascertain when the loop has generated a vector of
> 4 random numbers  5 times.
>
> On Tue, 6 Aug 2019, 10:09 Jim Lemon,  wrote:
>
> > Hi Tolulope,
> > The "in" operator steps through each element of the vector on the
> > right. You only have one element. Therefore you probably want:
> >
> > for(x in 1:5)
> > ...
> >
> > Jim
> >
> > Jim
> >
> > On Tue, Aug 6, 2019 at 6:54 PM Tolulope Adeagbo
> >  wrote:
> > >
> > > Hey guys,
> > >
> > > I'm trying to write a loop that will repeat an action for a stipulated
> > > number of times. I have written some code but i think i'm missing
> > something.
> > >
> > > for (x in 5) {
> > >
> > >   repeat{
> > >
> > > x = runif(1:4, min = 0, max = 1)
> > >
> > >
> > > print(x)
> > >
> > >
> > > if (x== var_1[5]){
> > >
> > >   print("done")
> > > }
> > >
> > > print(x)}
> > > }
> > >
> > > The goal is to generate the random number 5 times.
> > >
> > > Please help
> > >
> > > [[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.



-- 
Bob O'Hara
Institutt for matematiske fag
NTNU
7491 Trondheim
Norway

Mobile: +47 915 54 416
Journal of Negative Results - EEB: www.jnr-eeb.org

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


Re: [R] Loop Repetition

2019-08-06 Thread Tolulope Adeagbo
Thanks guys, I've tried all you're suggesting,  both for (x in 1:5) and
break, but I cant seem to ascertain when the loop has generated a vector of
4 random numbers  5 times.

On Tue, 6 Aug 2019, 10:09 Jim Lemon,  wrote:

> Hi Tolulope,
> The "in" operator steps through each element of the vector on the
> right. You only have one element. Therefore you probably want:
>
> for(x in 1:5)
> ...
>
> Jim
>
> Jim
>
> On Tue, Aug 6, 2019 at 6:54 PM Tolulope Adeagbo
>  wrote:
> >
> > Hey guys,
> >
> > I'm trying to write a loop that will repeat an action for a stipulated
> > number of times. I have written some code but i think i'm missing
> something.
> >
> > for (x in 5) {
> >
> >   repeat{
> >
> > x = runif(1:4, min = 0, max = 1)
> >
> >
> > print(x)
> >
> >
> > if (x== var_1[5]){
> >
> >   print("done")
> > }
> >
> > print(x)}
> > }
> >
> > The goal is to generate the random number 5 times.
> >
> > Please help
> >
> > [[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] Loop Repetition

2019-08-06 Thread Jim Lemon
Hi Tolulope,
The "in" operator steps through each element of the vector on the
right. You only have one element. Therefore you probably want:

for(x in 1:5)
...

Jim

Jim

On Tue, Aug 6, 2019 at 6:54 PM Tolulope Adeagbo
 wrote:
>
> Hey guys,
>
> I'm trying to write a loop that will repeat an action for a stipulated
> number of times. I have written some code but i think i'm missing something.
>
> for (x in 5) {
>
>   repeat{
>
> x = runif(1:4, min = 0, max = 1)
>
>
> print(x)
>
>
> if (x== var_1[5]){
>
>   print("done")
> }
>
> print(x)}
> }
>
> The goal is to generate the random number 5 times.
>
> Please help
>
> [[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] Loop Repetition

2019-08-06 Thread Bob O'Hara
Is there anything wrong with just doing this?

x <- runif(5, min = 0, max = 1)

Also note that you use x to be at last 2 things: in

for (x in 5) {

you set it to 5, and then in the loop you

x = runif(1:4, min = 0, max = 1)

you make it a vector of length 4.

You also fail to use break to stop the repeat (something I never knew
existed in R!).

Bob

On Tue, 6 Aug 2019 at 10:54, Tolulope Adeagbo  wrote:
>
> Hey guys,
>
> I'm trying to write a loop that will repeat an action for a stipulated
> number of times. I have written some code but i think i'm missing something.
>
> for (x in 5) {
>
>   repeat{
>
> x = runif(1:4, min = 0, max = 1)
>
>
> print(x)
>
>
> if (x== var_1[5]){
>
>   print("done")
> }
>
> print(x)}
> }
>
> The goal is to generate the random number 5 times.
>
> Please help
>
> [[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.



-- 
Bob O'Hara
Institutt for matematiske fag
NTNU
7491 Trondheim
Norway

Mobile: +47 915 54 416
Journal of Negative Results - EEB: www.jnr-eeb.org

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


Re: [R] loop through columns in a data frame

2019-03-26 Thread Yuan, Keming (CDC/DDNID/NCIPC/DVP) via R-help
Thank you so much, Jim. That’s exactly what I need. Sorry for not providing the 
data frame. But you created the correct data structure. Thanks again!

From: jim holtman 
Sent: Monday, March 25, 2019 2:07 PM
To: Yuan, Keming (CDC/DDNID/NCIPC/DVP) 
Cc: R-help@r-project.org
Subject: Re: [R] loop through columns in a data frame

R Notebook

You forgot to provide what your test data looks like. For example, are all the 
columns a single letter followed by “_" as the name, or are there longer names? 
Are there always matched pairs (‘le’ and ‘me’) or can singles occur?
Hide

library(tidyverse)

# create some data

test <- tibble(a_le = sample(3, 10, TRUE),

   a_me = sample(3, 10, TRUE),

   b_le = sample(3, 10, TRUE),

   b_me = sample(3, 10, TRUE),

   long_le = sample(3, 10, TRUE),

   long_me = sample(3, 10, TRUE),

   short_le = sample(3, 10, TRUE)

)

So get the names of the columns that contain ‘le’ or ‘me’ and group them 
together for processing
Hide

col_names <- grep("_(le|me)$", names(test), value = TRUE)

group <- tibble(id = str_remove(col_names, "_.*"), col = col_names)

result <- group %>%

  group_by(id) %>%

  do(tibble(x = rowSums(test[, .$col] == 1)))

# add new columns back

for (i in split(result, result$id)){

  test[, paste0(i$id[1], "_new")] <- as.integer(i$x > 0)

}

test


a_le


a_me


b_le


b_me


long_le


long_me


short_le


a_new


b_new


long_new


3

1

2

3

1

2

2

1

0

1

2

3

3

2

1

1

1

0

0

1

3

2

3

2

1

3

3

0

0

1

2

3

1

3

3

1

2

0

1

1

1

1

2

1

1

2

2

1

1

1

3

3

3

1

1

1

1

0

1

1

1

2

1

2

2

2

2

1

1

0

1

3

2

3

1

1

3

1

0

1

3

1

1

1

3

3

2

1

1

0

1

1

1

2

3

3

3

1

1

0

1-10 of 10 rows | 1-10 of 11 columns

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 Mon, Mar 25, 2019 at 10:08 AM Yuan, Keming (CDC/DDNID/NCIPC/DVP) via R-help 
mailto:r-help@r-project.org>> wrote:
Hi All,

I have a data frame with variable names like A_le, A_me, B_le, B_me, C_le, 
C_me
if A_le=1 or A_me=1 then  I need to create a new column A_new=1. Same operation 
to create columns B_new, C_new...
Does anyone know how to use loop (or other methods) to create new columns? In 
SAS, I can use array to get it done. But I don't know how to do it in R.

Thanks,

Keming Yuan
CDC


[[alternative HTML version deleted]]

__
R-help@r-project.org<mailto: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] loop through columns in a data frame

2019-03-25 Thread Bert Gunter
"Does anyone know how to use loop (or other methods) to create new columns?
In SAS, I can use array to get it done. But I don't know how to do it in R."

Yup. Practically all users of R know how, as this is entirely elementary.
You will too if you make the effort to go through a basic R tutorial, of
which there are many on the web (and one shipped with R).

Cheers,
Bert

On Mon, Mar 25, 2019 at 10:08 AM Yuan, Keming (CDC/DDNID/NCIPC/DVP) via
R-help  wrote:

> Hi All,
>
> I have a data frame with variable names like A_le, A_me, B_le, B_me, C_le,
> C_me
> if A_le=1 or A_me=1 then  I need to create a new column A_new=1. Same
> operation to create columns B_new, C_new...
> Does anyone know how to use loop (or other methods) to create new columns?
> In SAS, I can use array to get it done. But I don't know how to do it in R.
>
> Thanks,
>
> Keming Yuan
> CDC
>
>
> [[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] loop through columns in a data frame

2019-03-25 Thread jim holtman
R Notebook

You forgot to provide what your test data looks like. For example, are all
the columns a single letter followed by “_" as the name, or are there
longer names? Are there always matched pairs (‘le’ and ‘me’) or can singles
occur?
Hide

library(tidyverse)# create some data
test <- tibble(a_le = sample(3, 10, TRUE),
   a_me = sample(3, 10, TRUE),
   b_le = sample(3, 10, TRUE),
   b_me = sample(3, 10, TRUE),
   long_le = sample(3, 10, TRUE),
   long_me = sample(3, 10, TRUE),
   short_le = sample(3, 10, TRUE)
)

So get the names of the columns that contain ‘le’ or ‘me’ and group them
together for processing
Hide

col_names <- grep("_(le|me)$", names(test), value = TRUE)
group <- tibble(id = str_remove(col_names, "_.*"), col = col_names)
result <- group %>%
  group_by(id) %>%
  do(tibble(x = rowSums(test[, .$col] == 1)))# add new columns backfor
(i in split(result, result$id)){
  test[, paste0(i$id[1], "_new")] <- as.integer(i$x > 0)
}
test

a_le

a_me

b_le

b_me

long_le

long_me

short_le

a_new

b_new

long_new

3 1 2 3 1 2 2 1 0 1
2 3 3 2 1 1 1 0 0 1
3 2 3 2 1 3 3 0 0 1
2 3 1 3 3 1 2 0 1 1
1 1 2 1 1 2 2 1 1 1
3 3 3 1 1 1 1 0 1 1
1 2 1 2 2 2 2 1 1 0
1 3 2 3 1 1 3 1 0 1
3 1 1 1 3 3 2 1 1 0
1 1 1 2 3 3 3 1 1 0
1-10 of 10 rows | 1-10 of 11 columns

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 Mon, Mar 25, 2019 at 10:08 AM Yuan, Keming (CDC/DDNID/NCIPC/DVP) via
R-help  wrote:

> Hi All,
>
> I have a data frame with variable names like A_le, A_me, B_le, B_me, C_le,
> C_me
> if A_le=1 or A_me=1 then  I need to create a new column A_new=1. Same
> operation to create columns B_new, C_new...
> Does anyone know how to use loop (or other methods) to create new columns?
> In SAS, I can use array to get it done. But I don't know how to do it in R.
>
> Thanks,
>
> Keming Yuan
> CDC
>
>
> [[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] loop for comparing two or more groups using bootstrapping

2018-09-11 Thread Marna Wagley
Thank you Jim, it worked. I am very grateful for your help.
Thanks
KG

On Tue, Sep 11, 2018 at 3:51 PM Jim Lemon  wrote:

> Hi Kristi,
> My fault, I only worked out how to assign the values to the names and pick
> out the columns of daT for the calculations. I think this does what you
> want, but I can't guarantee the result.
>
> daT<-structure(list(year1=c(0.417,0.538,0.69,0.688,0.688,0.606,
> 0.667,0.7,0.545,0.462,0.711,0.642,0.744,0.604,0.612,
> 0.667,0.533,0.556,0.444,0.526,0.323,0.308,0.195,0.333,
> 0.323,0.256,0.345,0.205,0.286,0.706,0.7,0.6,0.571,0.364,
> 0.429,0.326,0.571,0.424,0.341,0.387,0.341,0.324,0.696,
> 0.696,0.583,0.556,0.645,0.435,0.471,0.556),year2=c(0.385,
> 0.552,0.645,0.516,0.629,0.595,0.72,0.638,0.557,0.588,
> 0.63,0.744,0.773,0.571,0.723,0.769,0.667,0.667,0.526,
> 0.476,0.294,0.323,0.222,0.556,0.263,0.37,0.357,0.25,
> 0.323,0.778,0.667,0.636,0.583,0.432,0.412,0.333,0.571,
> 0.39,0.4,0.452,0.326,0.471,0.7,0.75,0.615,0.462,0.556,
> 0.4,0.696,0.465),year3=c(0.435,0.759,0.759,0.759,0.714,
> 0.593,0.651,0.683,0.513,0.643,0.652,0.757,0.791,0.649,
> 0.78,0.5,0.5,0.5,0.533,0.429,0.333,0.286,0.231,0.533,
> 0.303,0.417,0.333,0.333,0.357,0.909,1,0.952,0.8,0.556,
> 0.529,0.562,0.762,0.513,0.733,0.611,0.733,0.647,0.909,
> 0.857,0.8,0.556,0.588,0.562,0.857,0.513),year4=c(0.333,
> 0.533,0.6,0.483,0.743,0.5,0.691,0.619,0.583,0.385,0.653,
> 0.762,0.844,0.64,0.667,0.571,0.571,0.615,0.421,0.5,0.205,
> 0.308,0.25,0.6,0.242,0.308,0.276,0.235,0.211,0.9,0.632,
> 0.72,0.727,0.356,0.5,0.368,0.5,0.41,0.562,0.514,0.4,
> 0.409,0.632,0.72,0.727,0.4,0.5,0.421,0.5,0.462)),.Names=c("year1",
> "year2","year3","year4"),row.names=c(NA,-50L),class="data.frame")
> colname.mat<-combn(paste0("year",1:4),2)
> samplenames<-apply(colname.mat,2,paste,collapse="")
> k<-1
> meandiff<-function(x) return(mean(x[[1]])-mean(x[[2]]))
> for(column in 1:ncol(colname.mat)) {
>  assign(samplenames[column],
>   replicate(k,data.frame(sample(daT[,colname.mat[1,column]],3,TRUE),
>sample(daT[,colname.mat[2,column]],3,TRUE
>  meandiffs<-unlist(apply(get(samplenames[column]),2,meandiff))
>  cat(samplenames[column],"\n")
>  cat("mean diff =",mean(meandiffs),"95% CI =",
>   quantile(meandiffs,c(0.025,0.975)),"\n")
>  png(paste0(samplenames[column],".png")
>  hist(meandiffs)
>  dev.off()
> }
>
> You should get a printout of the means and CIs and  bunch of PNG files with
> the histograms.
>
> Jim
>
>
> On Tue, Sep 11, 2018 at 11:55 PM Kristi Glover 
> wrote:
>
> > Dear Jim,
> >
> > Thank you very much for the code. I run it but it gave me row names
> > like "year224", "year142".
> >
> > are these the difference between columns? If we want to get bootstrapping
> > means of difference between years (year2-year1; year3-year1), its CI and
> > exact p value, how can we get it?
> >
> > thanks
> >
> > KG
> >
> > 
> >
> > head(daT)
> >
> > colname.mat<-combn(paste0("year",1:4),2)
> >
> > samplenames<-apply(colname.mat,2,paste,collapse="")
> >
> > k<-10
> >
> > for(column in 1:ncol(colname.mat)) {
> >
> >  assign(samplenames[column],replicate(k,sample(unlist(daT[,colname.mat[,
> > column]]),3,TRUE)))
> >
> > }
> >
> > > get(samplenames[1])
> >  [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
> > year224 0.556 0.667 0.571 0.526 0.629 0.696 0.323 0.526 0.256 0.667
> > year142 0.324 0.324 0.706 0.638 0.600 0.294 0.612 0.688 0.432 0.387
> > year237 0.571 0.696 0.629 0.471 0.462 0.471 0.452 0.595 0.333 0.435
> >
> >
> >
> >
> > --
> > *From:* Jim Lemon 
> > *Sent:* September 11, 2018 1:44 AM
> > *To:* Kristi Glover
> > *Cc:* r-help mailing list
> > *Subject:* Re: [R] loop for comparing two or more groups using
> > bootstrapping
> >
> > Hi Kristy,
> > Try this:
> >
> > colname.mat<-combn(paste0("year",1:4),2)
> > samplenames<-apply(colname.mat,2,paste,collapse="")
> > k<-1
> > for(column in 1:ncol(colname.mat)) {
> >
> >
> assign(samplenames[column],replicate(k,sample(unlist(daT[,colname.mat[,column]]),3,TRUE)))
> > }
> >
> > Then use get(samplenames[1]) and so on to access the values.
> >
> > Jim
> > On Tue, Sep 11, 2018 at 4:52 PM Kristi Glover  >
> > wrote:
> > >
> > > Hi R users,
> > >
> > > I was tryi

Re: [R] loop for comparing two or more groups using bootstrapping

2018-09-11 Thread Jim Lemon
Hi Kristi,
My fault, I only worked out how to assign the values to the names and pick
out the columns of daT for the calculations. I think this does what you
want, but I can't guarantee the result.

daT<-structure(list(year1=c(0.417,0.538,0.69,0.688,0.688,0.606,
0.667,0.7,0.545,0.462,0.711,0.642,0.744,0.604,0.612,
0.667,0.533,0.556,0.444,0.526,0.323,0.308,0.195,0.333,
0.323,0.256,0.345,0.205,0.286,0.706,0.7,0.6,0.571,0.364,
0.429,0.326,0.571,0.424,0.341,0.387,0.341,0.324,0.696,
0.696,0.583,0.556,0.645,0.435,0.471,0.556),year2=c(0.385,
0.552,0.645,0.516,0.629,0.595,0.72,0.638,0.557,0.588,
0.63,0.744,0.773,0.571,0.723,0.769,0.667,0.667,0.526,
0.476,0.294,0.323,0.222,0.556,0.263,0.37,0.357,0.25,
0.323,0.778,0.667,0.636,0.583,0.432,0.412,0.333,0.571,
0.39,0.4,0.452,0.326,0.471,0.7,0.75,0.615,0.462,0.556,
0.4,0.696,0.465),year3=c(0.435,0.759,0.759,0.759,0.714,
0.593,0.651,0.683,0.513,0.643,0.652,0.757,0.791,0.649,
0.78,0.5,0.5,0.5,0.533,0.429,0.333,0.286,0.231,0.533,
0.303,0.417,0.333,0.333,0.357,0.909,1,0.952,0.8,0.556,
0.529,0.562,0.762,0.513,0.733,0.611,0.733,0.647,0.909,
0.857,0.8,0.556,0.588,0.562,0.857,0.513),year4=c(0.333,
0.533,0.6,0.483,0.743,0.5,0.691,0.619,0.583,0.385,0.653,
0.762,0.844,0.64,0.667,0.571,0.571,0.615,0.421,0.5,0.205,
0.308,0.25,0.6,0.242,0.308,0.276,0.235,0.211,0.9,0.632,
0.72,0.727,0.356,0.5,0.368,0.5,0.41,0.562,0.514,0.4,
0.409,0.632,0.72,0.727,0.4,0.5,0.421,0.5,0.462)),.Names=c("year1",
"year2","year3","year4"),row.names=c(NA,-50L),class="data.frame")
colname.mat<-combn(paste0("year",1:4),2)
samplenames<-apply(colname.mat,2,paste,collapse="")
k<-1
meandiff<-function(x) return(mean(x[[1]])-mean(x[[2]]))
for(column in 1:ncol(colname.mat)) {
 assign(samplenames[column],
  replicate(k,data.frame(sample(daT[,colname.mat[1,column]],3,TRUE),
   sample(daT[,colname.mat[2,column]],3,TRUE
 meandiffs<-unlist(apply(get(samplenames[column]),2,meandiff))
 cat(samplenames[column],"\n")
 cat("mean diff =",mean(meandiffs),"95% CI =",
  quantile(meandiffs,c(0.025,0.975)),"\n")
 png(paste0(samplenames[column],".png")
 hist(meandiffs)
 dev.off()
}

You should get a printout of the means and CIs and  bunch of PNG files with
the histograms.

Jim


On Tue, Sep 11, 2018 at 11:55 PM Kristi Glover 
wrote:

> Dear Jim,
>
> Thank you very much for the code. I run it but it gave me row names
> like "year224", "year142".
>
> are these the difference between columns? If we want to get bootstrapping
> means of difference between years (year2-year1; year3-year1), its CI and
> exact p value, how can we get it?
>
> thanks
>
> KG
>
> 
>
> head(daT)
>
> colname.mat<-combn(paste0("year",1:4),2)
>
> samplenames<-apply(colname.mat,2,paste,collapse="")
>
> k<-10
>
> for(column in 1:ncol(colname.mat)) {
>
>  assign(samplenames[column],replicate(k,sample(unlist(daT[,colname.mat[,
> column]]),3,TRUE)))
>
> }
>
> > get(samplenames[1])
>  [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
> year224 0.556 0.667 0.571 0.526 0.629 0.696 0.323 0.526 0.256 0.667
> year142 0.324 0.324 0.706 0.638 0.600 0.294 0.612 0.688 0.432 0.387
> year237 0.571 0.696 0.629 0.471 0.462 0.471 0.452 0.595 0.333 0.435
>
>
>
>
> --
> *From:* Jim Lemon 
> *Sent:* September 11, 2018 1:44 AM
> *To:* Kristi Glover
> *Cc:* r-help mailing list
> *Subject:* Re: [R] loop for comparing two or more groups using
> bootstrapping
>
> Hi Kristy,
> Try this:
>
> colname.mat<-combn(paste0("year",1:4),2)
> samplenames<-apply(colname.mat,2,paste,collapse="")
> k<-1
> for(column in 1:ncol(colname.mat)) {
>
>  
> assign(samplenames[column],replicate(k,sample(unlist(daT[,colname.mat[,column]]),3,TRUE)))
> }
>
> Then use get(samplenames[1]) and so on to access the values.
>
> Jim
> On Tue, Sep 11, 2018 at 4:52 PM Kristi Glover 
> wrote:
> >
> > Hi R users,
> >
> > I was trying to test a null hypothesis of difference between two groups
> was 0. I have many years data, such as year1, year2,, year3, year4 and I
> was trying to compare between year1 and year2, year1 and year3, year2 and
> year3 and so on and have used following code with an example data.
> >
> >
> > I tried to make a loop but did not work to compare between many years,
> and also want to obtain the exact p value. Would you mind to help me to
> make a loop?
> >
> > Thanks for your help.
> >
> >
> > KG
> >
> >
> > daT<-structure(list(year1 = c(0.417, 0.538, 0.69, 0.688, 0.688, 0.606,
> > 0.667, 0.7, 0.545, 0.462, 0.711, 0.642, 0.744

Re: [R] loop for comparing two or more groups using bootstrapping

2018-09-11 Thread Kristi Glover
Dear Jim,

Thank you very much for the code. I run it but it gave me row names like 
"year224", "year142".

are these the difference between columns? If we want to get bootstrapping means 
of difference between years (year2-year1; year3-year1), its CI and exact p 
value, how can we get it?

thanks

KG



head(daT)

colname.mat<-combn(paste0("year",1:4),2)

samplenames<-apply(colname.mat,2,paste,collapse="")

k<-10

for(column in 1:ncol(colname.mat)) {

 
assign(samplenames[column],replicate(k,sample(unlist(daT[,colname.mat[,column]]),3,TRUE)))

}


> get(samplenames[1])
 [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
year224 0.556 0.667 0.571 0.526 0.629 0.696 0.323 0.526 0.256 0.667
year142 0.324 0.324 0.706 0.638 0.600 0.294 0.612 0.688 0.432 0.387
year237 0.571 0.696 0.629 0.471 0.462 0.471 0.452 0.595 0.333 0.435





From: Jim Lemon 
Sent: September 11, 2018 1:44 AM
To: Kristi Glover
Cc: r-help mailing list
Subject: Re: [R] loop for comparing two or more groups using bootstrapping

Hi Kristy,
Try this:

colname.mat<-combn(paste0("year",1:4),2)
samplenames<-apply(colname.mat,2,paste,collapse="")
k<-1
for(column in 1:ncol(colname.mat)) {
 
assign(samplenames[column],replicate(k,sample(unlist(daT[,colname.mat[,column]]),3,TRUE)))
}

Then use get(samplenames[1]) and so on to access the values.

Jim
On Tue, Sep 11, 2018 at 4:52 PM Kristi Glover  wrote:
>
> Hi R users,
>
> I was trying to test a null hypothesis of difference between two groups was 
> 0. I have many years data, such as year1, year2,, year3, year4 and I was 
> trying to compare between year1 and year2, year1 and year3, year2 and year3 
> and so on and have used following code with an example data.
>
>
> I tried to make a loop but did not work to compare between many years, and 
> also want to obtain the exact p value. Would you mind to help me to make a 
> loop?
>
> Thanks for your help.
>
>
> KG
>
>
> daT<-structure(list(year1 = c(0.417, 0.538, 0.69, 0.688, 0.688, 0.606,
> 0.667, 0.7, 0.545, 0.462, 0.711, 0.642, 0.744, 0.604, 0.612,
> 0.667, 0.533, 0.556, 0.444, 0.526, 0.323, 0.308, 0.195, 0.333,
> 0.323, 0.256, 0.345, 0.205, 0.286, 0.706, 0.7, 0.6, 0.571, 0.364,
> 0.429, 0.326, 0.571, 0.424, 0.341, 0.387, 0.341, 0.324, 0.696,
> 0.696, 0.583, 0.556, 0.645, 0.435, 0.471, 0.556), year2 = c(0.385,
> 0.552, 0.645, 0.516, 0.629, 0.595, 0.72, 0.638, 0.557, 0.588,
> 0.63, 0.744, 0.773, 0.571, 0.723, 0.769, 0.667, 0.667, 0.526,
> 0.476, 0.294, 0.323, 0.222, 0.556, 0.263, 0.37, 0.357, 0.25,
> 0.323, 0.778, 0.667, 0.636, 0.583, 0.432, 0.412, 0.333, 0.571,
> 0.39, 0.4, 0.452, 0.326, 0.471, 0.7, 0.75, 0.615, 0.462, 0.556,
> 0.4, 0.696, 0.465), year3 = c(0.435, 0.759, 0.759, 0.759, 0.714,
> 0.593, 0.651, 0.683, 0.513, 0.643, 0.652, 0.757, 0.791, 0.649,
> 0.78, 0.5, 0.5, 0.5, 0.533, 0.429, 0.333, 0.286, 0.231, 0.533,
> 0.303, 0.417, 0.333, 0.333, 0.357, 0.909, 1, 0.952, 0.8, 0.556,
> 0.529, 0.562, 0.762, 0.513, 0.733, 0.611, 0.733, 0.647, 0.909,
> 0.857, 0.8, 0.556, 0.588, 0.562, 0.857, 0.513), year4 = c(0.333,
> 0.533, 0.6, 0.483, 0.743, 0.5, 0.691, 0.619, 0.583, 0.385, 0.653,
> 0.762, 0.844, 0.64, 0.667, 0.571, 0.571, 0.615, 0.421, 0.5, 0.205,
> 0.308, 0.25, 0.6, 0.242, 0.308, 0.276, 0.235, 0.211, 0.9, 0.632,
> 0.72, 0.727, 0.356, 0.5, 0.368, 0.5, 0.41, 0.562, 0.514, 0.4,
> 0.409, 0.632, 0.72, 0.727, 0.4, 0.5, 0.421, 0.5, 0.462)), .Names = c("year1",
> "year2", "year3", "year4"), row.names = c(NA, -50L), class = "data.frame")
>
> head(daT)
>
> # null hypothesis; difference is equal to zero
>
> dif1.2<-daT$year2-daT$year1
>
> k=1
>
> mysamples1.2=replicate(k, sample(dif1.2, replace=T))
>
> mymeans1.2=apply(mysamples1.2, 2, mean)
>
> quantile(mymeans1.2, c(0.025, 0.975))
>
> hist(mysamples1.2)
>
> mean(mymeans1.2)
>
> #what is p value?
>
>
> #similarly Now I want to compare between year 1 and year3,
>
> dif1.3<-daT$year3-daT$year1
>
> mysamples1.3=replicate(k, sample(dif1.3, replace=T))
>
> mymeans1.3=apply(mysamples1.3, 2, mean)
>
> quantile(mymeans1.3, c(0.025, 0.975))
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help

thz.ch/mailman/listinfo/r-help>
stat.ethz.ch
The main R mailing list, for announcements about the development of R and the 
availability of new code, questions and answers about problems and solutions 
using R, enhancements and patches to the source code and documentation of R, 
comparison and compatibility with S and S-plus, an

Re: [R] loop for comparing two or more groups using bootstrapping

2018-09-11 Thread Jim Lemon
Hi Kristy,
Try this:

colname.mat<-combn(paste0("year",1:4),2)
samplenames<-apply(colname.mat,2,paste,collapse="")
k<-1
for(column in 1:ncol(colname.mat)) {
 
assign(samplenames[column],replicate(k,sample(unlist(daT[,colname.mat[,column]]),3,TRUE)))
}

Then use get(samplenames[1]) and so on to access the values.

Jim
On Tue, Sep 11, 2018 at 4:52 PM Kristi Glover  wrote:
>
> Hi R users,
>
> I was trying to test a null hypothesis of difference between two groups was 
> 0. I have many years data, such as year1, year2,, year3, year4 and I was 
> trying to compare between year1 and year2, year1 and year3, year2 and year3 
> and so on and have used following code with an example data.
>
>
> I tried to make a loop but did not work to compare between many years, and 
> also want to obtain the exact p value. Would you mind to help me to make a 
> loop?
>
> Thanks for your help.
>
>
> KG
>
>
> daT<-structure(list(year1 = c(0.417, 0.538, 0.69, 0.688, 0.688, 0.606,
>
> 0.667, 0.7, 0.545, 0.462, 0.711, 0.642, 0.744, 0.604, 0.612,
>
> 0.667, 0.533, 0.556, 0.444, 0.526, 0.323, 0.308, 0.195, 0.333,
>
> 0.323, 0.256, 0.345, 0.205, 0.286, 0.706, 0.7, 0.6, 0.571, 0.364,
>
> 0.429, 0.326, 0.571, 0.424, 0.341, 0.387, 0.341, 0.324, 0.696,
>
> 0.696, 0.583, 0.556, 0.645, 0.435, 0.471, 0.556), year2 = c(0.385,
>
> 0.552, 0.645, 0.516, 0.629, 0.595, 0.72, 0.638, 0.557, 0.588,
>
> 0.63, 0.744, 0.773, 0.571, 0.723, 0.769, 0.667, 0.667, 0.526,
>
> 0.476, 0.294, 0.323, 0.222, 0.556, 0.263, 0.37, 0.357, 0.25,
>
> 0.323, 0.778, 0.667, 0.636, 0.583, 0.432, 0.412, 0.333, 0.571,
>
> 0.39, 0.4, 0.452, 0.326, 0.471, 0.7, 0.75, 0.615, 0.462, 0.556,
>
> 0.4, 0.696, 0.465), year3 = c(0.435, 0.759, 0.759, 0.759, 0.714,
>
> 0.593, 0.651, 0.683, 0.513, 0.643, 0.652, 0.757, 0.791, 0.649,
>
> 0.78, 0.5, 0.5, 0.5, 0.533, 0.429, 0.333, 0.286, 0.231, 0.533,
>
> 0.303, 0.417, 0.333, 0.333, 0.357, 0.909, 1, 0.952, 0.8, 0.556,
>
> 0.529, 0.562, 0.762, 0.513, 0.733, 0.611, 0.733, 0.647, 0.909,
>
> 0.857, 0.8, 0.556, 0.588, 0.562, 0.857, 0.513), year4 = c(0.333,
>
> 0.533, 0.6, 0.483, 0.743, 0.5, 0.691, 0.619, 0.583, 0.385, 0.653,
>
> 0.762, 0.844, 0.64, 0.667, 0.571, 0.571, 0.615, 0.421, 0.5, 0.205,
>
> 0.308, 0.25, 0.6, 0.242, 0.308, 0.276, 0.235, 0.211, 0.9, 0.632,
>
> 0.72, 0.727, 0.356, 0.5, 0.368, 0.5, 0.41, 0.562, 0.514, 0.4,
>
> 0.409, 0.632, 0.72, 0.727, 0.4, 0.5, 0.421, 0.5, 0.462)), .Names = c("year1",
>
> "year2", "year3", "year4"), row.names = c(NA, -50L), class = "data.frame")
>
> head(daT)
>
> # null hypothesis; difference is equal to zero
>
> dif1.2<-daT$year2-daT$year1
>
> k=1
>
> mysamples1.2=replicate(k, sample(dif1.2, replace=T))
>
> mymeans1.2=apply(mysamples1.2, 2, mean)
>
> quantile(mymeans1.2, c(0.025, 0.975))
>
> hist(mysamples1.2)
>
> mean(mymeans1.2)
>
> #what is p value?
>
>
> #similarly Now I want to compare between year 1 and year3,
>
> dif1.3<-daT$year3-daT$year1
>
> mysamples1.3=replicate(k, sample(dif1.3, replace=T))
>
> mymeans1.3=apply(mysamples1.3, 2, mean)
>
> quantile(mymeans1.3, c(0.025, 0.975))
>
>
> [[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] loop over matrix: subscript out of bounds

2018-08-08 Thread Rui Barradas

Hello,

There are now three solutions to the OP's problem.
I have timed them and the results depend on the matrix size.

The solution I thought would be better, Enrico's diag(), is in fact the 
slowest. As for the other two, Eric's for loop is 50% fastest than the 
matrix index for small matrices but its relative performance degrades as 
the matrix becomes bigger and bigger.



library(microbenchmark)

#Enrico Schumann
mkMat_diag <- function(nr = 5, nc = 7) {
M <- matrix(0, nr, nc)
diag(M) <- -1
diag(M[, -1]) <- 1
M
}

#Eric Berger
mkMat_loop <- function(nr = 5, nc = 7) {
M <- matrix(0, nr, nc)
for(i in 2:nrow(M)) {
   M[i - 1, i - 1] <- -1
   M[i - 1, i] <- 1
}
M
}


#S.Ellison
mkMat_index <- function(nr = 5, nc = 7) {
   M <- matrix(0, nr, nc)
   i <- 1:min(nr, nc)
   j <- i[i < nc]
   M[ cbind(i, i) ] <- -1
   M[ cbind(j, j + 1) ] <- 1
   M
}



microbenchmark(
loop = mkMat_loop(),
index = mkMat_index(),
diag = mkMat_diag(),
times = 1e3
)


microbenchmark(
loop = mkMat_loop(50, 70),
index = mkMat_index(50, 70),
diag = mkMat_diag(50, 70)
)


microbenchmark(
loop = mkMat_loop(500, 700),
index = mkMat_index(500, 700),
diag = mkMat_diag(500, 700)
)


Hope this helps,

Rui Barradas

On 08/08/2018 12:59, S Ellison wrote:



Eric Bergeron Wed, 8 Aug 2018 12:53:32 +0300 writes:



You only need one "for loop"
for(i in 2:nrow(myMatrix)) {
myMatrix[i-1,i-1] = -1
myMatrix[i-1,i] = 1
}


Or none, with matrix-based array indexing and explicit control of the indices 
to prevent overrun in :

mkMat <- function(n=5, m=7) {
M <- matrix(0, n,m)
i <- 1:min(n,m)
j <- i[ihttps://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] loop over matrix: subscript out of bounds

2018-08-08 Thread S Ellison
 
> > Eric Bergeron Wed, 8 Aug 2018 12:53:32 +0300 writes:
> 
> > You only need one "for loop"
> > for(i in 2:nrow(myMatrix)) {
> >myMatrix[i-1,i-1] = -1
> >myMatrix[i-1,i] = 1
> > }

Or none, with matrix-based array indexing and explicit control of the indices 
to prevent overrun in :

mkMat <- function(n=5, m=7) {
   M <- matrix(0, n,m)
   i <- 1:min(n,m)
   j <- i[ihttps://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] loop over matrix: subscript out of bounds

2018-08-08 Thread Martin Maechler
> Eric Bergeron Wed, 8 Aug 2018 12:53:32 +0300 writes:

> You only need one "for loop"
> for(i in 2:nrow(myMatrix)) {
>myMatrix[i-1,i-1] = -1
>myMatrix[i-1,i] = 1
> }
> 
> HTH,
> Eric

and why are you not using Enrico Schumann's even nicer solution
(from August 6) that I had mentioned too ?
Here's the link to it in the (official) R-help archives:
 https://stat.ethz.ch/pipermail/r-help/2018-August/455673.html

Maija said
> Thanks, but I didn't quite get it. And I don't get it running as it should.

and actually she is right that that version does not work for
all dimensions of 'myMatrix' -- it does need  ncol(.) >= 3
but neither does the above solution -- it only works for nrow(.) >= 2

Here's a function version of Enrico's that does work in all cases(!)
without a for loop -- including examples (as comments)

mkMat <- function(n=5, m=7) {
M <- matrix(0, n,m)
diag(M) <- -1
## this fails when m == ncol(M) <= 2, and ', drop=FALSE' does *not* help :
## diag(M[, -1]) <- 1
## diag(M[, -1, drop=FALSE]) <- 1
## This *does* work:
M[col(M) - row(M) == 1L] <- 1

M
}
mkMat()
##  [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]   -1100000
## [2,]0   -110000
## [3,]00   -11000
## [4,]000   -1100
## [5,]0000   -110
mkMat(3,5)
##  [,1] [,2] [,3] [,4] [,5]
## [1,]   -11000
## [2,]0   -1100
## [3,]00   -110
mkMat(5,3)
##  [,1] [,2] [,3]
## [1,]   -110
## [2,]0   -11
## [3,]00   -1
## [4,]000
## [5,]000

## Show that all small (m,n) work:
for(m in 0:3)
for(n in 0:3) {
cat(sprintf("(%d,%d):\n", n,m)); print(mkMat(n,m))
}

## (output not shown here)


> 
> On Wed, Aug 8, 2018 at 12:40 PM, Maija Sirkjärvi 
> wrote:
> 
> >   [.]
> >   [.]

> > However. The result that I would need to get would be like this:
> >
> > [,1] [,2] [,3] [,4] [,5] [,6] [,7]
> > [1,]  -1100000
> > [2,]   0   -110000
> > [3,]   00   -11000
> > [4,]   000   -1100
> > [5,]   0000   -110

__
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] loop over matrix: subscript out of bounds

2018-08-08 Thread Maija Sirkjärvi
Thanks a lot ! That's it!

Maija

ke 8. elok. 2018 klo 12.53 Eric Berger (ericjber...@gmail.com) kirjoitti:

> You only need one "for loop"
>
> for(i in 2:nrow(myMatrix)) {
>myMatrix[i-1,i-1] = -1
>myMatrix[i-1,i] = 1
> }
>
> HTH,
> Eric
>
>
> On Wed, Aug 8, 2018 at 12:40 PM, Maija Sirkjärvi <
> maija.sirkja...@gmail.com> wrote:
>
>> Thanks!
>>
>> If I do it like this:
>>
>> myMatrix <- matrix(0,5,5*2-3)
>> print(myMatrix)
>> for(i in 2:nrow(myMatrix))
>>   for(j in 2:ncol(myMatrix))
>> myMatrix[i-1,j-1] = -1
>> myMatrix[i-1,j] = 1
>> print(myMatrix)
>>
>> I get the following result:
>>
>>[,1] [,2] [,3] [,4] [,5] [,6] [,7]
>> [1,]   -1   -1   -1   -1   -1   -10
>> [2,]   -1   -1   -1   -1   -1   -10
>> [3,]   -1   -1   -1   -1   -1   -10
>> [4,]   -1   -1   -1   -1   -1   -11
>> [5,]0000000
>>
>> However. The result that I would need to get would be like this:
>>
>>[,1] [,2] [,3] [,4] [,5] [,6] [,7]
>> [1,]   -1   1   0   0   0   00
>> [2,]   0   -1   1   0   0   00
>> [3,]   0   0   -1   1   0   00
>> [4,]   0   0   0   -1   1   00
>> [5,]0000-1  10
>>
>> I'd rather not create symmetric matrices as I would really like to learn
>> how to do this thing "the hard way" as I find matrix iteration to be quite
>> a basic procedure in everything I'm trying to do.
>>
>> Thanks again!
>> Maija
>>
>>
>>
>>
>> ti 7. elok. 2018 klo 17.37 Rui Barradas (ruipbarra...@sapo.pt) kirjoitti:
>>
>> > Hello,
>> >
>> > If it is not running as you want it, you should say what went wrong.
>> > Post the code that you have tried and the expected output, please.
>> > (In fact, the lack of expected output was the reason why my suggestion
>> > was completely off target.)
>> >
>> > Rui Barradas
>> >
>> > On 07/08/2018 09:20, Maija Sirkjärvi wrote:
>> > > Thanks, but I didn't quite get it. And I don't get it running as it
>> > should.
>> > >
>> > > ti 7. elok. 2018 klo 10.47 Martin Maechler (
>> maech...@stat.math.ethz.ch
>> > > ) kirjoitti:
>> > >
>> > >
>> > >  > Thanks for help!
>> > >  > However, changing the index from i to j for the column vector
>> > > changes the
>> > >  > output. I would like the matrix to be the following:
>> > >
>> > >  > -1 1 0 0 0 0 0
>> > >  > 0 -1 1 0 0 0 0
>> > >  > 0 0 -1 1 0 0 0
>> > >  > .
>> > >  > etc.
>> > >
>> > >  > How to code it?
>> > >
>> > > as Enrico Schumann showed you:  Without any loop, a very nice
>> > > R-ish way (see his message)!
>> > >
>> > > Martin
>> > >
>> > >  > Best,
>> > >  > Maija
>> > >
>> >
>>
>> [[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] loop over matrix: subscript out of bounds

2018-08-08 Thread Eric Berger
You only need one "for loop"

for(i in 2:nrow(myMatrix)) {
   myMatrix[i-1,i-1] = -1
   myMatrix[i-1,i] = 1
}

HTH,
Eric


On Wed, Aug 8, 2018 at 12:40 PM, Maija Sirkjärvi 
wrote:

> Thanks!
>
> If I do it like this:
>
> myMatrix <- matrix(0,5,5*2-3)
> print(myMatrix)
> for(i in 2:nrow(myMatrix))
>   for(j in 2:ncol(myMatrix))
> myMatrix[i-1,j-1] = -1
> myMatrix[i-1,j] = 1
> print(myMatrix)
>
> I get the following result:
>
>[,1] [,2] [,3] [,4] [,5] [,6] [,7]
> [1,]   -1   -1   -1   -1   -1   -10
> [2,]   -1   -1   -1   -1   -1   -10
> [3,]   -1   -1   -1   -1   -1   -10
> [4,]   -1   -1   -1   -1   -1   -11
> [5,]0000000
>
> However. The result that I would need to get would be like this:
>
>[,1] [,2] [,3] [,4] [,5] [,6] [,7]
> [1,]   -1   1   0   0   0   00
> [2,]   0   -1   1   0   0   00
> [3,]   0   0   -1   1   0   00
> [4,]   0   0   0   -1   1   00
> [5,]0000-1  10
>
> I'd rather not create symmetric matrices as I would really like to learn
> how to do this thing "the hard way" as I find matrix iteration to be quite
> a basic procedure in everything I'm trying to do.
>
> Thanks again!
> Maija
>
>
>
>
> ti 7. elok. 2018 klo 17.37 Rui Barradas (ruipbarra...@sapo.pt) kirjoitti:
>
> > Hello,
> >
> > If it is not running as you want it, you should say what went wrong.
> > Post the code that you have tried and the expected output, please.
> > (In fact, the lack of expected output was the reason why my suggestion
> > was completely off target.)
> >
> > Rui Barradas
> >
> > On 07/08/2018 09:20, Maija Sirkjärvi wrote:
> > > Thanks, but I didn't quite get it. And I don't get it running as it
> > should.
> > >
> > > ti 7. elok. 2018 klo 10.47 Martin Maechler (maech...@stat.math.ethz.ch
> > > ) kirjoitti:
> > >
> > >
> > >  > Thanks for help!
> > >  > However, changing the index from i to j for the column vector
> > > changes the
> > >  > output. I would like the matrix to be the following:
> > >
> > >  > -1 1 0 0 0 0 0
> > >  > 0 -1 1 0 0 0 0
> > >  > 0 0 -1 1 0 0 0
> > >  > .
> > >  > etc.
> > >
> > >  > How to code it?
> > >
> > > as Enrico Schumann showed you:  Without any loop, a very nice
> > > R-ish way (see his message)!
> > >
> > > Martin
> > >
> > >  > Best,
> > >  > Maija
> > >
> >
>
> [[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] loop over matrix: subscript out of bounds

2018-08-08 Thread Maija Sirkjärvi
Thanks!

If I do it like this:

myMatrix <- matrix(0,5,5*2-3)
print(myMatrix)
for(i in 2:nrow(myMatrix))
  for(j in 2:ncol(myMatrix))
myMatrix[i-1,j-1] = -1
myMatrix[i-1,j] = 1
print(myMatrix)

I get the following result:

   [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]   -1   -1   -1   -1   -1   -10
[2,]   -1   -1   -1   -1   -1   -10
[3,]   -1   -1   -1   -1   -1   -10
[4,]   -1   -1   -1   -1   -1   -11
[5,]0000000

However. The result that I would need to get would be like this:

   [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]   -1   1   0   0   0   00
[2,]   0   -1   1   0   0   00
[3,]   0   0   -1   1   0   00
[4,]   0   0   0   -1   1   00
[5,]0000-1  10

I'd rather not create symmetric matrices as I would really like to learn
how to do this thing "the hard way" as I find matrix iteration to be quite
a basic procedure in everything I'm trying to do.

Thanks again!
Maija




ti 7. elok. 2018 klo 17.37 Rui Barradas (ruipbarra...@sapo.pt) kirjoitti:

> Hello,
>
> If it is not running as you want it, you should say what went wrong.
> Post the code that you have tried and the expected output, please.
> (In fact, the lack of expected output was the reason why my suggestion
> was completely off target.)
>
> Rui Barradas
>
> On 07/08/2018 09:20, Maija Sirkjärvi wrote:
> > Thanks, but I didn't quite get it. And I don't get it running as it
> should.
> >
> > ti 7. elok. 2018 klo 10.47 Martin Maechler (maech...@stat.math.ethz.ch
> > ) kirjoitti:
> >
> >
> >  > Thanks for help!
> >  > However, changing the index from i to j for the column vector
> > changes the
> >  > output. I would like the matrix to be the following:
> >
> >  > -1 1 0 0 0 0 0
> >  > 0 -1 1 0 0 0 0
> >  > 0 0 -1 1 0 0 0
> >  > .
> >  > etc.
> >
> >  > How to code it?
> >
> > as Enrico Schumann showed you:  Without any loop, a very nice
> > R-ish way (see his message)!
> >
> > Martin
> >
> >  > Best,
> >  > Maija
> >
>

[[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] loop over matrix: subscript out of bounds

2018-08-07 Thread Rui Barradas

Hello,

If it is not running as you want it, you should say what went wrong.
Post the code that you have tried and the expected output, please.
(In fact, the lack of expected output was the reason why my suggestion 
was completely off target.)


Rui Barradas

On 07/08/2018 09:20, Maija Sirkjärvi wrote:

Thanks, but I didn't quite get it. And I don't get it running as it should.

ti 7. elok. 2018 klo 10.47 Martin Maechler (maech...@stat.math.ethz.ch
) kirjoitti:


 > Thanks for help!
 > However, changing the index from i to j for the column vector
changes the
 > output. I would like the matrix to be the following:

 > -1 1 0 0 0 0 0
 > 0 -1 1 0 0 0 0
 > 0 0 -1 1 0 0 0
 > .
 > etc.

 > How to code it?

as Enrico Schumann showed you:  Without any loop, a very nice
R-ish way (see his message)!

Martin

 > Best,
 > Maija



__
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] loop over matrix: subscript out of bounds

2018-08-07 Thread Maija Sirkjärvi
Thanks, but I didn't quite get it. And I don't get it running as it should.

ti 7. elok. 2018 klo 10.47 Martin Maechler (maech...@stat.math.ethz.ch)
kirjoitti:

>
> > Thanks for help!
> > However, changing the index from i to j for the column vector changes the
> > output. I would like the matrix to be the following:
>
> > -1 1 0 0 0 0 0
> > 0 -1 1 0 0 0 0
> > 0 0 -1 1 0 0 0
> > .
> > etc.
>
> > How to code it?
>
> as Enrico Schumann showed you:  Without any loop, a very nice
> R-ish way (see his message)!
>
> Martin
>
> > Best,
> > Maija
>
>

[[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] loop over matrix: subscript out of bounds

2018-08-07 Thread Martin Maechler


> Thanks for help!
> However, changing the index from i to j for the column vector changes the
> output. I would like the matrix to be the following:

> -1 1 0 0 0 0 0
> 0 -1 1 0 0 0 0
> 0 0 -1 1 0 0 0
> .
> etc.

> How to code it?

as Enrico Schumann showed you:  Without any loop, a very nice
R-ish way (see his message)!

Martin

> Best,
> Maija

__
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] loop over matrix: subscript out of bounds

2018-08-07 Thread Maija Sirkjärvi
Thanks for help!

However, changing the index from i to j for the column vector changes the
output. I would like the matrix to be the following:

-1 1 0 0 0 0 0
0 -1 1 0 0 0 0
0 0 -1 1 0 0 0
.
etc.

How to code it?

Best,
Maija


>> myMatrix <- matrix(0,5,12)
>> for(i in 1:nrow(myMatrix)) {
>>for(i in 1:ncol(myMatrix)) {
>>  myMatrix[i,i] = -1
>>  myMatrix[i,i+1] = 1
>> }}
>> print(myMatrix)



ma 6. elok. 2018 klo 13.58 Rui Barradas (ruipbarra...@sapo.pt) kirjoitti:

> Hello,
>
> Eric is right but...
>
> You have two assignments. The second sets a value that will be
> overwritten is the next iteration by myMatrix[i,i] = -1 when 'i' becomes
> the next value.
>
> If you fix the second index and use 'j', you might as well do
>
> myMatrix[] = -1
> myMatrix[, ncol(myMatrix)] = 1
>
> Hope this helps,
>
> Rui Barradas
>
> Às 10:24 de 06/08/2018, Eric Berger escreveu:
> > Both loops are on 'i', which is a bad idea. :-)
> > Also myMatrix[i,i+1] will be out-of-bounds if i = ncol(myMatrix)
> >
> >
> > On Mon, Aug 6, 2018 at 12:02 PM, Maija Sirkjärvi <
> maija.sirkja...@gmail.com>
> > wrote:
> >
> >> I have a basic for loop with a simple matrix. The code is doing what it
> is
> >> supposed to do, but I'm still wondering the error "subscript out of
> >> bounds". What would be a smoother way to code such a basic for loop?
> >>
> >> myMatrix <- matrix(0,5,12)
> >> for(i in 1:nrow(myMatrix)) {
> >>for(i in 1:ncol(myMatrix)) {
> >>  myMatrix[i,i] = -1
> >>  myMatrix[i,i+1] = 1
> >> }}
> >> print(myMatrix)
> >>
> >> Thanks in advance!
> >>
> >>  [[alternative HTML version deleted]]
> >>
> >> __
> >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide http://www.R-project.org/
> >> posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
> >   [[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] loop over matrix: subscript out of bounds

2018-08-06 Thread Rui Barradas

Hello,

Eric is right but...

You have two assignments. The second sets a value that will be 
overwritten is the next iteration by myMatrix[i,i] = -1 when 'i' becomes 
the next value.


If you fix the second index and use 'j', you might as well do

myMatrix[] = -1
myMatrix[, ncol(myMatrix)] = 1

Hope this helps,

Rui Barradas

Às 10:24 de 06/08/2018, Eric Berger escreveu:

Both loops are on 'i', which is a bad idea. :-)
Also myMatrix[i,i+1] will be out-of-bounds if i = ncol(myMatrix)


On Mon, Aug 6, 2018 at 12:02 PM, Maija Sirkjärvi 
wrote:


I have a basic for loop with a simple matrix. The code is doing what it is
supposed to do, but I'm still wondering the error "subscript out of
bounds". What would be a smoother way to code such a basic for loop?

myMatrix <- matrix(0,5,12)
for(i in 1:nrow(myMatrix)) {
   for(i in 1:ncol(myMatrix)) {
 myMatrix[i,i] = -1
 myMatrix[i,i+1] = 1
}}
print(myMatrix)

Thanks in advance!

 [[alternative HTML version deleted]]

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



[[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] loop over matrix: subscript out of bounds

2018-08-06 Thread Enrico Schumann



Quoting Maija Sirkjärvi :


I have a basic for loop with a simple matrix. The code is doing what it is
supposed to do, but I'm still wondering the error "subscript out of
bounds". What would be a smoother way to code such a basic for loop?

myMatrix <- matrix(0,5,12)
for(i in 1:nrow(myMatrix)) {
  for(i in 1:ncol(myMatrix)) {
myMatrix[i,i] = -1
myMatrix[i,i+1] = 1
}}
print(myMatrix)

Thanks in advance!



Perhaps you do not need loops at all?

myMatrix <- matrix(0, 5, 12)
diag(myMatrix) <- -1
diag(myMatrix[, -1]) <- 1

--
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net

__
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] loop over matrix: subscript out of bounds

2018-08-06 Thread Eric Berger
Both loops are on 'i', which is a bad idea. :-)
Also myMatrix[i,i+1] will be out-of-bounds if i = ncol(myMatrix)


On Mon, Aug 6, 2018 at 12:02 PM, Maija Sirkjärvi 
wrote:

> I have a basic for loop with a simple matrix. The code is doing what it is
> supposed to do, but I'm still wondering the error "subscript out of
> bounds". What would be a smoother way to code such a basic for loop?
>
> myMatrix <- matrix(0,5,12)
> for(i in 1:nrow(myMatrix)) {
>   for(i in 1:ncol(myMatrix)) {
> myMatrix[i,i] = -1
> myMatrix[i,i+1] = 1
> }}
> print(myMatrix)
>
> Thanks in advance!
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[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] Loop Function to Create Multiple Scatterplots

2018-05-21 Thread MacQueen, Don
Here is a simplified example:

dat <- data.frame(x=1:4, y1=runif(4), y2=runif(4), y3=4:1)

for (icol in 2:4) plot(dat[,1] , dat[,icol] )

(not tested, so hopefully all my parentheses are balanced, no typos, etc.)

This shows the basic principle.

An alternative is to construct each column name as a text string within the 
loop, and use that in the plot() call.

-Don

--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
 
 

On 5/20/18, 4:05 PM, "R-help on behalf of STEVEN MANCINI" 
 wrote:

Hello,

I am trying to create multiple scatter plot graphs. I have 1 independent
variable (Age - weeks post conception) and 18 dependent variables ("Gene n"
Expression) in one csv file. Is there a way to set up a looped function to
produce 18 individual scatterplots? At the moment, I am writing the plot()
function out 18 times to make the 18 graphs. My code is below and csv file
is attached.

*Code*
wd <- setwd("~/Dropbox/Steve/SM Research Projects/Allen Brain Bank
Project/Allen Brain Bank Inflammatory Markers Project Matlab:R/Other/2018
Tests")
list.files(wd)
mydata <- read.csv("Glutamate.Genes.V1.csv")
mydata

plot(mydata$Age..weeks.post.conception., mydata$GluA1..GRIA1..Expression)
plot(mydata$Age..weeks.post.conception.,
mydata$GluA2..GluR2.GRIA2..Expression), etc

Thank you for your time and help.

Regards,
Steven
__
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] Loop Function to Create Multiple Scatterplots

2018-05-21 Thread John Kane via R-help
 As Jim says, "No data". R-help is very fussy about what files it will accept.  
You might try changing the extention to txt.  However the preferred way here is 
to use the dput() command and paste the results into the post.  See ?dput for 
details. 



On Monday, May 21, 2018, 1:40:59 a.m. EDT, STEVEN MANCINI 
 wrote:  
 
 Hello,

I am trying to create multiple scatter plot graphs. I have 1 independent
variable (Age - weeks post conception) and 18 dependent variables ("Gene n"
Expression) in one csv file. Is there a way to set up a looped function to
produce 18 individual scatterplots? At the moment, I am writing the plot()
function out 18 times to make the 18 graphs. My code is below and csv file
is attached.

*Code*
wd <- setwd("~/Dropbox/Steve/SM Research Projects/Allen Brain Bank
Project/Allen Brain Bank Inflammatory Markers Project Matlab:R/Other/2018
Tests")
list.files(wd)
mydata <- read.csv("Glutamate.Genes.V1.csv")
mydata

plot(mydata$Age..weeks.post.conception., mydata$GluA1..GRIA1..Expression)
plot(mydata$Age..weeks.post.conception.,
mydata$GluA2..GluR2.GRIA2..Expression), etc

Thank you for your time and help.

Regards,
Steven
__
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] Loop Function to Create Multiple Scatterplots

2018-05-21 Thread Jim Lemon
Hi Steven,
Sad to say that your CSV file didn't make it to the list and I can't
access the data via your Dropbox account. Therefore we don't know the
structure of "mydata". If you are able to plot the data as in your
example, this might help:

genexp<-matrix(runif(360,1,2),ncol=18)
colnames(genexp)<-paste("GluA.",1:18,"GRIA",1:18,"..Expression",sep="")
mydata<-cbind(Age..weeks.post.conception=20:38,genexp)
for(genecol in colnames(mydata)[2:19]) {
 png(paste0(genecol,".png"))
 plot(mydata$Age..weeks.post.conception,mydata$genecol)
}

Jim

On Mon, May 21, 2018 at 9:05 AM, STEVEN MANCINI  wrote:
> Hello,
>
> I am trying to create multiple scatter plot graphs. I have 1 independent
> variable (Age - weeks post conception) and 18 dependent variables ("Gene n"
> Expression) in one csv file. Is there a way to set up a looped function to
> produce 18 individual scatterplots? At the moment, I am writing the plot()
> function out 18 times to make the 18 graphs. My code is below and csv file
> is attached.
>
> *Code*
> wd <- setwd("~/Dropbox/Steve/SM Research Projects/Allen Brain Bank
> Project/Allen Brain Bank Inflammatory Markers Project Matlab:R/Other/2018
> Tests")
> list.files(wd)
> mydata <- read.csv("Glutamate.Genes.V1.csv")
> mydata
>
> plot(mydata$Age..weeks.post.conception., mydata$GluA1..GRIA1..Expression)
> plot(mydata$Age..weeks.post.conception.,
> mydata$GluA2..GluR2.GRIA2..Expression), etc
>
> Thank you for your time and help.
>
> Regards,
> Steven
> __
> 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] loop with variable names

2016-11-05 Thread Bert Gunter
1. Statistically, you probably don't want to do this at all (but
that's another story).

2. Programatically, you probably want to use one of several packages
that do it already rather than trying to reinvent the wheel. A quick
search on rseek.org for "all subsets regression" brought up this:

http://rpubs.com/kaz_yos/all-subset

Cheers,
Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Fri, Nov 4, 2016 at 9:53 AM, paolo brunori  wrote:
> Suppose I have the following data:
>
> y<-rnorm(10)
> age<-rnorm(10)
> sex<-rbinom(10,1, 0.5)
> edu<-round(runif(10, 1, 20))
> edu2<-edu^2
>
> df<-data.frame(y,age,sex,edu,edu2)
>
> I want to run a large number of models, for example:
>
> lm(y~age)
> lm(y~age+sex)
> lm(y~age+sex+edu)
> lm(y~age+sex+edu+edu2)
> lm(y~sex+edu2)
> lm(y~age+edu+edu2)
> 
>
> But I would like to first define a list containing all possible sets of
> regressors, and then execute each one in a loop/lapply. Unfortunately I got
> lost in trying to paste variables' name in the formula with no result.
>
> many thanks in advance.
>
> paolo
>
>
>
> --
> Paolo Brunori
> Ricercatore in Economia Politica & Life Course Centre Fellow
> Dipartimento di Scienze Economiche - Università di Bari
> www.uniba.it/docenti/brunori-paolo
> www.equalchances.org
> www.lifecoursecentre.org.au
>
> __
> 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] loop with variable names

2016-11-04 Thread Ista Zahn
It's hard to imagine a situation where this makes sense, but of course
you can do it if you want. Perhaps

rhs <- unlist(sapply(1:(ncol(df)-1), function(x)
apply(combn(names(df)[-1], x), 2, paste, collapse = " + ")))

lapply(rhs, function(x) lm(as.formula(paste("y ~", x)), data = df))

--Ista
On Fri, Nov 4, 2016 at 12:53 PM, paolo brunori  wrote:
> Suppose I have the following data:
>
> y<-rnorm(10)
> age<-rnorm(10)
> sex<-rbinom(10,1, 0.5)
> edu<-round(runif(10, 1, 20))
> edu2<-edu^2
>
> df<-data.frame(y,age,sex,edu,edu2)
>
> I want to run a large number of models, for example:
>
> lm(y~age)
> lm(y~age+sex)
> lm(y~age+sex+edu)
> lm(y~age+sex+edu+edu2)
> lm(y~sex+edu2)
> lm(y~age+edu+edu2)
> 
>
> But I would like to first define a list containing all possible sets of
> regressors, and then execute each one in a loop/lapply. Unfortunately I got
> lost in trying to paste variables' name in the formula with no result.
>
> many thanks in advance.
>
> paolo
>
>
>
> --
> Paolo Brunori
> Ricercatore in Economia Politica & Life Course Centre Fellow
> Dipartimento di Scienze Economiche - Università di Bari
> www.uniba.it/docenti/brunori-paolo
> www.equalchances.org
> www.lifecoursecentre.org.au
>
> __
> 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] Loop to check for large dataset

2016-10-10 Thread Adrian Dușa
Granted,, there are better solutions than my "KISS" (keep it simple and
stupid) example.

Hopefully, Christoph will have learned from both.

Best,
Adrian

On 10 Oct 2016 13:44, "PIKAL Petr" <petr.pi...@precheza.cz> wrote:

> Hi
>
>
>
> Given this example data, you can get same answer with less typing and
> without loops.
>
>
>
> res<-xtabs(~W+P+S,mydata)
>
> res1<-which(res==0, arr.ind=T)
>
> head(res1)
>
>   W P S
>
> 10   10 1 1
>
> 11   11 1 1
>
> 82   82 1 1
>
> 100 100 1 1
>
> 117 117 1 1
>
> 148 148 1 1
>
>
>
> Cheers
>
> Petr
>
>
>
>
>
> *From:* dusa.adr...@gmail.com [mailto:dusa.adr...@gmail.com] *On Behalf
> Of *Adrian Du?a
> *Sent:* Monday, October 10, 2016 12:26 PM
> *To:* Christoph Puschmann <c.puschm...@student.unsw.edu.au>
> *Cc:* r-help@r-project.org; PIKAL Petr <petr.pi...@precheza.cz>
> *Subject:* Re: [R] Loop to check for large dataset
>
>
>
> This is an example of how a reproducible code looks like, assuming you
> have three columns in your dataset named S (store), P (product) and W
> (week), and also assuming they have integer values from 1 to 19, 1 to 22
> and 1 to 157 respectively:
>
> #
>
> mydata <- expand.grid(seq(19), seq(22), seq(157))
> names(mydata) <- c("S", "P", "W")
>
> # randomly delete 65626 - 63127 = 2499 rows
> set.seed(12345) # make it replicable
>
> mydata <- mydata[-sample(seq(nrow(mydata)), nrow(mydata) - 63127), ]
>
> #
>
>
> Now the dataframe mydata contains exactly 63127 rows, just as in your
> case. The task is to find which weeks are missing, from which store and for
> which product.
>
> Below is a possible code to do that. Given you have a small number of
> stores and products, I'll keep it simple and stupid, by using for loops:
>
>
>
>
>
> #
>
>
>
> result <- matrix(nrow = 0, ncol = 3)
>
>
>
> for (i in seq(19)) {
>
> for (j in seq(22)) {
>
> miss <- setdiff(seq(157), mydata$W[mydata$S == i & mydata$P == j])
>
> if (length(miss) > 0) {
>
> result <- rbind(result, cbind(S = i, P = j, W = miss))
>
> }
>
> }
>
> }
>
>
>
> # The result matrix contains 2499 rows that are missing.
>
>
>
> > head(result)
>
>  S P   W
>
> [1,] 1 1  10
>
> [2,] 1 1  11
>
> [3,] 1 1  82
>
> [4,] 1 1 100
>
> [5,] 1 1 117
>
> [6,] 1 1 148
>
>
>
> #
>
>
>
>
>
> In this example, for S(tore) number 1 and P(roduct) number 1, you are
> missing W(eek) 10, 11, 82 and so on.
>
>
>
> In hoping you can adapt this code to your particular example,
>
> Adrian
>
>
>
> On Sun, Oct 9, 2016 at 2:26 AM, Christoph Puschmann <
> c.puschm...@student.unsw.edu.au> wrote:
> >
> > Dear Adrian,
> >
> > Yes it is a cyclical data set and theoretically it should repeat this
> interval until 61327. The data set itself is divided into 2 Parts:
> > 1. Product category (column 10)
> > 2. Number of Stores Participating (column 01)
> > Overall there are 22 different products and in each you have 19
> different stores participating. And theoretically each store over each
> product category should have a 1 - 157 week interval.
> >
> > The part I am struggling with is how do I run a loop over the whole data
> set, while checking if all stores participated 157 weeks over the different
> products.
> >
> > So far I came up with this:
> >
> > n=61327   # Generate Matrix to check for values
> > Control = matrix(
> >   0,
> >   nrow = n,
> >   ncol = 1)
> >
> > s <- seq(from =1 , to = 157, by = 1)
> > CW = matrix(
> >   s,
> >   nrow = 157,
> >   ncol = 1
> > )
> >
> > colnames(CW)[1] <- ’s'
> >
> > CW = as.data.frame(CW)
> >
> > for (i in 1:nrow(FD)) {   # Let run trhough all the rows
> >   for (j in 1:157) {
> > if(FD$WEEk[j] == C$s[j]) {
> >   Control[i] = 1 # coresponding control row = 1
> > } else {
> >   Control[i] = 0 # corresponding control row = 0
> > }
> > }
> > }
> >
> > I coded a  MRE and attached an sample of my data set.
> >
> > MRE:
> >
> > #MRE
> >
> > dat <- data.frame(
> >   Store = c(rep(8, times = 157), rep(12, times = 157)),  # Number of
> stores
> >   WEEK = rep(seq(from=1, to = 157, by = 1), times = 2)
> > )
>

Re: [R] Loop to check for large dataset

2016-10-10 Thread PIKAL Petr
Hi

Given this example data, you can get same answer with less typing and without 
loops.

res<-xtabs(~W+P+S,mydata)
res1<-which(res==0, arr.ind=T)
head(res1)
  W P S
10   10 1 1
11   11 1 1
82   82 1 1
100 100 1 1
117 117 1 1
148 148 1 1

Cheers
Petr


From: dusa.adr...@gmail.com [mailto:dusa.adr...@gmail.com] On Behalf Of Adrian 
Du?a
Sent: Monday, October 10, 2016 12:26 PM
To: Christoph Puschmann <c.puschm...@student.unsw.edu.au>
Cc: r-help@r-project.org; PIKAL Petr <petr.pi...@precheza.cz>
Subject: Re: [R] Loop to check for large dataset

This is an example of how a reproducible code looks like, assuming you have 
three columns in your dataset named S (store), P (product) and W (week), and 
also assuming they have integer values from 1 to 19, 1 to 22 and 1 to 157 
respectively:

#
mydata <- expand.grid(seq(19), seq(22), seq(157))
names(mydata) <- c("S", "P", "W")

# randomly delete 65626 - 63127 = 2499 rows
set.seed(12345) # make it replicable
mydata <- mydata[-sample(seq(nrow(mydata)), nrow(mydata) - 63127), ]
#


Now the dataframe mydata contains exactly 63127 rows, just as in your case. The 
task is to find which weeks are missing, from which store and for which product.
Below is a possible code to do that. Given you have a small number of stores 
and products, I'll keep it simple and stupid, by using for loops:


#

result <- matrix(nrow = 0, ncol = 3)

for (i in seq(19)) {
for (j in seq(22)) {
miss <- setdiff(seq(157), mydata$W[mydata$S == i & mydata$P == j])
if (length(miss) > 0) {
result <- rbind(result, cbind(S = i, P = j, W = miss))
}
}
}

# The result matrix contains 2499 rows that are missing.

> head(result)
 S P   W
[1,] 1 1  10
[2,] 1 1  11
[3,] 1 1  82
[4,] 1 1 100
[5,] 1 1 117
[6,] 1 1 148

#


In this example, for S(tore) number 1 and P(roduct) number 1, you are missing 
W(eek) 10, 11, 82 and so on.

In hoping you can adapt this code to your particular example,
Adrian


On Sun, Oct 9, 2016 at 2:26 AM, Christoph Puschmann 
<c.puschm...@student.unsw.edu.au<mailto:c.puschm...@student.unsw.edu.au>> wrote:
>
> Dear Adrian,
>
> Yes it is a cyclical data set and theoretically it should repeat this 
> interval until 61327. The data set itself is divided into 2 Parts:
> 1. Product category (column 10)
> 2. Number of Stores Participating (column 01)
> Overall there are 22 different products and in each you have 19 different 
> stores participating. And theoretically each store over each product category 
> should have a 1 - 157 week interval.
>
> The part I am struggling with is how do I run a loop over the whole data set, 
> while checking if all stores participated 157 weeks over the different 
> products.
>
> So far I came up with this:
>
> n=61327   # Generate Matrix to check for values
> Control = matrix(
>   0,
>   nrow = n,
>   ncol = 1)
>
> s <- seq(from =1 , to = 157, by = 1)
> CW = matrix(
>   s,
>   nrow = 157,
>   ncol = 1
> )
>
> colnames(CW)[1] <- ’s'
>
> CW = as.data.frame(CW)
>
> for (i in 1:nrow(FD)) {   # Let run trhough all the rows
>   for (j in 1:157) {
> if(FD$WEEk[j] == C$s[j]) {
>   Control[i] = 1 # coresponding control row = 1
> } else {
>   Control[i] = 0 # corresponding control row = 0
> }
> }
> }
>
> I coded a  MRE and attached an sample of my data set.
>
> MRE:
>
> #MRE
>
> dat <- data.frame(
>   Store = c(rep(8, times = 157), rep(12, times = 157)),  # Number of stores
>   WEEK = rep(seq(from=1, to = 157, by = 1), times = 2)
> )
>
>
>
>



--
Adrian Dusa
University of Bucharest
Romanian Social Data Archive
Soseaua Panduri nr.90
050663 Bucharest sector 5
Romania


Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny 
pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně 
jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze 
svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email 
jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či 
zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a 
to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce 
s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným 
dosažením shody na všech je

Re: [R] Loop to check for large dataset

2016-10-10 Thread Adrian Dușa
This is an example of how a reproducible code looks like, assuming you have
three columns in your dataset named S (store), P (product) and W (week),
and also assuming they have integer values from 1 to 19, 1 to 22 and 1 to
157 respectively:

#

mydata <- expand.grid(seq(19), seq(22), seq(157))
names(mydata) <- c("S", "P", "W")

# randomly delete 65626 - 63127 = 2499 rows
set.seed(12345) # make it replicable

mydata <- mydata[-sample(seq(nrow(mydata)), nrow(mydata) - 63127), ]

#


Now the dataframe mydata contains exactly 63127 rows, just as in your case.
The task is to find which weeks are missing, from which store and for which
product.
Below is a possible code to do that. Given you have a small number of
stores and products, I'll keep it simple and stupid, by using for loops:


#

result <- matrix(nrow = 0, ncol = 3)

for (i in seq(19)) {
for (j in seq(22)) {
miss <- setdiff(seq(157), mydata$W[mydata$S == i & mydata$P == j])
if (length(miss) > 0) {
result <- rbind(result, cbind(S = i, P = j, W = miss))
}
}
}

# The result matrix contains 2499 rows that are missing.

> head(result)
 S P   W
[1,] 1 1  10
[2,] 1 1  11
[3,] 1 1  82
[4,] 1 1 100
[5,] 1 1 117
[6,] 1 1 148

#


In this example, for S(tore) number 1 and P(roduct) number 1, you are
missing W(eek) 10, 11, 82 and so on.

In hoping you can adapt this code to your particular example,
Adrian


On Sun, Oct 9, 2016 at 2:26 AM, Christoph Puschmann <
c.puschm...@student.unsw.edu.au> wrote:
>
> Dear Adrian,
>
> Yes it is a cyclical data set and theoretically it should repeat this
interval until 61327. The data set itself is divided into 2 Parts:
> 1. Product category (column 10)
> 2. Number of Stores Participating (column 01)
> Overall there are 22 different products and in each you have 19 different
stores participating. And theoretically each store over each product
category should have a 1 - 157 week interval.
>
> The part I am struggling with is how do I run a loop over the whole data
set, while checking if all stores participated 157 weeks over the different
products.
>
> So far I came up with this:
>
> n=61327   # Generate Matrix to check for values
> Control = matrix(
>   0,
>   nrow = n,
>   ncol = 1)
>
> s <- seq(from =1 , to = 157, by = 1)
> CW = matrix(
>   s,
>   nrow = 157,
>   ncol = 1
> )
>
> colnames(CW)[1] <- ’s'
>
> CW = as.data.frame(CW)
>
> for (i in 1:nrow(FD)) {   # Let run trhough all the rows
>   for (j in 1:157) {
> if(FD$WEEk[j] == C$s[j]) {
>   Control[i] = 1 # coresponding control row = 1
> } else {
>   Control[i] = 0 # corresponding control row = 0
> }
> }
> }
>
> I coded a  MRE and attached an sample of my data set.
>
> MRE:
>
> #MRE
>
> dat <- data.frame(
>   Store = c(rep(8, times = 157), rep(12, times = 157)),  # Number of
stores
>   WEEK = rep(seq(from=1, to = 157, by = 1), times = 2)
> )
>
>
>
>



--
Adrian Dusa
University of Bucharest
Romanian Social Data Archive
Soseaua Panduri nr.90
050663 Bucharest sector 5
Romania

[[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] Loop to check for large dataset

2016-10-10 Thread PIKAL Petr
Hi

I named your data test

res <- xtabs(~STORE+WEEK+Description, data=test)

should give you values in which there is for given Description WEEK and STORE 
missing.

you can select week and store by

which(res[, ,1]==0, arr.ind=T)

for description 1 and so on.

Another option is to generate full set STORE, WEEK and description and merge it 
with original data by merge.

Cheers
Petr

From: Christoph Puschmann [mailto:c.puschm...@student.unsw.edu.au]
Sent: Monday, October 10, 2016 9:34 AM
To: PIKAL Petr <petr.pi...@precheza.cz>; r-help@r-project.org
Subject: Re: [R] Loop to check for large dataset

Dear Petr,

I attached a sample file, which contains the first 4 products.

It is more that I have: 157 weeks, 19 different Stores and 22 products: 
157*19*22 = 65,626 rows. And as I sated I have roughly 63,127 rows. (so some 
have to be missing).

All the best,

Christoph



Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny 
pouze jeho adres?t?m.
Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? 
jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze 
sv?ho syst?mu.
Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email 
jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat.
Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i 
zpo?d?n?m p?enosu e-mailu.

V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?:
- vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a 
to z jak?hokoliv d?vodu i bez uveden? d?vodu.
- a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; 
Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce 
s dodatkem ?i odchylkou.
- trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m 
dosa?en?m shody na v?ech jej?ch n?le?itostech.
- odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost 
??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? 
pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? 
osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi 
?i osob? j?m zastoupen? zn?m?.

This e-mail and any documents attached to it may be confidential and are 
intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. 
Delete the contents of this e-mail with all attachments and its copies from 
your system.
If you are not the intended recipient of this e-mail, you are not authorized to 
use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by 
modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a 
contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately 
accept such offer; The sender of this e-mail (offer) excludes any acceptance of 
the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an 
express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into 
any contracts on behalf of the company except for cases in which he/she is 
expressly authorized to do so in writing, and such authorization or power of 
attorney is submitted to the recipient or the person represented by the 
recipient, or the existence of such authorization is known to the recipient of 
the person represented by the recipient.

[[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] Loop to check for large dataset

2016-10-10 Thread Christoph Puschmann
Dear Petr,

I attached a sample file, which contains the first 4 products.

It is more that I have: 157 weeks, 19 different Stores and 22 products: 
157*19*22 = 65,626 rows. And as I sated I have roughly 63,127 rows. (so some 
have to be missing).

All the best,

Christoph

__
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] Loop to check for large dataset

2016-10-10 Thread PIKAL Petr
Hi

see in line

> -Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Christoph
> Puschmann
> Sent: Sunday, October 9, 2016 1:27 AM
> To: Adrian Dușa <dusa.adr...@unibuc.ro>
> Cc: r-help@r-project.org; Christoph Puschmann
> <c.puschm...@student.unsw.edu.au>
> Subject: Re: [R] Loop to check for large dataset
>
> Dear Adrian,
>
> Yes it is a cyclical data set and theoretically it should repeat this 
> interval until
> 61327. The data set itself is divided into 2 Parts:
> 1. Product category (column 10)
> 2. Number of Stores Participating (column 01) Overall there are 22 different
> products and in each you have 19 different stores participating. And
> theoretically each store over each product category should have a 1 - 157
> week interval.

Not much clearer and definitely not reproducible.

From what I understand you have 22*19= 418 combinations of product/store. How 
do you want to put these 418 combinations into 157 rows?

It seems to me that it can be somehow done with aggregate function, however 
without some small reproducible example we are fishing in murky water.

Try to post data with let say 3 stores and 4 products to explain how your data 
is structured and what is or is not correct.

Cheers
Petr

>
> The part I am struggling with is how do I run a loop over the whole data set,
> while checking if all stores participated 157 weeks over the different
> products.
>
> So far I came up with this:
>
> n=61327   # Generate Matrix to check for values
> Control = matrix(
>   0,
>   nrow = n,
>   ncol = 1)
>
> s <- seq(from =1 , to = 157, by = 1)
> CW = matrix(
>   s,
>   nrow = 157,
>   ncol = 1
> )
>
> colnames(CW)[1] <- ’s'
>
> CW = as.data.frame(CW)
>
> for (i in 1:nrow(FD)) {   # Let run trhough all the rows
>   for (j in 1:157) {
> if(FD$WEEk[j] == C$s[j]) {
>   Control[i] = 1 # coresponding control row = 1
> } else {
>   Control[i] = 0 # corresponding control row = 0
> }
> }
> }
>
> I coded a  MRE and attached an sample of my data set.
>
> MRE:
>
> #MRE
>
> dat <- data.frame(
>   Store = c(rep(8, times = 157), rep(12, times = 157)),  # Number of stores
>   WEEK = rep(seq(from=1, to = 157, by = 1), times = 2)
> )
>
>
>
>
> __
> 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.


Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny 
pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně 
jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze 
svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email 
jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či 
zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a 
to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce 
s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným 
dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost 
žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně 
pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně 
osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi 
či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are 
intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. 
Delete the contents of this e-mail with all attachments and its copies from 
your system.
If you are not the intended recipient of this e-mail, you are not authorized to 
use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by 
modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a 
contract in any time, for any reason, and without stating any reaso

Re: [R] Loop to check for large dataset

2016-10-08 Thread Christoph Puschmann
Dear Adrian,

Yes it is a cyclical data set and theoretically it should repeat this interval 
until 61327. The data set itself is divided into 2 Parts:
1. Product category (column 10)
2. Number of Stores Participating (column 01)
Overall there are 22 different products and in each you have 19 different 
stores participating. And theoretically each store over each product category 
should have a 1 - 157 week interval.

The part I am struggling with is how do I run a loop over the whole data set, 
while checking if all stores participated 157 weeks over the different products.

So far I came up with this:

n=61327   # Generate Matrix to check for values
Control = matrix(
  0,
  nrow = n,
  ncol = 1)

s <- seq(from =1 , to = 157, by = 1)
CW = matrix(
  s,
  nrow = 157,
  ncol = 1
)

colnames(CW)[1] <- ’s'

CW = as.data.frame(CW)

for (i in 1:nrow(FD)) {   # Let run trhough all the rows
  for (j in 1:157) {
if(FD$WEEk[j] == C$s[j]) {
  Control[i] = 1 # coresponding control row = 1
} else {
  Control[i] = 0 # corresponding control row = 0
}
}
}

I coded a  MRE and attached an sample of my data set.

MRE:

#MRE

dat <- data.frame(
  Store = c(rep(8, times = 157), rep(12, times = 157)),  # Number of stores
  WEEK = rep(seq(from=1, to = 157, by = 1), times = 2)
)




__
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] Loop to check for large dataset

2016-10-08 Thread Adrian Dușa
It would help to have a minimal, reproducible example.
Unless revealing the structure of your FD object, it is difficult to
understand how a column having 61327 values would be "consistent over an 1
to 157 interval": is this interval cyclic until it reaches 61327 values?

>From your example using FD$WEEK, you are using a column called WEEK within
a dataframe names FD, and you only loop over the first 157 values of that
column. So where is the "column having 61327 values"?

For these types of problems you don't even need a loop, R is a vectorised
language (please note that you have a double loop but never use the "i"
one).

Very unclear, so please try to create a MRE, as the posting guide advices.


On Fri, Oct 7, 2016 at 11:22 PM, Christoph Puschmann <
c.puschm...@student.unsw.edu.au> wrote:

> Hey all,
>
> I would like to know if anyone, can put in the right direction of the
> following problem:
>
> I am currently want to use it to check if a column with a length of 61327
> is consistent over an 1 to 157 interval until the end of the column. In the
> case the interval is interrupted I want to know which values are missing
> and where the missing values are located. I started of with the following
> code to assign 1s, if we have a number ≤ 157 and 0 if not.
>
>
>
> I tried to do a double loop:
>
>
>
> n=61327
> Control = matrix(
> 0,
> nrow = n,
> ncol = 1)
>
>
>
> for (i in length(FD$WEEK)) {
> for (j in 1:157) {
> if(FD$WEEK[j] <=  157) {
>   Control[,1] = 1
> } else {
>   Control[,1] = 0
>   }
> }
> }
>
>
>
> I believe that this code is not correct, but I am unable to wrap my head
> around how I can check that the interval always will be followed.
>
> All the best,
>
> Christoph
>
>
> [[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.




-- 
Adrian Dusa
University of Bucharest
Romanian Social Data Archive
Soseaua Panduri nr.90
050663 Bucharest sector 5
Romania

[[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] Loop to check for large dataset

2016-10-08 Thread ruipbarradas

Hello,

I'm not at all sure if the following is what you need but instead of

for (i in length(FD$WEEK))

try

for (i in 1:length(FD$WEEK))

or even better

for (i in seq_len(FD$WEEK))

And use Control[i, 1], not Control[, 1]

Hope thi helps,

Rui Barradas





Citando Christoph Puschmann :


Hey all,

I would like to know if anyone, can put in the right direction of  
the following problem:


I am currently want to use it to check if a column with a length of  
61327 is consistent over an 1 to 157 interval until the end of the  
column. In the case the interval is interrupted I want to know which  
values are missing and where the missing values are located. I  
started of with the following code to assign 1s, if we have a number  
≤ 157 and 0 if not.




I tried to do a double loop:



n=61327
Control = matrix(
0,
nrow = n,
ncol = 1)



for (i in length(FD$WEEK)) {
for (j in 1:157) {
if(FD$WEEK[j] <=  157) {
  Control[,1] = 1
} else {
  Control[,1] = 0
  }
}
}



I believe that this code is not correct, but I am unable to wrap my  
head around how I can check that the interval always will be followed.


All the best,

Christoph


[[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] Loop over rda list files and using the attach function

2016-09-01 Thread Juan Ceccarelli Arias
Hi
I want to comment something.
When i added the detach(get(yyz)) the RAM consumption was considerable
reduced.
So, i want to declare this issue as solved and thank you all for your
assistance.
Good luck to all.

On Tue, Aug 30, 2016 at 6:24 PM, Juan Ceccarelli Arias 
wrote:

> The attach(get(yyz)) option i tried and it worked. The only issue, is that
> when i'm  trying to export the results, it takes a lot of time.
> Considerably more than Stata. Also, the computer almost collapses for a
> task which isn't so exhausting for my PC station.
> Im dubious...
>
> On Tue, Aug 30, 2016 at 3:42 PM,  wrote:
>
>> Hello,
>>
>> Try
>>
>> attach(get(yyz))
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>>
>>
>>
>>
>>
>>
>> Citando Juan Ceccarelli Arias :
>>
>> Hi.
>>> I need to loop over rda files.
>>> I generated the list of them.
>>> That's ok. The problem is that the name of the files are as _mm (eg
>>> 2010_01 is january or 2010, 2016_03 is march of 2016).
>>> So, when i try to use the attach function to create a simple table(age,
>>> sex) it fails.
>>> The only way to attach a file as is using
>>> attach(`2016_03`)
>>> The text above i have no idea how to declare it in my code below
>>>
>>>
>>> dd=list.files("C:/Users/Me/r", pattern="rda$", full.names=F)
>>> for (i in 1:length(dd)) {
>>> yyz=load(dd[i])
>>> attach(yyz)
>>> table(age, sex)
>>> rm(list=yyz)
>>>
>>> }
>>> This is the error it declares the loop:
>>> Error in attach(yyz) : file '2013_02' not found
>>>
>>>
>>> Thanks for your help and time
>>>
>>> [[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/posti
>>> ng-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] Loop over rda list files and using the attach function

2016-08-30 Thread Juan Ceccarelli Arias
The attach(get(yyz)) option i tried and it worked. The only issue, is that
when i'm  trying to export the results, it takes a lot of time.
Considerably more than Stata. Also, the computer almost collapses for a
task which isn't so exhausting for my PC station.
Im dubious...

On Tue, Aug 30, 2016 at 3:42 PM,  wrote:

> Hello,
>
> Try
>
> attach(get(yyz))
>
> Hope this helps,
>
> Rui Barradas
>
>
>
>
>
>
>
> Citando Juan Ceccarelli Arias :
>
> Hi.
>> I need to loop over rda files.
>> I generated the list of them.
>> That's ok. The problem is that the name of the files are as _mm (eg
>> 2010_01 is january or 2010, 2016_03 is march of 2016).
>> So, when i try to use the attach function to create a simple table(age,
>> sex) it fails.
>> The only way to attach a file as is using
>> attach(`2016_03`)
>> The text above i have no idea how to declare it in my code below
>>
>>
>> dd=list.files("C:/Users/Me/r", pattern="rda$", full.names=F)
>> for (i in 1:length(dd)) {
>> yyz=load(dd[i])
>> attach(yyz)
>> table(age, sex)
>> rm(list=yyz)
>>
>> }
>> This is the error it declares the loop:
>> Error in attach(yyz) : file '2013_02' not found
>>
>>
>> Thanks for your help and time
>>
>> [[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/posti
>> ng-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] Loop over rda list files and using the attach function

2016-08-30 Thread ruipbarradas

Hello,

Try

attach(get(yyz))

Hope this helps,

Rui Barradas







Citando Juan Ceccarelli Arias :


Hi.
I need to loop over rda files.
I generated the list of them.
That's ok. The problem is that the name of the files are as _mm (eg
2010_01 is january or 2010, 2016_03 is march of 2016).
So, when i try to use the attach function to create a simple table(age,
sex) it fails.
The only way to attach a file as is using
attach(`2016_03`)
The text above i have no idea how to declare it in my code below


dd=list.files("C:/Users/Me/r", pattern="rda$", full.names=F)
for (i in 1:length(dd)) {
yyz=load(dd[i])
attach(yyz)
table(age, sex)
rm(list=yyz)

}
This is the error it declares the loop:
Error in attach(yyz) : file '2013_02' not found


Thanks for your help and time

[[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] Loop over rda list files and using the attach function

2016-08-30 Thread Greg Snow
You can attach rda files directly with the attach function, no need to
load them first (see the what argument in the help for attach).  This
may do what you want more directly.

In general it is better to not use loops and attach for this kind of
thing.  It is better to store multiple data objects in a list, then
use lapply/sapply to apply functions to each element.


On Tue, Aug 30, 2016 at 9:43 AM, Juan Ceccarelli Arias
 wrote:
> Hi.
> I need to loop over rda files.
> I generated the list of them.
> That's ok. The problem is that the name of the files are as _mm (eg
> 2010_01 is january or 2010, 2016_03 is march of 2016).
> So, when i try to use the attach function to create a simple table(age,
> sex) it fails.
> The only way to attach a file as is using
> attach(`2016_03`)
> The text above i have no idea how to declare it in my code below
>
>
> dd=list.files("C:/Users/Me/r", pattern="rda$", full.names=F)
> for (i in 1:length(dd)) {
> yyz=load(dd[i])
> attach(yyz)
> table(age, sex)
> rm(list=yyz)
>
> }
> This is the error it declares the loop:
> Error in attach(yyz) : file '2013_02' not found
>
>
> Thanks for your help and time
>
> [[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.



-- 
Gregory (Greg) L. Snow Ph.D.
538...@gmail.com

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


Re: [R] Loop over rda list files and using the attach function

2016-08-30 Thread Jorge Cimentada
Here's the problem: when you load the object and name it yyz, its simply
storing the name of the data frame as a string. Naturally, when you you
attach the string, it throws an error. The loop actually loads the data
frame but inside yyz there's not a data frame.

One problem with load() is that you can't save it as an object; it simply
loads the data to the chosen environment. A different solution would be to
use .Rds, which allows to be saved as an object and makes your loop work.
However, you need to make sure your files are saved as .Rds with the
saveRDS() function. Here's an example with loadRDS() instead of load():

## Generate 10 data frames and save it to your working directory as .Rds
for (i in 1:10) {
x <- data.frame(a=rnorm(10), b=c("Yes","No"))
saveRDS(x, file=paste0("data",i,".rds")) # save as RDS
rm(x)
}


dd <- grep(".rds", list.files(), value=T) # vector with data file names
for (i in 1:length(dd)) {
yyz <- readRDS(dd[i]) # load data.frame and save it to yyz
print(with(yyz, table(a, b))) # print the table
rm(yyz) # remove data frame
}

Hope this helps. I found the solution through:
https://www.r-bloggers.com/a-better-way-of-saving-and-loading-objects-in-r/

*Jorge Cimentada*
*Ph.D. Candidate*
Dpt. Ciències Polítiques i Socials
Ramon Trias Fargas, 25-27 | 08005 Barcelona

Office 24.331
[Tel.] 697 382 009
jorge.ciment...@upf.edu
http://www.upf.edu/dcpis/



On Tue, Aug 30, 2016 at 5:43 PM, Juan Ceccarelli Arias 
wrote:

> Hi.
> I need to loop over rda files.
> I generated the list of them.
> That's ok. The problem is that the name of the files are as _mm (eg
> 2010_01 is january or 2010, 2016_03 is march of 2016).
> So, when i try to use the attach function to create a simple table(age,
> sex) it fails.
> The only way to attach a file as is using
> attach(`2016_03`)
> The text above i have no idea how to declare it in my code below
>
>
> dd=list.files("C:/Users/Me/r", pattern="rda$", full.names=F)
> for (i in 1:length(dd)) {
> yyz=load(dd[i])
> attach(yyz)
> table(age, sex)
> rm(list=yyz)
>
> }
> This is the error it declares the loop:
> Error in attach(yyz) : file '2013_02' not found
>
>
> Thanks for your help and time
>
> [[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] Loop over folder files

2016-08-24 Thread Juan Ceccarelli Arias
Ok. Please, declare this issue as solved.
And thanks again for your help.

On Wed, Aug 24, 2016 at 2:18 PM,  wrote:

> Maybe it's better to open a new thread.
>
> Rui Barradas
>
>
> Citando Juan Ceccarelli Arias :
>
> The error wasn't in the loop. It was in the file list.
> It's running now because i added full.names option to TRUE
> fuente=list.files("C:/Users/Jceccarelli/Bases/Stata", pattern="dta$",
> full.names=T)
> Now R can proccess the data. Now it callapses or stops because other kind
> of error.
> ¿Should i open another topic or just use the mail list?
> By the way, thank you all.
>
>
> On Wed, Aug 24, 2016 at 11:48 AM,  wrote:
>>
>>
>>
>>
>>
>>
>>
>>
>> *Hello, That means that probably the files are in a different
>> folder/directory. Use getwd() to see what is your current directory and
>> setwd("path/to/files") to set the right place where the files can be found.
>> Rui Barradas  *
>>
>> *Citando Juan Ceccarelli Arias >:*
>>
>> *I just doesn't work...*
>> *Im loading the read,dta13 package already.*
>> *When i try to perform a simple table(sex), i received the "File not
>> found" message.*
>> *However, if i load the data using the file.choose() option inside
>> read.dta13, i can open the stata file.*
>> *I don't know what am i doing wrong...*
>>
>> *On Tue, Aug 23, 2016 at 5:08 PM, > > wrote:*
>>>
>>>
>>>
>>>
>>>
>>>
>>> *Or maybe a print() statement on the table() in the loop.
>>> print(table(...)) Rui Barradas  *
>>>
>>> *Citando David Winsemius >> >:*
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> *On Aug 23, 2016, at 10:01 AM, Juan Ceccarelli Arias >> > wrote: Im running this but the code doesn't seem work.
>>> It just hangs out but doesn't show any error. for (i in 1:length(fuente)){
>>> xxx=read_dta(fuente[i]) table(xxx$cise, xxx$sexo) rm(xxx) }*
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> *I still find the behavior of R's `for`-loop to be rather puzzling. In
>>> this case you appear to be getting the operation done, but because you
>>> didn't assign those table values to a variable they just disappeared. Try
>>> this: XXX <- list() for (i in 1:length(fuente)){   xxx=read_dta(fuente[i])
>>>   XXX[[i]] <- table(xxx$cise, xxx$sexo)   rm(xxx) } str(XXX) Seems to me
>>> that if you can do assignment to the parent environment (without actually
>>> using assign( ..., env=...)  that you should also be able to see the
>>> results of evaluation occurring inside the for loop, but for-loops return
>>> NULL. So you see nothing. David.  *
>>>
>>> *On Tue, Aug 23, 2016 at 6:31 AM, >> > wrote:*
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> *Hello, The op could also use package sos to find that and other
>>> packages to read stata files. install.packages("sos") library(sos)
>>> findFn("stata") found 374 matches;  retrieving 19 pages 2 3 4 5 6 7 8 9 10
>>> 11 12 13 14 15 16 17 18 19 Downloaded 258 links in 121 packages The first
>>> package is readstata13 but there are others. Hope this helps, Rui Barradas
>>> Citando Michael Dewey >:
>>> Dear Juan If this is a Stata 13 file the package readstata13 available from
>>> CRAN may be of assistance. On 22/08/2016 18:40, Juan Ceccarelli Arias
>>> wrote: I removed the data,frame=True... I obtain this warnings... Error in
>>> read.dta(fuente[i]) : not a Stata version 5-12 .dta file In addition: There
>>> were 50 or more warnings (use warnings() to see the first 50) the
>>> warnings() throws this Warning messages: 1: In `levels<-`(`*tmp*`, value =
>>> if (nl == nL) as.character(labels) else paste0(labels,  ... : duplicated
>>> levels in factors are deprecated 2: In `levels<-`(`*tmp*`, value = if (nl
>>> == nL) as.character(labels) else paste0(labels,  ... : duplicated levels in
>>> factors are deprecated 3: In `levels<-`(`*tmp*`, value = if (nl == nL)
>>> as.character(labels) else paste0(labels,  ... : duplicated levels in
>>> factors are deprecated 4: In `levels<-`(`*tmp*`, value = if (nl == nL)
>>> as.character(labels) else paste0(labels,  ... : duplicated levels in
>>> factors are deprecated 5: In `levels<-`(`*tmp*`, value = if (nl == nL)
>>> as.character(labels) else paste0(labels,  ... : duplicated levels in
>>> factors are deprecated On Mon, Aug 22, 2016 at 2:32 PM,
>>> > wrote: Hello, That argument

Re: [R] Loop over folder files

2016-08-24 Thread Juan Ceccarelli Arias
The error wasn't in the loop. It was in the file list.
It's running now because i added full.names option to TRUE
fuente=list.files("C:/Users/Jceccarelli/Bases/Stata", pattern="dta$",
full.names=T)
Now R can proccess the data. Now it callapses or stops because other kind
of error.
¿Should i open another topic or just use the mail list?
By the way, thank you all.


On Wed, Aug 24, 2016 at 11:48 AM,  wrote:

> Hello,
>
> That means that probably the files are in a different folder/directory.
> Use getwd() to see what is your current directory and
> setwd("path/to/files") to set the right place where the files can be found.
>
> Rui Barradas
>
>
> Citando Juan Ceccarelli Arias :
>
> I just doesn't work...
> Im loading the read,dta13 package already.
> When i try to perform a simple table(sex), i received the "File not found"
> message.
> However, if i load the data using the file.choose() option inside
> read.dta13, i can open the stata file.
> I don't know what am i doing wrong...
>
> On Tue, Aug 23, 2016 at 5:08 PM,  wrote:
>>
>>
>>
>>
>>
>>
>> *Or maybe a print() statement on the table() in the loop.
>> print(table(...)) Rui Barradas  *
>>
>> *Citando David Winsemius > >:*
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> *On Aug 23, 2016, at 10:01 AM, Juan Ceccarelli Arias > > wrote: Im running this but the code doesn't seem work.
>> It just hangs out but doesn't show any error. for (i in 1:length(fuente)){
>> xxx=read_dta(fuente[i]) table(xxx$cise, xxx$sexo) rm(xxx) }*
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> *I still find the behavior of R's `for`-loop to be rather puzzling. In
>> this case you appear to be getting the operation done, but because you
>> didn't assign those table values to a variable they just disappeared. Try
>> this: XXX <- list() for (i in 1:length(fuente)){   xxx=read_dta(fuente[i])
>>   XXX[[i]] <- table(xxx$cise, xxx$sexo)   rm(xxx) } str(XXX) Seems to me
>> that if you can do assignment to the parent environment (without actually
>> using assign( ..., env=...)  that you should also be able to see the
>> results of evaluation occurring inside the for loop, but for-loops return
>> NULL. So you see nothing. David.  *
>>
>> *On Tue, Aug 23, 2016 at 6:31 AM, > > wrote:*
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> *Hello, The op could also use package sos to find that and other packages
>> to read stata files. install.packages("sos") library(sos) findFn("stata")
>> found 374 matches;  retrieving 19 pages 2 3 4 5 6 7 8 9 10 11 12 13 14 15
>> 16 17 18 19 Downloaded 258 links in 121 packages The first package is
>> readstata13 but there are others. Hope this helps, Rui Barradas Citando
>> Michael Dewey >: Dear
>> Juan If this is a Stata 13 file the package readstata13 available from CRAN
>> may be of assistance. On 22/08/2016 18:40, Juan Ceccarelli Arias wrote: I
>> removed the data,frame=True... I obtain this warnings... Error in
>> read.dta(fuente[i]) : not a Stata version 5-12 .dta file In addition: There
>> were 50 or more warnings (use warnings() to see the first 50) the
>> warnings() throws this Warning messages: 1: In `levels<-`(`*tmp*`, value =
>> if (nl == nL) as.character(labels) else paste0(labels,  ... : duplicated
>> levels in factors are deprecated 2: In `levels<-`(`*tmp*`, value = if (nl
>> == nL) as.character(labels) else paste0(labels,  ... : duplicated levels in
>> factors are deprecated 3: In `levels<-`(`*tmp*`, value = if (nl == nL)
>> as.character(labels) else paste0(labels,  ... : duplicated levels in
>> factors are deprecated 4: In `levels<-`(`*tmp*`, value = if (nl == nL)
>> as.character(labels) else paste0(labels,  ... : duplicated levels in
>> factors are deprecated 5: In `levels<-`(`*tmp*`, value = if (nl == nL)
>> as.character(labels) else paste0(labels,  ... : duplicated levels in
>> factors are deprecated On Mon, Aug 22, 2016 at 2:32 PM,
>> > wrote: Hello, That argument
>> doesn't exist, hence the error. Read the help page ?read.dta more
>> carefully. You will see that already read.dta reads into a data.frame. Hope
>> this helps, Rui Barradas Citando Juan Ceccarelli Arias > >: Hi I need to apply some code over some stata files
>> that are in folder. I've wrote this library(foreign)
>> fuente=list.files("C:/Users/Jceccarelli/Bases/Stata", pattern="dta$",
>> full.names=FALSE) for (i in 1:length(fuente)){ xxx=read.dta(fuente[i],
>> to.data.frame=TRUE) } But i get this error 

Re: [R] Loop over folder files

2016-08-24 Thread ruipbarradas
Maybe it's better to open a new thread.

Rui Barradas
 

Citando Juan Ceccarelli Arias :

> The error wasn't in the loop. It was in the file list.
> It's running now because i added full.names option to TRUE
> fuente=list.files("C:/Users/Jceccarelli/Bases/Stata",  
> pattern="dta$", full.names=T)
> Now R can proccess the data. Now it callapses or stops because other  
> kind of error.
> ¿Should i open another topic or just use the mail list?
> By the way, thank you all.
>  
>   On Wed, Aug 24, 2016 at 11:48 AM,  wrote:
>> _Hello,
>>
>> That means that probably the files are in a different folder/directory.
>> Use getwd() to see what is your current directory and
>> setwd("path/to/files") to set the right place where the files can be found.
>>
>> Rui Barradas
>>  _
>>
>> _Citando Juan Ceccarelli Arias :_
>>
>>> _I just doesn't work..._
>>> _Im loading the read,dta13 package already._
>>> _When i try to perform a simple table(sex), i received the "File  
>>> not found" message._
>>> _However, if i load the data using the file.choose() option inside  
>>> read.dta13, i can open the stata file._
>>> _I don't know what am i doing wrong..._
>>> _  _ _On Tue, Aug 23, 2016 at 5:08 PM,  wrote: _
 __Or maybe a print() statement on the table() in the loop.

 print(table(...))

 Rui Barradas
  __

 __Citando David Winsemius :__

>> __On Aug 23, 2016, at 10:01 AM, Juan Ceccarelli Arias  
>>  wrote:
>>
>> Im running this but the code doesn't seem work.
>> It just hangs out but doesn't show any error.
>>
>> for (i in 1:length(fuente)){
>>
>> xxx=read_dta(fuente[i])
>>
>> table(xxx$cise, xxx$sexo)
>>
>> rm(xxx)
>>
>> }__
>
> __I still find the behavior of R's `for`-loop to be rather  
> puzzling. In this case you appear to be getting the operation  
> done, but because you didn't assign those table values to a  
> variable they just disappeared.
>
> Try this:
>
> XXX <- list()
>
> for (i in 1:length(fuente)){
>   xxx=read_dta(fuente[i])
>   XXX[[i]] <- table(xxx$cise, xxx$sexo)
>   rm(xxx)
> }
> str(XXX)
>
> Seems to me that if you can do assignment to the parent  
> environment (without actually using assign( ..., env=...)  that  
> you should also be able to see the results of evaluation  
> occurring inside the for loop, but for-loops return NULL. So you  
> see nothing.
>
> David.
>  _ _
>> __On Tue, Aug 23, 2016 at 6:31 AM,  wrote:__
>>
>>> __Hello,
>>>
>>> The op could also use package sos to find that and other  
>>> packages to read
>>> stata files.
>>>
>>> install.packages("sos")
>>>
>>> library(sos)
>>> findFn("stata")
>>> found 374 matches;  retrieving 19 pages
>>> 2 3 4 5 6 7 8 9 10
>>> 11 12 13 14 15 16 17 18 19
>>> Downloaded 258 links in 121 packages
>>>
>>> The first package is readstata13 but there are others.
>>>
>>> Hope this helps,
>>>
>>> Rui Barradas
>>>
>>> Citando Michael Dewey :
>>>
>>> Dear Juan
>>>
>>> If this is a Stata 13 file the package readstata13 available  
>>> from CRAN may
>>> be of assistance.
>>>
>>> On 22/08/2016 18:40, Juan Ceccarelli Arias wrote:
>>>
>>> I removed the data,frame=True...
>>> I obtain this warnings...
>>> Error in read.dta(fuente[i]) : not a Stata version 5-12 .dta file
>>> In addition: There were 50 or more warnings (use warnings() to see the
>>> first 50)
>>>
>>> the warnings() throws this
>>> Warning messages:
>>> 1: In `levels<-`(`*tmp*`, value = if (nl == nL)  
>>> as.character(labels) else
>>> paste0(labels,  ... :
>>> duplicated levels in factors are deprecated
>>> 2: In `levels<-`(`*tmp*`, value = if (nl == nL)  
>>> as.character(labels) else
>>> paste0(labels,  ... :
>>> duplicated levels in factors are deprecated
>>> 3: In `levels<-`(`*tmp*`, value = if (nl == nL)  
>>> as.character(labels) else
>>> paste0(labels,  ... :
>>> duplicated levels in factors are deprecated
>>> 4: In `levels<-`(`*tmp*`, value = if (nl == nL)  
>>> as.character(labels) else
>>> paste0(labels,  ... :
>>> duplicated levels in factors are deprecated
>>> 5: In `levels<-`(`*tmp*`, value = if (nl == nL)  
>>> as.character(labels) else
>>> paste0(labels,  ... :
>>> duplicated levels in factors are deprecated
>>>
>>> On Mon, Aug 22, 2016 at 2:32 PM,  wrote:
>>>
>>> Hello,
>>>
>>> That argument doesn't exist, hence the error.
>>> Read the help page ?read.dta more carefully. You will see that already
>>> read.dta reads into a 

Re: [R] Loop over folder files

2016-08-24 Thread Juan Ceccarelli Arias
I just doesn't work...
Im loading the read,dta13 package already.
When i try to perform a simple table(sex), i received the "File not found"
message.
However, if i load the data using the file.choose() option inside
read.dta13, i can open the stata file.
I don't know what am i doing wrong...

On Tue, Aug 23, 2016 at 5:08 PM,  wrote:

> Or maybe a print() statement on the table() in the loop.
>
> print(table(...))
>
> Rui Barradas
>
>
> Citando David Winsemius :
>
> On Aug 23, 2016, at 10:01 AM, Juan Ceccarelli Arias 
> wrote:
>
> Im running this but the code doesn't seem work.
> It just hangs out but doesn't show any error.
>
>
> for (i in 1:length(fuente)){
>
> xxx=read_dta(fuente[i])
>
> table(xxx$cise, xxx$sexo)
>
> rm(xxx)
>
> }
>
> I still find the behavior of R's `for`-loop to be rather puzzling. In this
> case you appear to be getting the operation done, but because you didn't
> assign those table values to a variable they just disappeared.
>
> Try this:
>
> XXX <- list()
>
> for (i in 1:length(fuente)){
>   xxx=read_dta(fuente[i])
>   XXX[[i]] <- table(xxx$cise, xxx$sexo)
>   rm(xxx)
> }
> str(XXX)
>
> Seems to me that if you can do assignment to the parent environment
> (without actually using assign( ..., env=...)  that you should also be able
> to see the results of evaluation occurring inside the for loop, but
> for-loops return NULL. So you see nothing.
>
> David.
>
>
> On Tue, Aug 23, 2016 at 6:31 AM,  wrote:
>
> Hello,
>
> The op could also use package sos to find that and other packages to read
> stata files.
>
> install.packages("sos")
>
> library(sos)
> findFn("stata")
> found 374 matches;  retrieving 19 pages
> 2 3 4 5 6 7 8 9 10
> 11 12 13 14 15 16 17 18 19
> Downloaded 258 links in 121 packages
>
>
> The first package is readstata13 but there are others.
>
> Hope this helps,
>
> Rui Barradas
>
>
>
> Citando Michael Dewey :
>
> Dear Juan
>
> If this is a Stata 13 file the package readstata13 available from CRAN may
> be of assistance.
>
> On 22/08/2016 18:40, Juan Ceccarelli Arias wrote:
>
> I removed the data,frame=True...
> I obtain this warnings...
> Error in read.dta(fuente[i]) : not a Stata version 5-12 .dta file
> In addition: There were 50 or more warnings (use warnings() to see the
> first 50)
>
> the warnings() throws this
> Warning messages:
> 1: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
> paste0(labels,  ... :
> duplicated levels in factors are deprecated
> 2: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
> paste0(labels,  ... :
> duplicated levels in factors are deprecated
> 3: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
> paste0(labels,  ... :
> duplicated levels in factors are deprecated
> 4: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
> paste0(labels,  ... :
> duplicated levels in factors are deprecated
> 5: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
> paste0(labels,  ... :
> duplicated levels in factors are deprecated
>
>
>
> On Mon, Aug 22, 2016 at 2:32 PM,  wrote:
>
> Hello,
>
> That argument doesn't exist, hence the error.
> Read the help page ?read.dta more carefully. You will see that already
> read.dta reads into a data.frame.
>
> Hope this helps,
>
> Rui Barradas
>
>
> Citando Juan Ceccarelli Arias :
>
> Hi
> I need to apply some code over some stata files that are in folder.
> I've wrote this
>
> library(foreign)
>
> fuente=list.files("C:/Users/Jceccarelli/Bases/Stata", pattern="dta$",
> full.names=FALSE)
>
> for (i in 1:length(fuente)){
>
> xxx=read.dta(fuente[i], to.data.frame=TRUE)
>
>
> }
>
> But i get this error
>
> Error in read.dta(fuente[i], to.data.frame = TRUE) :
> unused argument (to.data.frame = TRUE)
>
> What am i doing wrong?
>
>   [[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.htmland 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.
>
> --
> Michaelhttp://www.dewey.myzen.co.uk/home.html
>
>
>
>
> [[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/
> 

Re: [R] Loop over folder files

2016-08-24 Thread ruipbarradas
Hello,

That means that probably the files are in a different folder/directory.
Use getwd() to see what is your current directory and
setwd("path/to/files") to set the right place where the files can be found.

Rui Barradas
 

Citando Juan Ceccarelli Arias :

> I just doesn't work...
> Im loading the read,dta13 package already.
> When i try to perform a simple table(sex), i received the "File not  
> found" message.
> However, if i load the data using the file.choose() option inside  
> read.dta13, i can open the stata file.
> I don't know what am i doing wrong...
>   On Tue, Aug 23, 2016 at 5:08 PM,  wrote:
>> _Or maybe a print() statement on the table() in the loop.
>>
>> print(table(...))
>>
>> Rui Barradas
>>  _
>>
>> _Citando David Winsemius :_
>>
 _On Aug 23, 2016, at 10:01 AM, Juan Ceccarelli Arias  
  wrote:

 Im running this but the code doesn't seem work.
 It just hangs out but doesn't show any error.

 for (i in 1:length(fuente)){

 xxx=read_dta(fuente[i])

 table(xxx$cise, xxx$sexo)

 rm(xxx)

 }_
>>>
>>> _I still find the behavior of R's `for`-loop to be rather  
>>> puzzling. In this case you appear to be getting the operation  
>>> done, but because you didn't assign those table values to a  
>>> variable they just disappeared.
>>>
>>> Try this:
>>>
>>> XXX <- list()
>>>
>>> for (i in 1:length(fuente)){
>>>   xxx=read_dta(fuente[i])
>>>   XXX[[i]] <- table(xxx$cise, xxx$sexo)
>>>   rm(xxx)
>>> }
>>> str(XXX)
>>>
>>> Seems to me that if you can do assignment to the parent  
>>> environment (without actually using assign( ..., env=...)  that  
>>> you should also be able to see the results of evaluation occurring  
>>> inside the for loop, but for-loops return NULL. So you see nothing.
>>>
>>> David.
>>>   _
 _On Tue, Aug 23, 2016 at 6:31 AM,  wrote:_

> _Hello,
>
> The op could also use package sos to find that and other packages to read
> stata files.
>
> install.packages("sos")
>
> library(sos)
> findFn("stata")
> found 374 matches;  retrieving 19 pages
> 2 3 4 5 6 7 8 9 10
> 11 12 13 14 15 16 17 18 19
> Downloaded 258 links in 121 packages
>
> The first package is readstata13 but there are others.
>
> Hope this helps,
>
> Rui Barradas
>
> Citando Michael Dewey :
>
> Dear Juan
>
> If this is a Stata 13 file the package readstata13 available  
> from CRAN may
> be of assistance.
>
> On 22/08/2016 18:40, Juan Ceccarelli Arias wrote:
>
> I removed the data,frame=True...
> I obtain this warnings...
> Error in read.dta(fuente[i]) : not a Stata version 5-12 .dta file
> In addition: There were 50 or more warnings (use warnings() to see the
> first 50)
>
> the warnings() throws this
> Warning messages:
> 1: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
> paste0(labels,  ... :
> duplicated levels in factors are deprecated
> 2: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
> paste0(labels,  ... :
> duplicated levels in factors are deprecated
> 3: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
> paste0(labels,  ... :
> duplicated levels in factors are deprecated
> 4: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
> paste0(labels,  ... :
> duplicated levels in factors are deprecated
> 5: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
> paste0(labels,  ... :
> duplicated levels in factors are deprecated
>
> On Mon, Aug 22, 2016 at 2:32 PM,  wrote:
>
> Hello,
>
> That argument doesn't exist, hence the error.
> Read the help page ?read.dta more carefully. You will see that already
> read.dta reads into a data.frame.
>
> Hope this helps,
>
> Rui Barradas
>
> Citando Juan Ceccarelli Arias :
>
> Hi
> I need to apply some code over some stata files that are in folder.
> I've wrote this
>
> library(foreign)
>
> fuente=list.files("C:/Users/Jceccarelli/Bases/Stata[1]", pattern="dta$",
> full.names=FALSE)
>
> for (i in 1:length(fuente)){
>
> xxx=read.dta(fuente[i], to.data.frame=TRUE)
>
> }
>
> But i get this error
>
> Error in read.dta(fuente[i], to.data.frame = TRUE) :
> unused argument (to.data.frame = TRUE)
>
> What am i doing wrong?
>
>       [[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 

Re: [R] Loop over folder files

2016-08-23 Thread ruipbarradas
Or maybe a print() statement on the table() in the loop.

print(table(...))

Rui Barradas
 

Citando David Winsemius :

>> On Aug 23, 2016, at 10:01 AM, Juan Ceccarelli Arias  
>>  wrote:
>>
>> Im running this but the code doesn't seem work.
>> It just hangs out but doesn't show any error.
>>
>> for (i in 1:length(fuente)){
>>
>> xxx=read_dta(fuente[i])
>>
>> table(xxx$cise, xxx$sexo)
>>
>> rm(xxx)
>>
>> }
>
> I still find the behavior of R's `for`-loop to be rather puzzling.  
> In this case you appear to be getting the operation done, but  
> because you didn't assign those table values to a variable they just  
> disappeared.
>
> Try this:
>
> XXX <- list()
>
> for (i in 1:length(fuente)){
>   xxx=read_dta(fuente[i])
>   XXX[[i]] <- table(xxx$cise, xxx$sexo)
>   rm(xxx)
> }
> str(XXX)
>
> Seems to me that if you can do assignment to the parent environment  
> (without actually using assign( ..., env=...)  that you should also  
> be able to see the results of evaluation occurring inside the for  
> loop, but for-loops return NULL. So you see nothing.
>
> David.
>  
>> On Tue, Aug 23, 2016 at 6:31 AM,  wrote:
>>> Hello,
>>>
>>> The op could also use package sos to find that and other packages to read
>>> stata files.
>>>
>>> install.packages("sos")
>>>
>>> library(sos)
>>> findFn("stata")
>>> found 374 matches;  retrieving 19 pages
>>> 2 3 4 5 6 7 8 9 10
>>> 11 12 13 14 15 16 17 18 19
>>> Downloaded 258 links in 121 packages
>>>
>>> The first package is readstata13 but there are others.
>>>
>>> Hope this helps,
>>>
>>> Rui Barradas
>>>
>>> Citando Michael Dewey :
>>>
>>> Dear Juan
>>>
>>> If this is a Stata 13 file the package readstata13 available from CRAN may
>>> be of assistance.
>>>
>>> On 22/08/2016 18:40, Juan Ceccarelli Arias wrote:
>>>
>>> I removed the data,frame=True...
>>> I obtain this warnings...
>>> Error in read.dta(fuente[i]) : not a Stata version 5-12 .dta file
>>> In addition: There were 50 or more warnings (use warnings() to see the
>>> first 50)
>>>
>>> the warnings() throws this
>>> Warning messages:
>>> 1: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
>>> paste0(labels,  ... :
>>> duplicated levels in factors are deprecated
>>> 2: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
>>> paste0(labels,  ... :
>>> duplicated levels in factors are deprecated
>>> 3: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
>>> paste0(labels,  ... :
>>> duplicated levels in factors are deprecated
>>> 4: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
>>> paste0(labels,  ... :
>>> duplicated levels in factors are deprecated
>>> 5: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
>>> paste0(labels,  ... :
>>> duplicated levels in factors are deprecated
>>>
>>> On Mon, Aug 22, 2016 at 2:32 PM,  wrote:
>>>
>>> Hello,
>>>
>>> That argument doesn't exist, hence the error.
>>> Read the help page ?read.dta more carefully. You will see that already
>>> read.dta reads into a data.frame.
>>>
>>> Hope this helps,
>>>
>>> Rui Barradas
>>>
>>> Citando Juan Ceccarelli Arias :
>>>
>>> Hi
>>> I need to apply some code over some stata files that are in folder.
>>> I've wrote this
>>>
>>> library(foreign)
>>>
>>> fuente=list.files("C:/Users/Jceccarelli/Bases/Stata", pattern="dta$",
>>> full.names=FALSE)
>>>
>>> for (i in 1:length(fuente)){
>>>
>>> xxx=read.dta(fuente[i], to.data.frame=TRUE)
>>>
>>> }
>>>
>>> But i get this error
>>>
>>> Error in read.dta(fuente[i], to.data.frame = TRUE) :
>>> unused argument (to.data.frame = TRUE)
>>>
>>> What am i doing wrong?
>>>
>>>       [[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.htmland 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.
>>>
>>> --
>>> Michaelhttp://www.dewey.myzen.co.uk/home.html
>>>
>>>  
>>
>> [[alternative HTML version deleted]]
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> David WinsemiusAlameda, CA, USA

 


Re: [R] Loop over folder files

2016-08-23 Thread MacQueen, Don
Compare what happens with these two command:

for (i in 1:3) { table(letters[1:4])  }
for (i in 1:3) { print(table(letters[1:4]))  }

Then try modifying your loop similarly.


-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 8/23/16, 10:01 AM, "R-help on behalf of Juan Ceccarelli Arias"
 wrote:

>Im running this but the code doesn't seem work.
>It just hangs out but doesn't show any error.
>
>
>for (i in 1:length(fuente)){
>
>xxx=read_dta(fuente[i])
>
>table(xxx$cise, xxx$sexo)
>
>rm(xxx)
>
>}
>
>On Tue, Aug 23, 2016 at 6:31 AM,  wrote:
>
>> Hello,
>>
>> The op could also use package sos to find that and other packages to
>>read
>> stata files.
>>
>> install.packages("sos")
>>
>> library(sos)
>> findFn("stata")
>> found 374 matches;  retrieving 19 pages
>> 2 3 4 5 6 7 8 9 10
>> 11 12 13 14 15 16 17 18 19
>> Downloaded 258 links in 121 packages
>>
>>
>> The first package is readstata13 but there are others.
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>>
>>
>> Citando Michael Dewey :
>>
>> Dear Juan
>>
>> If this is a Stata 13 file the package readstata13 available from CRAN
>>may
>> be of assistance.
>>
>> On 22/08/2016 18:40, Juan Ceccarelli Arias wrote:
>>
>> I removed the data,frame=True...
>> I obtain this warnings...
>> Error in read.dta(fuente[i]) : not a Stata version 5-12 .dta file
>> In addition: There were 50 or more warnings (use warnings() to see the
>> first 50)
>>
>> the warnings() throws this
>> Warning messages:
>> 1: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels)
>>else
>> paste0(labels,  ... :
>> duplicated levels in factors are deprecated
>> 2: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels)
>>else
>> paste0(labels,  ... :
>> duplicated levels in factors are deprecated
>> 3: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels)
>>else
>> paste0(labels,  ... :
>> duplicated levels in factors are deprecated
>> 4: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels)
>>else
>> paste0(labels,  ... :
>> duplicated levels in factors are deprecated
>> 5: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels)
>>else
>> paste0(labels,  ... :
>> duplicated levels in factors are deprecated
>>
>>
>>
>> On Mon, Aug 22, 2016 at 2:32 PM,  wrote:
>>
>> Hello,
>>
>> That argument doesn't exist, hence the error.
>> Read the help page ?read.dta more carefully. You will see that already
>> read.dta reads into a data.frame.
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>>
>> Citando Juan Ceccarelli Arias :
>>
>> Hi
>> I need to apply some code over some stata files that are in folder.
>> I've wrote this
>>
>> library(foreign)
>>
>> fuente=list.files("C:/Users/Jceccarelli/Bases/Stata", pattern="dta$",
>> full.names=FALSE)
>>
>> for (i in 1:length(fuente)){
>>
>> xxx=read.dta(fuente[i], to.data.frame=TRUE)
>>
>>
>> }
>>
>> But i get this error
>>
>> Error in read.dta(fuente[i], to.data.frame = TRUE) :
>> unused argument (to.data.frame = TRUE)
>>
>> What am i doing wrong?
>>
>>[[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.htmland 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.
>>
>> --
>> Michaelhttp://www.dewey.myzen.co.uk/home.html
>>
>>
>>
>>
>
>   [[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] Loop over folder files

2016-08-23 Thread ruipbarradas
Hello,

Where does read_dta come from? You should also post the library() instruction.
Try to run the code without the loop, with just one file and inspect  
xxx to see what's happening.

xxx <- read_dta(fuente[1])
str(xxx)
table(xxx$cise, xxx$sexo)

Rui Barradas
 

Citando Juan Ceccarelli Arias :

> Im running this but the code doesn't seem work.
> It just hangs out but doesn't show any error.
>  
> for (i in 1:length(fuente)){
>  
> xxx=read_dta(fuente[i])
>
> table(xxx$cise, xxx$sexo)
>  
> rm(xxx)
>  
> }
>
>   On Tue, Aug 23, 2016 at 6:31 AM,  wrote:
>> _Hello,
>>
>> The op could also use package sos to find that and other packages  
>> to read stata files.
>>
>> install.packages("sos")
>>
>> library(sos)
>> findFn("stata")
>> found 374 matches;  retrieving 19 pages
>> 2 3 4 5 6 7 8 9 10
>> 11 12 13 14 15 16 17 18 19
>> Downloaded 258 links in 121 packages
>>
>> The first package is readstata13 but there are others.
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>>  _
>>
>> _Citando Michael Dewey :_
>>
>>> _Dear Juan
>>>
>>> If this is a Stata 13 file the package readstata13 available from  
>>> CRAN may be of assistance.
>>>
>>> On 22/08/2016 18:40, Juan Ceccarelli Arias wrote:_
>>>
 _I removed the data,frame=True...
 I obtain this warnings...
 Error in read.dta(fuente[i]) : not a Stata version 5-12 .dta file
 In addition: There were 50 or more warnings (use warnings() to see the
 first 50)

 the warnings() throws this
 Warning messages:
 1: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
 paste0(labels,  ... :
 duplicated levels in factors are deprecated
 2: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
 paste0(labels,  ... :
 duplicated levels in factors are deprecated
 3: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
 paste0(labels,  ... :
 duplicated levels in factors are deprecated
 4: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
 paste0(labels,  ... :
 duplicated levels in factors are deprecated
 5: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
 paste0(labels,  ... :
 duplicated levels in factors are deprecated

 On Mon, Aug 22, 2016 at 2:32 PM,  wrote:_

> _Hello,
>
> That argument doesn't exist, hence the error.
> Read the help page ?read.dta more carefully. You will see that already
> read.dta reads into a data.frame.
>
> Hope this helps,
>
> Rui Barradas
>
> Citando Juan Ceccarelli Arias :
>
> Hi
> I need to apply some code over some stata files that are in folder.
> I've wrote this
>
> library(foreign)
>
> fuente=list.files("C:/Users/Jceccarelli/Bases/Stata[1]", pattern="dta$",
> full.names=FALSE)
>
> for (i in 1:length(fuente)){
>
> xxx=read.dta(fuente[i], to.data.frame=TRUE)
>
> }
>
> But i get this error
>
> Error in read.dta(fuente[i], to.data.frame = TRUE) :
> unused argument (to.data.frame = TRUE)
>
> What am i doing wrong?
>
>        [[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.htmland 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._
>>>
>>> _--
>>> Michaelhttp://www.dewey.myzen.co.uk/home.html[1]_
>>
>> _ _

 

Ligações:
-
[1]

[[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] Loop over folder files

2016-08-23 Thread David Winsemius

> On Aug 23, 2016, at 10:01 AM, Juan Ceccarelli Arias  wrote:
> 
> Im running this but the code doesn't seem work.
> It just hangs out but doesn't show any error.
> 
> 
> for (i in 1:length(fuente)){
> 
> xxx=read_dta(fuente[i])
> 
> table(xxx$cise, xxx$sexo)
> 
> rm(xxx)
> 
> }

I still find the behavior of R's `for`-loop to be rather puzzling. In this case 
you appear to be getting the operation done, but because you didn't assign 
those table values to a variable they just disappeared.

Try this:

XXX <- list()

for (i in 1:length(fuente)){
   xxx=read_dta(fuente[i])
   XXX[[i]] <- table(xxx$cise, xxx$sexo)
   rm(xxx)
}
str(XXX)

Seems to me that if you can do assignment to the parent environment (without 
actually using assign( ..., env=...)  that you should also be able to see the 
results of evaluation occurring inside the for loop, but for-loops return NULL. 
So you see nothing.

David.


> 
> On Tue, Aug 23, 2016 at 6:31 AM,  wrote:
> 
>> Hello,
>> 
>> The op could also use package sos to find that and other packages to read
>> stata files.
>> 
>> install.packages("sos")
>> 
>> library(sos)
>> findFn("stata")
>> found 374 matches;  retrieving 19 pages
>> 2 3 4 5 6 7 8 9 10
>> 11 12 13 14 15 16 17 18 19
>> Downloaded 258 links in 121 packages
>> 
>> 
>> The first package is readstata13 but there are others.
>> 
>> Hope this helps,
>> 
>> Rui Barradas
>> 
>> 
>> 
>> Citando Michael Dewey :
>> 
>> Dear Juan
>> 
>> If this is a Stata 13 file the package readstata13 available from CRAN may
>> be of assistance.
>> 
>> On 22/08/2016 18:40, Juan Ceccarelli Arias wrote:
>> 
>> I removed the data,frame=True...
>> I obtain this warnings...
>> Error in read.dta(fuente[i]) : not a Stata version 5-12 .dta file
>> In addition: There were 50 or more warnings (use warnings() to see the
>> first 50)
>> 
>> the warnings() throws this
>> Warning messages:
>> 1: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
>> paste0(labels,  ... :
>> duplicated levels in factors are deprecated
>> 2: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
>> paste0(labels,  ... :
>> duplicated levels in factors are deprecated
>> 3: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
>> paste0(labels,  ... :
>> duplicated levels in factors are deprecated
>> 4: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
>> paste0(labels,  ... :
>> duplicated levels in factors are deprecated
>> 5: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
>> paste0(labels,  ... :
>> duplicated levels in factors are deprecated
>> 
>> 
>> 
>> On Mon, Aug 22, 2016 at 2:32 PM,  wrote:
>> 
>> Hello,
>> 
>> That argument doesn't exist, hence the error.
>> Read the help page ?read.dta more carefully. You will see that already
>> read.dta reads into a data.frame.
>> 
>> Hope this helps,
>> 
>> Rui Barradas
>> 
>> 
>> Citando Juan Ceccarelli Arias :
>> 
>> Hi
>> I need to apply some code over some stata files that are in folder.
>> I've wrote this
>> 
>> library(foreign)
>> 
>> fuente=list.files("C:/Users/Jceccarelli/Bases/Stata", pattern="dta$",
>> full.names=FALSE)
>> 
>> for (i in 1:length(fuente)){
>> 
>> xxx=read.dta(fuente[i], to.data.frame=TRUE)
>> 
>> 
>> }
>> 
>> But i get this error
>> 
>> Error in read.dta(fuente[i], to.data.frame = TRUE) :
>> unused argument (to.data.frame = TRUE)
>> 
>> What am i doing wrong?
>> 
>>   [[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.htmland 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.
>> 
>> --
>> Michaelhttp://www.dewey.myzen.co.uk/home.html
>> 
>> 
>> 
>> 
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA

__
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 

Re: [R] Loop over folder files

2016-08-23 Thread Juan Ceccarelli Arias
Im running this but the code doesn't seem work.
It just hangs out but doesn't show any error.


for (i in 1:length(fuente)){

xxx=read_dta(fuente[i])

table(xxx$cise, xxx$sexo)

rm(xxx)

}

On Tue, Aug 23, 2016 at 6:31 AM,  wrote:

> Hello,
>
> The op could also use package sos to find that and other packages to read
> stata files.
>
> install.packages("sos")
>
> library(sos)
> findFn("stata")
> found 374 matches;  retrieving 19 pages
> 2 3 4 5 6 7 8 9 10
> 11 12 13 14 15 16 17 18 19
> Downloaded 258 links in 121 packages
>
>
> The first package is readstata13 but there are others.
>
> Hope this helps,
>
> Rui Barradas
>
>
>
> Citando Michael Dewey :
>
> Dear Juan
>
> If this is a Stata 13 file the package readstata13 available from CRAN may
> be of assistance.
>
> On 22/08/2016 18:40, Juan Ceccarelli Arias wrote:
>
> I removed the data,frame=True...
> I obtain this warnings...
> Error in read.dta(fuente[i]) : not a Stata version 5-12 .dta file
> In addition: There were 50 or more warnings (use warnings() to see the
> first 50)
>
> the warnings() throws this
> Warning messages:
> 1: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
> paste0(labels,  ... :
> duplicated levels in factors are deprecated
> 2: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
> paste0(labels,  ... :
> duplicated levels in factors are deprecated
> 3: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
> paste0(labels,  ... :
> duplicated levels in factors are deprecated
> 4: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
> paste0(labels,  ... :
> duplicated levels in factors are deprecated
> 5: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
> paste0(labels,  ... :
> duplicated levels in factors are deprecated
>
>
>
> On Mon, Aug 22, 2016 at 2:32 PM,  wrote:
>
> Hello,
>
> That argument doesn't exist, hence the error.
> Read the help page ?read.dta more carefully. You will see that already
> read.dta reads into a data.frame.
>
> Hope this helps,
>
> Rui Barradas
>
>
> Citando Juan Ceccarelli Arias :
>
> Hi
> I need to apply some code over some stata files that are in folder.
> I've wrote this
>
> library(foreign)
>
> fuente=list.files("C:/Users/Jceccarelli/Bases/Stata", pattern="dta$",
> full.names=FALSE)
>
> for (i in 1:length(fuente)){
>
> xxx=read.dta(fuente[i], to.data.frame=TRUE)
>
>
> }
>
> But i get this error
>
> Error in read.dta(fuente[i], to.data.frame = TRUE) :
> unused argument (to.data.frame = TRUE)
>
> What am i doing wrong?
>
>[[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.htmland 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.
>
> --
> Michaelhttp://www.dewey.myzen.co.uk/home.html
>
>
>
>

[[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] Loop over folder files

2016-08-23 Thread ruipbarradas
Hello,

The op could also use package sos to find that and other packages to  
read stata files.

install.packages("sos")

library(sos)
findFn("stata")
found 374 matches;  retrieving 19 pages
2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19
Downloaded 258 links in 121 packages

The first package is readstata13 but there are others.

Hope this helps,

Rui Barradas

 

Citando Michael Dewey :

> Dear Juan
>
> If this is a Stata 13 file the package readstata13 available from  
> CRAN may be of assistance.
>
> On 22/08/2016 18:40, Juan Ceccarelli Arias wrote:
>> I removed the data,frame=True...
>> I obtain this warnings...
>> Error in read.dta(fuente[i]) : not a Stata version 5-12 .dta file
>> In addition: There were 50 or more warnings (use warnings() to see the
>> first 50)
>>
>> the warnings() throws this
>> Warning messages:
>> 1: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
>> paste0(labels,  ... :
>> duplicated levels in factors are deprecated
>> 2: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
>> paste0(labels,  ... :
>> duplicated levels in factors are deprecated
>> 3: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
>> paste0(labels,  ... :
>> duplicated levels in factors are deprecated
>> 4: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
>> paste0(labels,  ... :
>> duplicated levels in factors are deprecated
>> 5: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
>> paste0(labels,  ... :
>> duplicated levels in factors are deprecated
>>
>> On Mon, Aug 22, 2016 at 2:32 PM,  wrote:
>>> Hello,
>>>
>>> That argument doesn't exist, hence the error.
>>> Read the help page ?read.dta more carefully. You will see that already
>>> read.dta reads into a data.frame.
>>>
>>> Hope this helps,
>>>
>>> Rui Barradas
>>>
>>> Citando Juan Ceccarelli Arias :
>>>
>>> Hi
>>> I need to apply some code over some stata files that are in folder.
>>> I've wrote this
>>>
>>> library(foreign)
>>>
>>> fuente=list.files("C:/Users/Jceccarelli/Bases/Stata", pattern="dta$",
>>> full.names=FALSE)
>>>
>>> for (i in 1:length(fuente)){
>>>
>>> xxx=read.dta(fuente[i], to.data.frame=TRUE)
>>>
>>> }
>>>
>>> But i get this error
>>>
>>> Error in read.dta(fuente[i], to.data.frame = TRUE) :
>>> unused argument (to.data.frame = TRUE)
>>>
>>> What am i doing wrong?
>>>
>>>        [[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.htmland 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.
>
> --
> Michaelhttp://www.dewey.myzen.co.uk/home.html

 

[[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] Loop over folder files

2016-08-23 Thread Michael Dewey

Dear Juan

If this is a Stata 13 file the package readstata13 available from CRAN 
may be of assistance.


On 22/08/2016 18:40, Juan Ceccarelli Arias wrote:

I removed the data,frame=True...
I obtain this warnings...
Error in read.dta(fuente[i]) : not a Stata version 5-12 .dta file
In addition: There were 50 or more warnings (use warnings() to see the
first 50)

the warnings() throws this
Warning messages:
1: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
paste0(labels,  ... :
  duplicated levels in factors are deprecated
2: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
paste0(labels,  ... :
  duplicated levels in factors are deprecated
3: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
paste0(labels,  ... :
  duplicated levels in factors are deprecated
4: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
paste0(labels,  ... :
  duplicated levels in factors are deprecated
5: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
paste0(labels,  ... :
  duplicated levels in factors are deprecated



On Mon, Aug 22, 2016 at 2:32 PM,  wrote:


Hello,

That argument doesn't exist, hence the error.
Read the help page ?read.dta more carefully. You will see that already
read.dta reads into a data.frame.

Hope this helps,

Rui Barradas


Citando Juan Ceccarelli Arias :

Hi
I need to apply some code over some stata files that are in folder.
I've wrote this

library(foreign)

fuente=list.files("C:/Users/Jceccarelli/Bases/Stata", pattern="dta$",
full.names=FALSE)

for (i in 1:length(fuente)){

xxx=read.dta(fuente[i], to.data.frame=TRUE)


}

But i get this error

Error in read.dta(fuente[i], to.data.frame = TRUE) :
unused argument (to.data.frame = TRUE)

What am i doing wrong?

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



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

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


Re: [R] Loop over folder files

2016-08-22 Thread David Winsemius

> On Aug 22, 2016, at 10:40 AM, Juan Ceccarelli Arias  wrote:
> 
> I removed the data,frame=True...
> I obtain this warnings...
> Error in read.dta(fuente[i]) : not a Stata version 5-12 .dta file

Well, that seems fairly self-explanatory. What version of Stata are you using 
and does it have capacity to write to one of hte versions that are supported by 
read.dta?

-- 
David.


> In addition: There were 50 or more warnings (use warnings() to see the
> first 50)
> 
> the warnings() throws this
> Warning messages:
> 1: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
> paste0(labels,  ... :
>  duplicated levels in factors are deprecated
> 2: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
> paste0(labels,  ... :
>  duplicated levels in factors are deprecated
> 3: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
> paste0(labels,  ... :
>  duplicated levels in factors are deprecated
> 4: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
> paste0(labels,  ... :
>  duplicated levels in factors are deprecated
> 5: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
> paste0(labels,  ... :
>  duplicated levels in factors are deprecated
> 
> 
> 
> On Mon, Aug 22, 2016 at 2:32 PM,  wrote:
> 
>> Hello,
>> 
>> That argument doesn't exist, hence the error.
>> Read the help page ?read.dta more carefully. You will see that already
>> read.dta reads into a data.frame.
>> 
>> Hope this helps,
>> 
>> Rui Barradas
>> 
>> 
>> Citando Juan Ceccarelli Arias :
>> 
>> Hi
>> I need to apply some code over some stata files that are in folder.
>> I've wrote this
>> 
>> library(foreign)
>> 
>> fuente=list.files("C:/Users/Jceccarelli/Bases/Stata", pattern="dta$",
>> full.names=FALSE)
>> 
>> for (i in 1:length(fuente)){
>> 
>> xxx=read.dta(fuente[i], to.data.frame=TRUE)
>> 
>> 
>> }
>> 
>> But i get this error
>> 
>> Error in read.dta(fuente[i], to.data.frame = TRUE) :
>> unused argument (to.data.frame = TRUE)
>> 
>> What am i doing wrong?
>> 
>>[[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.htmland 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.

David Winsemius
Alameda, CA, USA

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


Re: [R] Loop over folder files

2016-08-22 Thread Juan Ceccarelli Arias
I removed the data,frame=True...
I obtain this warnings...
Error in read.dta(fuente[i]) : not a Stata version 5-12 .dta file
In addition: There were 50 or more warnings (use warnings() to see the
first 50)

the warnings() throws this
Warning messages:
1: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
paste0(labels,  ... :
  duplicated levels in factors are deprecated
2: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
paste0(labels,  ... :
  duplicated levels in factors are deprecated
3: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
paste0(labels,  ... :
  duplicated levels in factors are deprecated
4: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
paste0(labels,  ... :
  duplicated levels in factors are deprecated
5: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else
paste0(labels,  ... :
  duplicated levels in factors are deprecated



On Mon, Aug 22, 2016 at 2:32 PM,  wrote:

> Hello,
>
> That argument doesn't exist, hence the error.
> Read the help page ?read.dta more carefully. You will see that already
> read.dta reads into a data.frame.
>
> Hope this helps,
>
> Rui Barradas
>
>
> Citando Juan Ceccarelli Arias :
>
> Hi
> I need to apply some code over some stata files that are in folder.
> I've wrote this
>
> library(foreign)
>
> fuente=list.files("C:/Users/Jceccarelli/Bases/Stata", pattern="dta$",
> full.names=FALSE)
>
> for (i in 1:length(fuente)){
>
> xxx=read.dta(fuente[i], to.data.frame=TRUE)
>
>
> }
>
> But i get this error
>
> Error in read.dta(fuente[i], to.data.frame = TRUE) :
> unused argument (to.data.frame = TRUE)
>
> What am i doing wrong?
>
> [[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.htmland 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] Loop over folder files

2016-08-22 Thread ruipbarradas
Hello,

That argument doesn't exist, hence the error.
Read the help page ?read.dta more carefully. You will see that already  
read.dta reads into a data.frame.

Hope this helps,

Rui Barradas
 

Citando Juan Ceccarelli Arias :

> Hi
> I need to apply some code over some stata files that are in folder.
> I've wrote this
>
> library(foreign)
>
> fuente=list.files("C:/Users/Jceccarelli/Bases/Stata", pattern="dta$",
> full.names=FALSE)
>
> for (i in 1:length(fuente)){
>
> xxx=read.dta(fuente[i], to.data.frame=TRUE)
>
> }
>
> But i get this error
>
> Error in read.dta(fuente[i], to.data.frame = TRUE) :
> unused argument (to.data.frame = TRUE)
>
> What am i doing wrong?
>
>         [[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.htmland 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] loop function

2016-08-18 Thread Adams, Jean
You seemed to have re-written over the "b" object in your code.
This might work for you.

library(MASS)
for (i in 58:1){
  for(j in 58:i){
str.temp <- paste("y1 ~ x", i, "* x", j, sep = "")
univar <- glm.nb(as.formula(str.temp), data=df)
b <- summary(univar)$coeffients[4, 4]
if(b < 0.6){
  cat(i, j, b, "\n")
}
  }
}

By the way, you should post using plain text (not html).

Jean


On Thu, Aug 18, 2016 at 6:48 AM, fatimah soleimany <
fatimah.soleim...@gmail.com> wrote:

> Hi Dear users,
> for an interactive use, i am trying to write a loop that looks for all
> variables in the conditions which i introduced, this is what I'm trying:
>
> library(MASS)> i=1> for (i in 58:1){+ for(j in 58:i){+ + str.temp <-
> paste("y1 ~ x", i, "* x", j, sep = "")+
> univar<-glm.nb(as.formula(str.temp), data=df)+
> b=summary(univar)$coeffients[4,4]+ b<-c(i,j)+ if(b < 0.6){ +
> print(b)+}+   }+   }
>
> i have no error but i didn't get true response too, my result is this:
>
>
> b[1] 1 1
>
>  but i want all the variables that their p-value are less than 0.6, i
> think i couldn't write a true loop,can you help me?
>
> thank you in advance for any help
>
> [[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] loop testing unidentified columns

2016-06-20 Thread Brittany Demmitt
Thank you!

> On Jun 20, 2016, at 12:41 PM, David L Carlson  wrote:
> 
> It does not test the first column, but a vector must have consecutive 
> indices. Since you did not assign a value, R inserts a missing value. If you 
> don't want to see it use
> 
>> results.pc.all[, -1]
>  [,1] [,2]
> results.212
> results.323
> 
> -
> David L Carlson
> Department of Anthropology
> Texas A University
> College Station, TX 77840-4352
> 
> -Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Brittany 
> Demmitt
> Sent: Monday, June 20, 2016 12:15 PM
> To: r-help@r-project.org
> Subject: [R] loop testing unidentified columns
> 
> Hello,
> 
> I want to compare all of the columns of one data frame to another to see if 
> any of the columns are equivalent to one another. The first column in both of 
> my data frames are the sample IDs and do not need to be compared. Below is an 
> example of the loop I am using to compare the two data frames that counts the 
> number of equivalent values there between two columns. So in this example the 
> value of 3 means that all three observations for the two columns being 
> compared were equivalent. The loop works fine but I do not understand why it 
> tests the first column of the sample IDs providing “NA” for the sum of 
> matching when my loop is specifying to only test columns 2-3.  
> 
> Thank you!
> 
> 
> #create dataframe A 
> A = matrix(c("a",3,4,"b",5,7,"c",3,7),nrow=3, ncol=3,byrow = TRUE)
> A <- as.data.frame(A)
> A$V2 <- as.numeric(A$V2)
> A$V3 <- as.numeric(A$V3)
> str(A)
> 
> #create dataframe B
> B = matrix(c("a",1,1,"b",6,2,"c",2,2),nrow=3, ncol=3,byrow = TRUE)
> B <- as.data.frame(B)
> B$V2 <- as.numeric(B$V2)
> B$V3 <- as.numeric(B$V3)
> str(B)
> 
> results.2 <- numeric()
> results.3  <- numeric()
> 
> 
> #compare columns to identify those that are identical in the two dataframes 
> for(i in 2:3){
>  results.2[i] <- sum(A[,2]==B[,i])
>  results.3[i] <- sum(A[,3]==B[,i])
>  results.pc.all <- rbind(results.2,results.3)
> }
> results.pc.all
> 
> __
> 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] loop testing unidentified columns

2016-06-20 Thread David L Carlson
It does not test the first column, but a vector must have consecutive indices. 
Since you did not assign a value, R inserts a missing value. If you don't want 
to see it use

> results.pc.all[, -1]
  [,1] [,2]
results.212
results.323

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

-Original Message-
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Brittany Demmitt
Sent: Monday, June 20, 2016 12:15 PM
To: r-help@r-project.org
Subject: [R] loop testing unidentified columns

Hello,

I want to compare all of the columns of one data frame to another to see if any 
of the columns are equivalent to one another. The first column in both of my 
data frames are the sample IDs and do not need to be compared. Below is an 
example of the loop I am using to compare the two data frames that counts the 
number of equivalent values there between two columns. So in this example the 
value of 3 means that all three observations for the two columns being compared 
were equivalent. The loop works fine but I do not understand why it tests the 
first column of the sample IDs providing “NA” for the sum of matching when my 
loop is specifying to only test columns 2-3.  

Thank you!


#create dataframe A 
A = matrix(c("a",3,4,"b",5,7,"c",3,7),nrow=3, ncol=3,byrow = TRUE)
A <- as.data.frame(A)
A$V2 <- as.numeric(A$V2)
A$V3 <- as.numeric(A$V3)
str(A)

#create dataframe B
B = matrix(c("a",1,1,"b",6,2,"c",2,2),nrow=3, ncol=3,byrow = TRUE)
B <- as.data.frame(B)
B$V2 <- as.numeric(B$V2)
B$V3 <- as.numeric(B$V3)
str(B)

results.2 <- numeric()
results.3  <- numeric()


#compare columns to identify those that are identical in the two dataframes 
for(i in 2:3){
  results.2[i] <- sum(A[,2]==B[,i])
  results.3[i] <- sum(A[,3]==B[,i])
  results.pc.all <- rbind(results.2,results.3)
}
results.pc.all

__
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] Loop Help

2016-02-29 Thread David Winsemius

> On Feb 29, 2016, at 6:24 AM, Fernando McRayearth  wrote:
> 
> Need to create ascii maps for 10 species by writing a loop. So i have to have 
> the vectors ready in the Global Environment, and the "raster map" so the 
> information can be added. 
> 
> when writing the loop I am using the "paste" function because the only thing 
> that changes in the vector is the name of the specie ( 
> ens_KAPPA_F45_Ambystoma.altamirani, ens_KAPPA_F45_Ambystoma.lermaense), now 
> the issue is that the result returns it like this "Ambystoma.lermaense" , and 
> i don't know how to bring the vector without the "". any suggestions? thanks. 
> 
> 
> CURRENT CODE NOT WORKING 
> for (i in 1:length(amph_nome)){
> orden=match(testo,0:93831)
> amf1=paste("ens_KAPPA_F45_",amph_nome[i],sep="")

You are trying to use R as a macro language, a common source of difficulty for 
persons coming from other language backgrounds. If you have committed to this 
course (instead of using R's capacities to handle lists) then you will nee to 
use the `get` function which returns a data-objent when given a character 
vector name.

working <- get(amf1)

> 
> #amf3=amf2[orden]

# Perhaps (only guessing since you do not describe the algorithm desired
# and `Map` is not defined in your code and on my machine `Map` is a 
"funprog"-function

amf0 <- working[ orden ]


> r1=Map
> values(r1)=amf2
> }
> 
> 
> IF I WAS TO DO IT ONE BY ONE THIS VERSION WORKS 
> 
> orden=match(testo,0:93831)
> amf0=ens_KAPPA_F45_Ambystoma.altamirani[orden]
> r1=Map
> values(r1)=amf0
> 
> writeRaster(r1, filename = "r1.asc",dataType=ascii,overwrite=TRUE)
> 
> Sent from Mail for Windows 10
> 
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA

__
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] Loop over regression results

2015-02-17 Thread Ronald Kölpin
Thank you David and Thierry, your answers helped a lot!

Kind regards,

RK.

__
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] Loop over regression results

2015-02-16 Thread Thierry Onkelinx
Or even easier is you use lmList() from the nlme package

library(nlme)
data(iris)
regression.list - lmList(Sepal.Width ~ Petal.Width | Species, data = iris)
summary(regression.list)
coef(regression.list)


Best regards,

Thierry

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
Forest
team Biometrie  Kwaliteitszorg / team Biometrics  Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

2015-02-16 16:17 GMT+01:00 David L Carlson dcarl...@tamu.edu:

 Or for the slopes and t-values:

  do.call(rbind, lapply(mod, function(x) summary(x)[[coefficients]][2,]))
 Estimate Std. Error  t value Pr(|t|)
 setosa 0.8371922  0.5049134 1.658091 1.038211e-01
 versicolor 1.0536478  0.1712595 6.152348 1.41e-07
 virginica  0.6314052  0.1428938 4.418702 5.647610e-05

 David C

 -Original Message-
 From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of David L
 Carlson
 Sent: Monday, February 16, 2015 8:52 AM
 To: Ronald Kölpin; r-help@r-project.org
 Subject: Re: [R] Loop over regression results

 In R you would want to combine the results into a list. This could be done
 when you create the regressions or afterwards. To repeat your example using
 a list:

 data(iris)
 taxon - levels(iris$Species)
 mod - lapply(taxon, function (x) lm(Sepal.Width ~ Petal.Width,
 data=iris, subset=Species==x))
 names(mod) - taxon
 lapply(mod, summary)
 coeffs - do.call(rbind, lapply(mod, coef, [1))
 coeffs
 # (Intercept) Petal.Width
 # setosa3.222051   0.8371922
 # versicolor1.372863   1.0536478
 # virginica 1.694773   0.6314052

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




 -Original Message-
 From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Ronald
 Kölpin
 Sent: Monday, February 16, 2015 7:37 AM
 To: r-help@r-project.org
 Subject: [R] Loop over regression results

 -BEGIN PGP SIGNED MESSAGE-
 Hash: SHA1

 Dear all,

 I have a problem when trying to present the results of several
 regression. Say I have run several regressions on a dataset and saved
 the different results (as in the mini example below). I then want to
 loop over the regression results in order so save certain values to a
 matrix (in order to put them into a paper or presentation).

 Aside from the question of how to access certain information stored by
 lm() (or printed by summary()) I can't seem to so loop over lm()
 objects -- no matter whether they are stored in a vector or a list.
 They are always evaluated immediately when called. I tried quote() or
 substitute() but that didn't work either as Objects of type 'symbol'
 cannot be indexed.

 In Stata I would simply do something like

 forvalues k = 1/3 {
  quietly estimates restore mod`k'
 // [...]
 }

 and I am looking for the R equivalent of that syntax.

 Kind regard and thanks

 RK


 attach(iris)
 mod1 - lm(Sepal.Width ~ Petal.Width, data=iris, subset=Species==setosa)
 mod2 - lm(Sepal.Width ~ Petal.Width, data=iris,
 subset=Species==versicolor)
 mod3 - lm(Sepal.Width ~ Petal.Width, data=iris,
 subset=Species==virginica)

 summary(mod1); summary(mod2); summary(mod3)

 mat - matrix(data=NA, nrow=3, ncol=5,
   dimnames=list(1:3, c(Model, Intercept, p(T  |T|),
 Slope, R^2)))

 mods - c(mod1, mod2, mod3)

 for(k in 1:3)
 {
 mod - mods[k]
 mat[2,k] - as.numeric(coef(mod))[1]
 mat[3,k] - as.numeric(coef(mod))[1]
 }
 -BEGIN PGP SIGNATURE-
 Version: GnuPG v1

 iQEcBAEBAgAGBQJU4fJnAAoJEKdHe5EUSrVeafwIALerOj+rsZTnbSKOUX6vYpr4
 Uqsx0X2g+IgJw0KLdyqnlDmOut4wW6sWExtVgiugo/bkN8g5rDotGAl06d0UYRQV
 17aLQqQjI6EGXKV9swwlm2DBphtXCIYUCXnDWUoG4Y2wC/4hDnaLbZ9yJFF1GSjn
 +aN/PFf1mPPZLvF1NgMmzLdszP76VYzEgcOcEUfbmB7RU/2WEBLeBYJ8+FD1utPJ
 cnh03rSc/0dgvphP8FO47Nj7mbqqhKL76a9oQqJSJiZJoCFCGiDIIgzq7vwGWc4T
 9apwC/R3ahciB18yYOSMq7ZkVdQ+OpsqDTodnnIIUZjrVIcn9AI+GE0eq1VdLSE=
 =x+gM
 -END PGP SIGNATURE-

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

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

Re: [R] Loop over regression results

2015-02-16 Thread David L Carlson
In R you would want to combine the results into a list. This could be done when 
you create the regressions or afterwards. To repeat your example using a list:

data(iris)
taxon - levels(iris$Species)
mod - lapply(taxon, function (x) lm(Sepal.Width ~ Petal.Width, 
data=iris, subset=Species==x))
names(mod) - taxon
lapply(mod, summary)
coeffs - do.call(rbind, lapply(mod, coef, [1))
coeffs
# (Intercept) Petal.Width
# setosa3.222051   0.8371922
# versicolor1.372863   1.0536478
# virginica 1.694773   0.6314052

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




-Original Message-
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Ronald Kölpin
Sent: Monday, February 16, 2015 7:37 AM
To: r-help@r-project.org
Subject: [R] Loop over regression results

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Dear all,

I have a problem when trying to present the results of several
regression. Say I have run several regressions on a dataset and saved
the different results (as in the mini example below). I then want to
loop over the regression results in order so save certain values to a
matrix (in order to put them into a paper or presentation).

Aside from the question of how to access certain information stored by
lm() (or printed by summary()) I can't seem to so loop over lm()
objects -- no matter whether they are stored in a vector or a list.
They are always evaluated immediately when called. I tried quote() or
substitute() but that didn't work either as Objects of type 'symbol'
cannot be indexed.

In Stata I would simply do something like

forvalues k = 1/3 {
 quietly estimates restore mod`k'
// [...]
}

and I am looking for the R equivalent of that syntax.

Kind regard and thanks

RK


attach(iris)
mod1 - lm(Sepal.Width ~ Petal.Width, data=iris, subset=Species==setosa)
mod2 - lm(Sepal.Width ~ Petal.Width, data=iris,
subset=Species==versicolor)
mod3 - lm(Sepal.Width ~ Petal.Width, data=iris,
subset=Species==virginica)

summary(mod1); summary(mod2); summary(mod3)

mat - matrix(data=NA, nrow=3, ncol=5,
  dimnames=list(1:3, c(Model, Intercept, p(T  |T|),
Slope, R^2)))

mods - c(mod1, mod2, mod3)

for(k in 1:3)
{
mod - mods[k]
mat[2,k] - as.numeric(coef(mod))[1]
mat[3,k] - as.numeric(coef(mod))[1]
}
-BEGIN PGP SIGNATURE-
Version: GnuPG v1

iQEcBAEBAgAGBQJU4fJnAAoJEKdHe5EUSrVeafwIALerOj+rsZTnbSKOUX6vYpr4
Uqsx0X2g+IgJw0KLdyqnlDmOut4wW6sWExtVgiugo/bkN8g5rDotGAl06d0UYRQV
17aLQqQjI6EGXKV9swwlm2DBphtXCIYUCXnDWUoG4Y2wC/4hDnaLbZ9yJFF1GSjn
+aN/PFf1mPPZLvF1NgMmzLdszP76VYzEgcOcEUfbmB7RU/2WEBLeBYJ8+FD1utPJ
cnh03rSc/0dgvphP8FO47Nj7mbqqhKL76a9oQqJSJiZJoCFCGiDIIgzq7vwGWc4T
9apwC/R3ahciB18yYOSMq7ZkVdQ+OpsqDTodnnIIUZjrVIcn9AI+GE0eq1VdLSE=
=x+gM
-END PGP SIGNATURE-

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

__
R-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] Loop over regression results

2015-02-16 Thread David L Carlson
Or for the slopes and t-values:

 do.call(rbind, lapply(mod, function(x) summary(x)[[coefficients]][2,]))
Estimate Std. Error  t value Pr(|t|)
setosa 0.8371922  0.5049134 1.658091 1.038211e-01
versicolor 1.0536478  0.1712595 6.152348 1.41e-07
virginica  0.6314052  0.1428938 4.418702 5.647610e-05

David C

-Original Message-
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of David L Carlson
Sent: Monday, February 16, 2015 8:52 AM
To: Ronald Kölpin; r-help@r-project.org
Subject: Re: [R] Loop over regression results

In R you would want to combine the results into a list. This could be done when 
you create the regressions or afterwards. To repeat your example using a list:

data(iris)
taxon - levels(iris$Species)
mod - lapply(taxon, function (x) lm(Sepal.Width ~ Petal.Width, 
data=iris, subset=Species==x))
names(mod) - taxon
lapply(mod, summary)
coeffs - do.call(rbind, lapply(mod, coef, [1))
coeffs
# (Intercept) Petal.Width
# setosa3.222051   0.8371922
# versicolor1.372863   1.0536478
# virginica 1.694773   0.6314052

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




-Original Message-
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Ronald Kölpin
Sent: Monday, February 16, 2015 7:37 AM
To: r-help@r-project.org
Subject: [R] Loop over regression results

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Dear all,

I have a problem when trying to present the results of several
regression. Say I have run several regressions on a dataset and saved
the different results (as in the mini example below). I then want to
loop over the regression results in order so save certain values to a
matrix (in order to put them into a paper or presentation).

Aside from the question of how to access certain information stored by
lm() (or printed by summary()) I can't seem to so loop over lm()
objects -- no matter whether they are stored in a vector or a list.
They are always evaluated immediately when called. I tried quote() or
substitute() but that didn't work either as Objects of type 'symbol'
cannot be indexed.

In Stata I would simply do something like

forvalues k = 1/3 {
 quietly estimates restore mod`k'
// [...]
}

and I am looking for the R equivalent of that syntax.

Kind regard and thanks

RK


attach(iris)
mod1 - lm(Sepal.Width ~ Petal.Width, data=iris, subset=Species==setosa)
mod2 - lm(Sepal.Width ~ Petal.Width, data=iris,
subset=Species==versicolor)
mod3 - lm(Sepal.Width ~ Petal.Width, data=iris,
subset=Species==virginica)

summary(mod1); summary(mod2); summary(mod3)

mat - matrix(data=NA, nrow=3, ncol=5,
  dimnames=list(1:3, c(Model, Intercept, p(T  |T|),
Slope, R^2)))

mods - c(mod1, mod2, mod3)

for(k in 1:3)
{
mod - mods[k]
mat[2,k] - as.numeric(coef(mod))[1]
mat[3,k] - as.numeric(coef(mod))[1]
}
-BEGIN PGP SIGNATURE-
Version: GnuPG v1

iQEcBAEBAgAGBQJU4fJnAAoJEKdHe5EUSrVeafwIALerOj+rsZTnbSKOUX6vYpr4
Uqsx0X2g+IgJw0KLdyqnlDmOut4wW6sWExtVgiugo/bkN8g5rDotGAl06d0UYRQV
17aLQqQjI6EGXKV9swwlm2DBphtXCIYUCXnDWUoG4Y2wC/4hDnaLbZ9yJFF1GSjn
+aN/PFf1mPPZLvF1NgMmzLdszP76VYzEgcOcEUfbmB7RU/2WEBLeBYJ8+FD1utPJ
cnh03rSc/0dgvphP8FO47Nj7mbqqhKL76a9oQqJSJiZJoCFCGiDIIgzq7vwGWc4T
9apwC/R3ahciB18yYOSMq7ZkVdQ+OpsqDTodnnIIUZjrVIcn9AI+GE0eq1VdLSE=
=x+gM
-END PGP SIGNATURE-

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

__
R-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] Loop with ggplot2 not as simple as it seems...

2014-10-29 Thread PIKAL Petr
Hi Patricia

You are somewhat circling around solution.

Is this what you wanted?

for (i in 5:7) {
  plotname = paste(Graph, names(scores)[i], sep=)
  png(paste0(plotname,.png))
  p - ggplot(scores, aes(x=scores[,i], fill=gender ))
  print(p+ geom_density(alpha=.3)+xlab(names(scores)[i]))
dev.off()
}

Cheers
Petr


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of Patricia Seo
 Sent: Tuesday, October 28, 2014 1:59 AM
 To: r-help@r-project.org
 Subject: [R] Loop with ggplot2 not as simple as it seems...

 Hi everyone,

 I have been battling with this problem for the past month and reading
 all that I can about it, but I just can't seem to understand what I'm
 doing wrong. It seems easy and I can replicate others well-recorded
 attempts, but can not seem to apply this to my data.

 I have created a data.frame to easily illustrate my problem. I would
 like to plot columns 5:7 in my data without having to write-out-every-
 single-variable name. In this example it is only three columns but in
 my actual data set it is more like 50 columns. Here is the simplified
 data frame:

 sid - c(1001:1010) #student id
 age - c(10, 12, 14, 15, 13, 16, 14, 12, 14, 10)
 race - race - c(w, b, a, w, a, w, b, a, w, a)
 gender - gender -c(M, F, M, F, M, F, M, F, M, F)
 read - rnorm(10, 100, 50)
 write - rnorm(10, 100, 50)
 math-rnorm(10, 100, 50)

 scores - data.frame(sid, age, race, gender, read, write, math)

 My end goal is to produce a .png like this for every column:

 ggplot(scores, aes(x=scores$read, fill=scores$gender)) +
 geom_density(alpha=.3)

 In other words, I would like to write a loop that would produce
 separate .pngs for each column. No facet-wrap. I would like one plot
 showing the distribution of one score for male and female in one .png
 file.

 Failed Attempt #1 of 3: My first attempt was to melt data since I know
 ggplot2 likes melted data. I have tried melting the data using sid,
 age, race and gender as the id variable. But I’m not sure how to plot
 what I would like above with just using values. In my actual data set
 there are over 50 columns to plot (instead of the simple three in the
 example scores data). I tried creating:

 xobject - subset(scores.melt, variable=read)

 but it gives me the error: Don't know how to automatically pick scale
 for object of type data.frame. Defaulting to continuous
 Error: Aesthetics must either be length one, or the same length as the
 dataProblems:test1

 Failed Attempt #2 of 3: My second attempt is below but it had similar
 problems to what others have mentioned with ggplot changing the names
 of each plot but keeping the last known data column so all the graphs
 look the same. It works in terms of titles but not data! And I have
 tried as_string() but to no avail:

 for (i in 5:7) {
   column_to_plot = as.character(paste(Col_, i, sep=))
   png(paste0(Graph, column_to_plot,.png))
   ggplot(scores, aes(x=column_to_plot, fill=gender )) +
 geom_density(alpha=.3)
   ggsave(paste0(Graph, column_to_plot,.png))

 }

 Failed Attempt #3 of 3: My third attempt is my most successful attempt.
 It is because I created a vector that includes the list of variables I
 would like to plot. Unfortunately, I have two problems with this
 method: (1) Don’t know how to include the corresponding title name so I
 know which plot corresponds to the score even though I do indicate the
 names of the Indexes, and (2) It is annoying to have to write the
 entire list of variables I want to plot especially if there are 50
 variable names. I know I can specify columns 5:7 (like above) but I’m
 not grasping the logics of this loop:

 Indexes = list()
 Indexes[[1]] = scores$read
 Indexes[[2]] = scores$write
 Indexes[[3]] = scores$math

 names(Indexes) = c(“Scores for Read“,
“Scores for Write”, “Scores for Math“)

 # for some reason, I have to run the top half and after it has
 processed that, run the bottom half#

 for(i in seq(along = Indexes)) {
   Index = Indexes[[i]]

   for(j in 1:length(gender)) {
 png(paste0(Graph, Index,.png))
 ggplot(scores, aes(x=Index, fill = gender)) +
 geom_density(alpha=.3)
 ggsave(paste0(Graph, Index,.png))

   }
 }

 Any help would be much appreciated. I know this is a frankenstein from
 previous questions and problems with loops in ggplot2, but just
 understand what I'm doing wrong would even be a huge help.

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


Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny 
pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně 
jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte 

Re: [R] loop

2014-10-05 Thread jim holtman
Please don't post in HTML since your code was all messed up.  You did
not mention what problems you were having with your code.  Now a
couple of things to check is to look at what the structure of r that
you are trying to add to sum (which should have been Sum according
to your assignment earlier in the function.).

Browse[1] str(r)
List of 5
 $ value   : num 0.0548
 $ abs.error   : num 6.08e-16
 $ subdivisions: int 1
 $ message : chr OK
 $ call: language integrate(f = integrand2, lower = 0, upper = 2)
 - attr(*, class)= chr integrate

shows that r is a list and you want r$value to do the addition.
So after formatting your code, and making a couple of corrections, is
this what you were expecting to see:

 B-function(n){
+ Sum-1
+ for (k in 0:n){
+ BB-function(k){
+ integrand2-function(x,a=1.5){
+
(((a+1)*x)^k)*((2-x)^(n))*(exp(-a*x-2))/(factorial(k)*factorial(n))
+ }
+ integrate(integrand2,0,2)
+ }
+ r-BB(k)
+ print(r)
+ Sum-Sum+r$value
+ }
+ print(Sum-1)
+ }
 B(3)
0.05479674 with absolute error  6.1e-16
0.03780519 with absolute error  4.2e-16
0.02371485 with absolute error  2.6e-16
0.01355982 with absolute error  1.5e-16
[1] 0.1298766

Jim Holtman
Data Munger Guru

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


On Sun, Oct 5, 2014 at 6:01 PM, pari hesabi statistic...@hotmail.com wrote:
 Hello ,I am trying to write a loop for sum of integrals .
 the integral 
 is:integrand4-function(x,a=1.5,n=3,k=0){(((a+1)*x)^k)*((2-x)^n)*(exp(-a*x-2))/(factorial(k)*factorial(n))}

 integrate(integrand4,0,2).
 I need a loop to give me the sum of integrals over k = 0,.n , for every 
 positive integer input (n).can anybody check my program and tell me about 
 it's problem?I am looking forward to your suggestions.
 B-function(n){Sum-1for (k in 
 0:n){BB-function(k){integrand2-function(x,a=1.5){(((a+1)*x)^k)*((2-x)^(n))*(exp(-a*x-2))/(factorial(k)*factorial(n))}
  integrate(integrand2,0,2)}r-print(BB(k))sum-sum+r}print(sum-1)}

 Best Regards,Diba
 [[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] Loop does not work: Error in else statement (II)

2014-10-02 Thread Frank S.
Jim, Thanks for the comment about else! 
  
[[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] Loop does not work: Error in else statement

2014-09-30 Thread PIKAL Petr
Hi

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of Frank S.
 Sent: Monday, September 29, 2014 9:17 PM
 To: r-help@r-project.org
 Subject: [R] Loop does not work: Error in else statement

 Hi to all members of R list,



 I m working with data.table package, and with 6
 variables: ID (Identifier), born (Birthdate), start (Starting
 date), register (date of measurement), valueand end (date of
 expiration). So, the natural order of dates would be: born = start =
 register = end.  As an example, I have: DT - data.table(ID =
 as.factor(rep(1:4,each=2)),

  born =
 as.Date(rep(c(1955-02-20, 1990-07-25, 1972-03-18, 1988-05-
 03),each=2)),

  start =
 as.Date(rep(c(1953-03-28, 1990-07-01, 1983-09-05, 1988-07-
 18),each=2)),

  register =
 as.Date(c(1955-08-11, 1958-03-28,
 1990-07-09, 1992-07-01,

 1983-09-05,
 2002-09-28, 1992-07-10, 1993-03-12)),

  value =
 c(205, 346, 34, 76, 320, 148, 209, 274),

  end =
 as.Date(rep(c(1960-11-05, 1997-10-15, 2002-09-27, 1997-03-
 02),each=2)))



 I would want to make 3 operations:1. First: Remove entire ID s where
 start is previous to born date (excepting those subjects whose
 month and year values are the same in start and born variables: I
 assign born
 date to start date in these cases).Afterwords:2. Remove only specific
 rows (not all ID) where  register  is previous to  start .3.Remove only
 specific rows (not all ID) where  end  is previous to  register .

 I have: DT[ , {if (all(born  start))   { indx -
 which(paste(year(born)) == paste(year(start))   paste(month(born)) ==
 paste(month(start)))  result - list(born=born[indx],
 start=born[indx], register=register[indx], value=value[indx],
 end=end[indx])  }if (all(register  start) | all(end 
 register)) { indx - which((register  start) | (end 
 register)) result - list(born=born[indx], start=start[indx],
 register=register[indx], value=value[indx], end=end[indx])  }
 else { NULL }   else { indx -
 which(all(register  start) | all(end  register)) result -
 list(born=born[indx], start=start[indx], register=register[indx],
 value=value[indx], end=end[indx])  }   result   }, by=ID] BUT I GET
 AN ERROR MESSAGE: Error: syntax error, unexpected ELSE in else
 Please, can anyone help me? Thank you!!

Missing parentheses? Missing comma? Syntactically valid expression before else?

From HTML scrambled post hard to tell.

Wild guess.

some if statement could be evaluated without before else statement as valid.

if (something) {do this}
else {do that}

results in error as first line is evaluated and else is not needed for this 
evaluation. Therefore you start the second line without leading if.

if (something) {do this} else
{do that}

is valid and does not throw error.

Maybe it is covered in FAQs

Regards
Petr






   [[alternative HTML version deleted]]



Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny 
pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně 
jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze 
svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email 
jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či 
zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a 
to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce 
s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným 
dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost 
žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně 
pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně 
osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi 
či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are 
intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. 
Delete the contents of this e-mail with all attachments and its copies from 
your system.
If you are not the intended recipient of this e-mail, you are not authorized to 
use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by 
modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of 

Re: [R] Loop does not work: Error in else statement

2014-09-30 Thread Frank S.
Dear Berend and Petr,
 
I do apologise for the disorderly code I posted. I have tried to solve it in a 
new mail.
 
Frank S. 
  
[[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] Loop does not work: Error in else statement (II)

2014-09-30 Thread PIKAL Petr
Hi

Slightly better but still html scrambled.

see in line

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of Frank S.
 Sent: Tuesday, September 30, 2014 2:55 PM
 To: r-help@r-project.org
 Subject: [R] Loop does not work: Error in else statement (II)



 Hi to all members of R list,



 I m working with data.table package, and with 6
 variables:



 ID: Subject identifier

 born: Birthdate

 start: Starting date

 register: date of measurement

 value: Value of measurement

 end: date of expiration of the measurements.



 So, the natural order of date variables would be: born = start =
 register = end. As an example, I have:



 DT - data.table(ID =
 as.factor(rep(1:4,each=2)),

   born=as.Date(rep(c(1955-02-20,1990-07-25,1972-03-18,
 1988-05-03),each=2)),

   start=as.Date(rep(c(1953-03-28,1990-07-01,1983-09-05,1988-
 07-18),each=2)),

   register = as.Date(c(1955-08-11, 1958-03-28, 1990-07-09,
 1992-07-01,

   1983-09-05, 2002-09-28, 1992-07-10, 1993-03-
 12)),

   value=c(205, 346, 34, 76, 320, 148, 209, 274),

   end=as.Date(rep(c(1960-11-05,
 1997-10-15, 2002-09-27,
 1997-03-02),each=2)))



 I would want to make 3 operations:

 a)  Remove
 entire ID s where start is previous to born date (excepting those
 subjects whose month and year values are the same in  start  and  born
 variables: I assign  born  date to  start  date in these cases).

 DT$start-DT$born
Time differences in days
[1] -694 -694  -24  -24 4188 4188   76   76

suggests you can remove line 1 and 2 and check line 3 and 4

as.numeric(format(DT$start, %Y.%m))- as.numeric(format(DT$born, %Y.%m))

[1] -1.99 -1.99  0.00  0.00 11.06 11.06  0.02  0.02

so only lines 1 and 2 shall be removed

#first change born to start where appropriate
cond1 - which((as.numeric(format(DT$start, %Y.%m))- 
as.numeric(format(DT$born, %Y.%m)))==0)
DT$born[cond1] - DT$start[cond1]

#then select rows for which start is earlier than born
cond2 -((DT$start-DT$born)0)

#remove rows
DT2-DT[!cond2,]

If there are rows which does not hold for some ID it is trickier.

Hint
DT$ID[cond2]


 b)  Remove
 only specific rows (not all ID) where  register  is previous to  start

#select rows which hold your condition
cond3 -((DT2$register-DT2$born)0)
#no items does not result as TRUE but

#remove rows which are TRUE in cond3
DT2 -DT2[!cond3,]


 .

 c)   Remove
 only specific rows (not all ID) where  end  is previous to  register .


the same principle applies here. You shall be able to do it yourself


No if, no else no loop, no error.

If there are NAs the solution would be slightly different but in principle 
similar.

Regards
Petr





 DT[ ,

   {



 if (all(born  start))


 {


 indx - which(paste(year(born))==paste(year(start)) 
 paste(month(born))==paste(month(start)))


 result -
 list(born=born[indx],start=born[indx],register=register[indx],

 value=value[indx], end=end[indx])


 }


 if (all(register  start) | all(end  register))

   {


   indx - which((register
  start) | (end  register))


   result -
 list(born=born[indx],start=start[indx], register=register[indx],

 value=value[indx], end=end[indx])

 }

   else

   {


  NULL


   }

   else


 {


 indx - which(all(register  start) | all(end  register))


 result - list(born=born[indx], start=start[indx],
 register=register[indx],

 value=value[indx], end=end[indx])


 }


 result


 }, by=ID]



 Error: unexpected 'else' in:

   }

   else




   [[alternative HTML version deleted]]



Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny 
pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně 
jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze 
svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email 
jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či 
zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a 
to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce 
s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným 
dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost 
žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně 
pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně 
osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi 
či osobě jím zastoupené známá.

This e-mail and 

Re: [R] Loop does not work: Error in else statement

2014-09-29 Thread Berend Hasselman

Please, please do not post in HTML as the Posting guide requests. See the tail 
of each message to R-help.
Your code is completely messed up and unreadable.

Berend

On 29-09-2014, at 21:17, Frank S. f_j_...@hotmail.com wrote:

 Hi to all members of R list,
 
 
 
 I�m working with data.table package, and with 6
 variables: ID (Identifier), born (Birthdate), start (Starting date), 
 register (date of measurement), valueand end (date of expiration). So, 
 the natural order of dates would be: born
 = start = register = end.  As an example, I have: DT - data.table(ID = 
 as.factor(rep(1:4,each=2)), 
 
 born =
 as.Date(rep(c(1955-02-20, 1990-07-25,
 1972-03-18, 1988-05-03),each=2)),
 
 start =
 as.Date(rep(c(1953-03-28, 1990-07-01,
 1983-09-05, 1988-07-18),each=2)),
 
 register =
 as.Date(c(1955-08-11, 1958-03-28,
 1990-07-09, 1992-07-01, 
 
1983-09-05,
 2002-09-28, 1992-07-10, 1993-03-12)),
 
 value =
 c(205, 346, 34, 76, 320, 148, 209, 274),
 
 end =
 as.Date(rep(c(1960-11-05, 1997-10-15,
 2002-09-27, 1997-03-02),each=2)))
 
 
 
 I would want to make 3 operations:1. First: Remove
 entire ID�s where start is previous to born date (excepting those 
 subjects whose
 month and year values are the same in start and born variables: I assign 
 born
 date to start date in these cases).Afterwords:2. Remove only
 specific rows (not all ID) where �register� is previous to �start�.3.Remove 
 only
 specific rows (not all ID) where �end� is previous to �register�.
 
 I have: DT[ , {if (all(born  start))   { indx - 
 which(paste(year(born)) == paste(year(start))   paste(month(born)) == 
 paste(month(start)))  result - list(born=born[indx], start=born[indx], 
 register=register[indx], value=value[indx], end=end[indx])  }if 
 (all(register  start) | all(end  register)) { indx - 
 which((register  start) | (end  register)) result - 
 list(born=born[indx], start=start[indx], register=register[indx], 
 value=value[indx], end=end[indx])  }  else { NULL 
 }   else { indx - which(all(register  start) | all(end  
 register)) result - list(born=born[indx], start=start[indx], 
 register=register[indx], value=value[indx], end=end[indx])  }   result   
 }, by=ID] BUT I GET AN ERROR MESSAGE: Error: syntax error, unexpected ELSE in 
 else  Please, can anyone help me? Thank you!!  
 
 
   [[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] Loop Autoregression

2014-06-04 Thread PIKAL Petr
Hi Jonas

Actually I am a poor chemist and so I do not know what you are talkin' about. 
On stackoverflow I found that the error is probably due to wrong format of 
NSSParameter which probably is data frame and it shall be xts.

Maybe others can help you more but I believe they will expect more info from 
your side (at least what object is NSSParameter).

Regards
Petr


 -Original Message-
 From: Jonas Ulbrich [mailto:jonas.ulbr...@ruhr-uni-bochum.de]
 Sent: Tuesday, June 03, 2014 3:02 PM
 To: PIKAL Petr
 Subject: RE: [R] Loop Autoregression


 Dear Petr,

 thank you for your rapid answer. The problem is solved.

 My second and last question is as follows:


 I use the yield curve package and I want to calculate the fitted yields
 via the Svensson approach.
 The I have already calculated the six required parameters. At first I
 read them in from a CSV-file.
 NSSParameter-
 (read.csv(C:/Users/admin/Desktop/Universität/SeminarinFinance/R/Parame
 terSvensson.csv,sep=;,dec=.,header=TRUE)

 Now I want to plug them in in the function.
 Srates(NSSParameter,maturity,whichRate = Spot).

 NSSParameter is defined as mentioned above looks like for 500 values:

 beta_0beta_1   beta_2 beta_3  tau1 tau2
1 3.824276 -3.726540 -1.878026723  -3.856153 1.1617407 1.672910
2 3.786777 -3.688137 -1.929259408  -3.780010 1.1617407 1.672910

 Maturity is maturity-c(1/12,3/12,6/12,1,2,3,5,7,10,20,30).
 Spot is the sort of interest rate I want to determine.

 The error request I receive now is: Error in hasTsp(x) : invalid time
 series parameters specified. How fix this concrete case? I have not
 found an example to that.

 Best Regards Jonas


Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny 
pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně 
jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze 
svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email 
jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či 
zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a 
to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce 
s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným 
dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost 
žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně 
pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně 
osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi 
či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are 
intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. 
Delete the contents of this e-mail with all attachments and its copies from 
your system.
If you are not the intended recipient of this e-mail, you are not authorized to 
use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by 
modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a 
contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately 
accept such offer; The sender of this e-mail (offer) excludes any acceptance of 
the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an 
express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into 
any contracts on behalf of the company except for cases in which he/she is 
expressly authorized to do so in writing, and such authorization or power of 
attorney is submitted to the recipient or the person represented by the 
recipient, or the existence of such authorization is known to the recipient of 
the person represented by the recipient.
__
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] Loop Autoregression

2014-06-04 Thread PIKAL Petr
Hi

I am not sure if Srates is a function I found in R-Forge. If yes, NSSParameter 
has to be time series as there is command

time(x) in the function which, in case of data frame, gives this error.

 time(sadr)
Error in hasTsp(x) : invalid time series parameters specified

Regards
Petr

 -Original Message-
 From: Jonas Ulbrich [mailto:jonas.ulbr...@ruhr-uni-bochum.de]
 Sent: Wednesday, June 04, 2014 10:48 AM
 To: PIKAL Petr
 Subject: Re: [R] Loop Autoregression

 Thanks for the answer. The class of NSS Parameter is data frame.

 On 2014-06-04 09:12, PIKAL Petr wrote:
  Hi Jonas
 
  Actually I am a poor chemist and so I do not know what you are
 talkin'
  about. On stackoverflow I found that the error is probably due to
  wrong format of NSSParameter which probably is data frame and it
 shall
  be xts.
 
  Maybe others can help you more but I believe they will expect more
  info from your side (at least what object is NSSParameter).
 
  Regards
  Petr
 
 
  -Original Message-
  From: Jonas Ulbrich [mailto:jonas.ulbr...@ruhr-uni-bochum.de]
  Sent: Tuesday, June 03, 2014 3:02 PM
  To: PIKAL Petr
  Subject: RE: [R] Loop Autoregression
 
 
  Dear Petr,
 
  thank you for your rapid answer. The problem is solved.
 
  My second and last question is as follows:
 
 
  I use the yield curve package and I want to calculate the fitted
  yields via the Svensson approach.
  The I have already calculated the six required parameters. At first
 I
  read them in from a CSV-file.
  NSSParameter-
 
 
 (read.csv(C:/Users/admin/Desktop/Universität/SeminarinFinance/R/Para
  me
  terSvensson.csv,sep=;,dec=.,header=TRUE)
 
  Now I want to plug them in in the function.
  Srates(NSSParameter,maturity,whichRate = Spot).
 
  NSSParameter is defined as mentioned above looks like for 500
  values:
 
  beta_0beta_1   beta_2 beta_3  tau1 tau2
 1 3.824276 -3.726540 -1.878026723  -3.856153 1.1617407 1.672910
 2 3.786777 -3.688137 -1.929259408  -3.780010 1.1617407 1.672910
 
  Maturity is maturity-c(1/12,3/12,6/12,1,2,3,5,7,10,20,30).
  Spot is the sort of interest rate I want to determine.
 
  The error request I receive now is: Error in hasTsp(x) : invalid
 time
  series parameters specified. How fix this concrete case? I have not
  found an example to that.
 
  Best Regards Jonas
 
  
  Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a
  jsou určeny pouze jeho adresátům.
  Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě
  neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho
  kopie vymažte ze svého systému.
  Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento
  email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
  Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou
  modifikacemi či zpožděním přenosu e-mailu.
 
  V případě, že je tento e-mail součástí obchodního jednání:
  - vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření
  smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
  - a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně
  přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí
 nabídky
  ze strany příjemce s dodatkem či odchylkou.
  - trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve
  výslovným dosažením shody na všech jejích náležitostech.
  - odesílatel tohoto emailu informuje, že není oprávněn uzavírat za
  společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně
  zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly
  adresátovi tohoto emailu případně osobě, kterou adresát zastupuje,
  předloženy nebo jejich existence je adresátovi či osobě jím
 zastoupené
  známá.
 
  This e-mail and any documents attached to it may be confidential and
  are intended only for its intended recipients.
  If you received this e-mail by mistake, please immediately inform its
  sender. Delete the contents of this e-mail with all attachments and
  its copies from your system.
  If you are not the intended recipient of this e-mail, you are not
  authorized to use, disseminate, copy or disclose this e-mail in any
  manner.
  The sender of this e-mail shall not be liable for any possible damage
  caused by modifications of the e-mail or by delay with transfer of
 the
  email.
 
  In case that this e-mail forms part of business dealings:
  - the sender reserves the right to end negotiations about entering
  into a contract in any time, for any reason, and without stating any
  reasoning.
  - if the e-mail contains an offer, the recipient is entitled to
  immediately accept such offer; The sender of this e-mail (offer)
  excludes any acceptance of the offer on the part of the recipient
  containing any amendment or variation.
  - the sender insists on that the respective contract is concluded
 only
  upon an express mutual agreement on all its aspects.
  - the sender of this e-mail informs that he/she

Re: [R] Loop Autoregression

2014-06-02 Thread PIKAL Petr
Hi

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of Jonas Ulbrich
 Sent: Sunday, June 01, 2014 1:51 PM
 To: R-help@r-project.org
 Subject: [R] Loop Autoregression

 Hello everybody, I have to confess that I am relatively new to R.

 My problem is the following one:

 I have a data set of 500 values. For each value I wanna make prediction
 of four steps ahead via autogregression. Because I have to repeat this
 procedure on three other data sets I wanna make a loop. (Manual
 calculation would take ages.) My idea which does not work looks is as
 follows:

 x-NULL
 for (i in 1:500){

 x[i]-predict(ar(y[i],order.max=1,method=yule-walker,intercept=TRUE,
 n.used=500, n.ahead=4))
 }

We have no idea what is y and y[i]. I presume that y is vector and y[i] is one 
item from this vector, hence the error as n.used is actually 1 and order.max is 
also 1.

 ar((lh)[1])
Error in ar.yw.default(x, aic = aic, order.max = order.max, na.action = 
na.action,  :
  'order.max' must be = 1


you can find how to use ar in its help page.
?ar

Also your parentheses are mismatched. Most probably you want

predict(ar(y,order.max=1,method=yule-walker,intercept=TRUE,n.used=500), 
n.ahead=4)

Regards
Petr




 Doing this I only get an error request that the value of order.maX
 needs to be smaller than n.used. But this is the case. Isn't it?

 Best Regards

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


Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny 
pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně 
jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze 
svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email 
jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či 
zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a 
to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce 
s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným 
dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost 
žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně 
pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně 
osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi 
či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are 
intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. 
Delete the contents of this e-mail with all attachments and its copies from 
your system.
If you are not the intended recipient of this e-mail, you are not authorized to 
use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by 
modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a 
contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately 
accept such offer; The sender of this e-mail (offer) excludes any acceptance of 
the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an 
express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into 
any contracts on behalf of the company except for cases in which he/she is 
expressly authorized to do so in writing, and such authorization or power of 
attorney is submitted to the recipient or the person represented by the 
recipient, or the existence of such authorization is known to the recipient of 
the person represented by the recipient.
__
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] Loop Issue

2014-05-23 Thread Jim Lemon
On Thu, 22 May 2014 09:11:43 PM Ricardo Rocha wrote:
 Hi everybody.
 
 Consider the following exampling code:
 
 x=numeric()
   for(i in 1:10){
 u=runif(1,-1,2)
 x[i]=log(u)
   }
 This code, in each interation, generates a random value in the (-1,2)
 interval and then calculates the log of the value. When the generated 
value
 is less than 0 the log produces a NaN, which gives a warning.
 
 What I want is to make it start over when a warning is produced, in 
order to
 repeat it until a positive value is generated and therefore the log is
 calculated. Logically, would be like: if there's a warning here, go back
 at the beginning and start over, without changing the iteration.
 
 Could someone help me with some directions?
 
Hi Ricardo,
Perhaps what you want is something like this:

i-1
while(I  11) {
 u-runif(1,-1,2)
 if(u = 0) {
  x[i]-log(u)
  i-i+1
 }
}

Jim

__
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] Loop Issue

2014-05-23 Thread arun
Hi,
You may also try:
fun1 - function(n, repl, val1, val2) {
    mat1 - suppressWarnings(replicate(repl, log(runif(n, val1, val2
    mat1[!is.na(mat1)][seq(n)]
}

#Jim's function
fun2 - function(init, final, val1, val2) {
    i - init
    while (i  final) {
    u - runif(1, val1, val2)
    if (u = 0) {
    x[i] - log(u)
    i - i + 1
    }
    }
    x
}


set.seed(49)
 x1 - fun1(10,3,-1,2)
 set.seed(49)
 x2 - fun2(1,11,-1,2)
 identical(x1,x2)
#[1] TRUE

###Speed comparison
set.seed(58)
 system.time(x1 - fun1(1e5,100, -1,2))
#   user  system elapsed 
#  1.392   0.105   1.500 


set.seed(58)
 system.time(x2 - fun2(1,1e5+1,-1,2))
#   user  system elapsed 
# 31.930   0.988  32.973

identical(x1,x2)
#[1] TRUE


A.K.








On Friday, May 23, 2014 4:19 AM, Jim Lemon j...@bitwrit.com.au wrote:
On Thu, 22 May 2014 09:11:43 PM Ricardo Rocha wrote:
 Hi everybody.
 
 Consider the following exampling code:
 
 x=numeric()
   for(i in 1:10){
     u=runif(1,-1,2)
     x[i]=log(u)
   }
 This code, in each interation, generates a random value in the (-1,2)
 interval and then calculates the log of the value. When the generated 
value
 is less than 0 the log produces a NaN, which gives a warning.
 
 What I want is to make it start over when a warning is produced, in 
order to
 repeat it until a positive value is generated and therefore the log is
 calculated. Logically, would be like: if there's a warning here, go back
 at the beginning and start over, without changing the iteration.
 
 Could someone help me with some directions?
 
Hi Ricardo,
Perhaps what you want is something like this:

i-1
while(I  11) {
u-runif(1,-1,2)
if(u = 0) {
  x[i]-log(u)
  i-i+1
}
}

Jim


__
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] Loop Issue

2014-05-22 Thread Yvan Richard
Hi Ricardo
Assuming you have a good reason to use such approach (what are you
trying to do ultimately?), you can just increment your counter when
you get a good value, i.e.:

x - numeric()
n - 0
while (n  10) {
u - log(runif(1, -1, 2))
if (is.finite(u)) {
n - n+1
x[n] - u
}
}
x

On 23 May 2014 12:11, Ricardo Rocha ricardoroch...@hotmail.com wrote:
 Hi everybody.

 Consider the following exampling code:

 x=numeric()
   for(i in 1:10){
 u=runif(1,-1,2)
 x[i]=log(u)
   }
 This code, in each interation, generates a random value in the (-1,2) 
 interval and then calculates the log of the value.
 When the generated value is less than 0 the log produces a NaN, which gives a 
 warning.

 What I want is to make it start over when a warning is produced, in order to 
 repeat it until a positive value is generated and therefore the log is 
 calculated. Logically, would be like: if there's a warning here, go back at 
 the beginning and start over, without changing the iteration.

 Could someone help me with some directions?

 Thanks a lot,

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



-- 
Yvan Richard

  DRAGONFLY Science

Physical address: Level 5, 158 Victoria St, Te Aro, Wellington
Postal address: PO Box 27535, Wellington 6141
New Zealand
Ph: 04.385.9285
web page

__
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] Loop to extract from variables in the workspace

2014-04-23 Thread Bea GD

Dear Frede,

Sorry for the late reply, yesterday I was out of office.

Thanks so much for your code! It has just compacted in a few lines what 
I had written in several 'paragraphs'!


To be honest, I'm not familiar to work with lists in R. I think one of 
its main advantages is the reduced size of the outputs.


Thanks again and have a nice day,

Bea


On 22.04.2014 07:04, Frede Aakmann Tøgersen wrote:

Well, sticking to your loop that you seem comfortable with try this:

## list to hold dataframes
extracList - vector(list, length(1981:2010))

cnt - 0
for(i in 1981:2010){
cnt - cnt + 1
rasterObj -get( c(paste(RR_, i, _1, sep=)))
extractList[[cnt]]- extract(rasterObj, coords, df=T)}
  }

See ?get which is kind of the opposite of assign.

To coerce the list of dataframes into one dataframe several methods are 
available:

do.call(rbind, extractList)

plyr::rbind.fill(extractList)

plyr::ldply(extractList, data.frame)

as.data.frame(data.table::rbindlist(extractList))

This was taken from 
http://stackoverflow.com/questions/2851327/converting-a-list-of-data-frames-into-one-data-frame-in-r
 where you get get some pros and cons on the methods.

The fastest way will probably be to create a huge dataframe and during the loop 
calculated the row indices of that dataframe for each raster into the result 
from extract should be put.



Yours sincerely / Med venlig hilsen


Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance  Modeling

Technology  Service Solutions
T +45 9730 5135
M +45 2547 6050
fr...@vestas.com
http://www.vestas.com

Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice
If you have received this e-mail in error please contact the sender.



-Original Message-
From: Beatriz R. Gonzalez Dominguez [mailto:aguitatie...@hotmail.com]
Sent: 21. april 2014 16:27
To: Frede Aakmann Tøgersen; r-help@r-project.org
Subject: Re: [R] Loop to extract from variables in the workspace

Hi Frede,

Many thanks for your reply.
1. The first argument in extract is a Formal class RasterLayer in the
Workspace (e.g RR_1981_1 ).

2. I created an intermediate name to hold the result fromthe extract
function because I'd like to create several dataframes with the output
of the iterative (loop) extraction. I'd like to get the same result as
when I do:

PE.coords_01_1981 - extract(RR_1981_1, coords, df=T)
PE.coords_01_1982 - extract(RR_1982_1, coords, df=T)
PE.coords_01_1983 - extract(RR_1983_1, coords, df=T)
PE.coords_01_1984 - extract(RR_1984_1, coords, df=T)
[... this works no problem]

3. 'coords' is a SpatialPointsDataFrame.

4. I used assign in the loop becuase I thought it was the way forward to
create new variables out of it. Isn't it?

What I'd like to do is to use coordinate points ('coords') to extract
raster pixel values (eg. 'RR_1981_1') on which the points are overlying.
Then I'd like to build a bigger data frame including the data from all
the outputs (i.e. PE.coords_01_1981, PE.coords_01_1982).

Hope to have explained myself properly. Please let me know if anything
else should be clarified.

Best wishes,

Bea


On 21/04/2014 15:17, Frede Aakmann Tøgersen wrote:

Hi Beatriz

Did you read the help for extract{raster} carefully?

Several things can be wrong.

1) First argument to extract is not a file name but a raster object.
2) In the loop you name an object extract as an intermediate name to hold

the result from the extract function. Do you think there could be a name
clash? R is clever but perhaps not clever enough.

3) coords are of the right class (see ?extract).
4) assign can be useful from time to time. But in a loop?

I think the things you are doing are some intermediate results that needs

more processing. Do you think this is the right way to do that. For instance
instead of storing the immediate result as separate objects why not store
those in a list.

Perhaps if you tell us what you would like to do  overall, i.e. from first to

last, then we will be able to help you to become more efficient.


Yours sincerely / Med venlig hilsen


Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance  Modeling

Technology  Service Solutions
T +45 9730 5135
M +45 2547 6050
fr...@vestas.com
http://www.vestas.com

Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice
If you have received this e-mail in error please contact the sender.



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

project.org]

On Behalf Of Beatriz R. Gonzalez Dominguez
Sent: 21. april 2014 14:53
To: r-help@r-project.org
Subject: [R] Loop to extract from variables in the workspace

Dear all,

I'm starting to work with loops and I'm stucked on something.
I've been searching and trying different possibilities but I don't get
to the solution.
I'd be very grateful if you

Re: [R] Loop to extract from variables in the workspace

2014-04-21 Thread Frede Aakmann Tøgersen
Hi Beatriz

Did you read the help for extract{raster} carefully?

Several things can be wrong.

1) First argument to extract is not a file name but a raster object.
2) In the loop you name an object extract as an intermediate name to hold the 
result from the extract function. Do you think there could be a name clash? R 
is clever but perhaps not clever enough.
3) coords are of the right class (see ?extract).
4) assign can be useful from time to time. But in a loop?

I think the things you are doing are some intermediate results that needs more 
processing. Do you think this is the right way to do that. For instance instead 
of storing the immediate result as separate objects why not store those in a 
list.

Perhaps if you tell us what you would like to do  overall, i.e. from first to 
last, then we will be able to help you to become more efficient.  


Yours sincerely / Med venlig hilsen


Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance  Modeling

Technology  Service Solutions
T +45 9730 5135
M +45 2547 6050
fr...@vestas.com
http://www.vestas.com

Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice
If you have received this e-mail in error please contact the sender. 


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
 On Behalf Of Beatriz R. Gonzalez Dominguez
 Sent: 21. april 2014 14:53
 To: r-help@r-project.org
 Subject: [R] Loop to extract from variables in the workspace
 
 Dear all,
 
 I'm starting to work with loops and I'm stucked on something.
 I've been searching and trying different possibilities but I don't get
 to the solution.
 I'd be very grateful if you could share any ideas that you think may help.
 
 library(raster)
 
 # All my variables are in the workspace
 
 # This is what I'd like to obtain, but with a loop (I'm working with
 several years and variables).
 PE.coords_01_1981 - extract(RR_1981_1, coords, df=T)
 PE.coords_01_1982 - extract(RR_1982_1, coords, df=T)
 PE.coords_01_1983 - extract(RR_1983_1, coords, df=T)
 PE.coords_01_1984 - extract(RR_1984_1, coords, df=T)
 PE.coords_01_1985 - extract(RR_1985_1, coords, df=T)
 PE.coords_01_1986 - extract(RR_1986_1, coords, df=T)
 PE.coords_01_1987 - extract(RR_1987_1, coords, df=T)
 PE.coords_01_1988 - extract(RR_1988_1, coords, df=T)
 PE.coords_01_1989 - extract(RR_1989_1, coords, df=T)
 PE.coords_01_1990 - extract(RR_1990_1, coords, df=T)
 
 
 # This is one of the things I've tried.
 
 for(i in 1981:2010){
 file - c(paste(RR_, i, _1, sep=))
 extract - extract(file, coords, df=T)}
 names.a - paste(PE.coords_01_, i, sep=)
 assign(names.a, value=extract)
 }
 
 # I get the following error.
 Error in (function (classes, fdef, mtable) :
 unable to find an inherited method for function 'extract'
 for signature 'character, SpatialPointsDataFrame'
 # I think the error must be something when I'm defining 'file'
 
 Thanks a lot for any help!
 
 __
 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] Loop to extract from variables in the workspace

2014-04-21 Thread Beatriz R. Gonzalez Dominguez

Hi Frede,

Many thanks for your reply.
1. The first argument in extract is a Formal class RasterLayer in the 
Workspace (e.g RR_1981_1 ).


2. I created an intermediate name to hold the result fromthe extract 
function because I'd like to create several dataframes with the output 
of the iterative (loop) extraction. I'd like to get the same result as 
when I do:


PE.coords_01_1981 - extract(RR_1981_1, coords, df=T)
PE.coords_01_1982 - extract(RR_1982_1, coords, df=T)
PE.coords_01_1983 - extract(RR_1983_1, coords, df=T)
PE.coords_01_1984 - extract(RR_1984_1, coords, df=T)
[... this works no problem]

3. 'coords' is a SpatialPointsDataFrame.

4. I used assign in the loop becuase I thought it was the way forward to 
create new variables out of it. Isn't it?


What I'd like to do is to use coordinate points ('coords') to extract 
raster pixel values (eg. 'RR_1981_1') on which the points are overlying. 
Then I'd like to build a bigger data frame including the data from all 
the outputs (i.e. PE.coords_01_1981, PE.coords_01_1982).


Hope to have explained myself properly. Please let me know if anything 
else should be clarified.


Best wishes,

Bea


On 21/04/2014 15:17, Frede Aakmann Tøgersen wrote:

Hi Beatriz

Did you read the help for extract{raster} carefully?

Several things can be wrong.

1) First argument to extract is not a file name but a raster object.
2) In the loop you name an object extract as an intermediate name to hold the 
result from the extract function. Do you think there could be a name clash? R 
is clever but perhaps not clever enough.
3) coords are of the right class (see ?extract).
4) assign can be useful from time to time. But in a loop?

I think the things you are doing are some intermediate results that needs more 
processing. Do you think this is the right way to do that. For instance instead 
of storing the immediate result as separate objects why not store those in a 
list.

Perhaps if you tell us what you would like to do  overall, i.e. from first to 
last, then we will be able to help you to become more efficient.


Yours sincerely / Med venlig hilsen


Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance  Modeling

Technology  Service Solutions
T +45 9730 5135
M +45 2547 6050
fr...@vestas.com
http://www.vestas.com

Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice
If you have received this e-mail in error please contact the sender.



-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
On Behalf Of Beatriz R. Gonzalez Dominguez
Sent: 21. april 2014 14:53
To: r-help@r-project.org
Subject: [R] Loop to extract from variables in the workspace

Dear all,

I'm starting to work with loops and I'm stucked on something.
I've been searching and trying different possibilities but I don't get
to the solution.
I'd be very grateful if you could share any ideas that you think may help.

library(raster)

# All my variables are in the workspace

# This is what I'd like to obtain, but with a loop (I'm working with
several years and variables).
PE.coords_01_1981 - extract(RR_1981_1, coords, df=T)
PE.coords_01_1982 - extract(RR_1982_1, coords, df=T)
PE.coords_01_1983 - extract(RR_1983_1, coords, df=T)
PE.coords_01_1984 - extract(RR_1984_1, coords, df=T)
PE.coords_01_1985 - extract(RR_1985_1, coords, df=T)
PE.coords_01_1986 - extract(RR_1986_1, coords, df=T)
PE.coords_01_1987 - extract(RR_1987_1, coords, df=T)
PE.coords_01_1988 - extract(RR_1988_1, coords, df=T)
PE.coords_01_1989 - extract(RR_1989_1, coords, df=T)
PE.coords_01_1990 - extract(RR_1990_1, coords, df=T)


# This is one of the things I've tried.

for(i in 1981:2010){
file - c(paste(RR_, i, _1, sep=))
extract - extract(file, coords, df=T)}
names.a - paste(PE.coords_01_, i, sep=)
assign(names.a, value=extract)
}

# I get the following error.
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function 'extract'
for signature 'character, SpatialPointsDataFrame'
# I think the error must be something when I'm defining 'file'

Thanks a lot for any help!

__
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] Loop to extract from variables in the workspace

2014-04-21 Thread Frede Aakmann Tøgersen

Well, sticking to your loop that you seem comfortable with try this:

## list to hold dataframes
extracList - vector(list, length(1981:2010))

cnt - 0
for(i in 1981:2010){
cnt - cnt + 1
rasterObj -get( c(paste(RR_, i, _1, sep=)))
extractList[[cnt]]- extract(rasterObj, coords, df=T)}
 }

See ?get which is kind of the opposite of assign.

To coerce the list of dataframes into one dataframe several methods are 
available:

do.call(rbind, extractList)

plyr::rbind.fill(extractList)

plyr::ldply(extractList, data.frame)

as.data.frame(data.table::rbindlist(extractList))

This was taken from 
http://stackoverflow.com/questions/2851327/converting-a-list-of-data-frames-into-one-data-frame-in-r
 where you get get some pros and cons on the methods.

The fastest way will probably be to create a huge dataframe and during the loop 
calculated the row indices of that dataframe for each raster into the result 
from extract should be put.



Yours sincerely / Med venlig hilsen


Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance  Modeling

Technology  Service Solutions
T +45 9730 5135
M +45 2547 6050
fr...@vestas.com
http://www.vestas.com

Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice
If you have received this e-mail in error please contact the sender. 


 -Original Message-
 From: Beatriz R. Gonzalez Dominguez [mailto:aguitatie...@hotmail.com]
 Sent: 21. april 2014 16:27
 To: Frede Aakmann Tøgersen; r-help@r-project.org
 Subject: Re: [R] Loop to extract from variables in the workspace
 
 Hi Frede,
 
 Many thanks for your reply.
 1. The first argument in extract is a Formal class RasterLayer in the
 Workspace (e.g RR_1981_1 ).
 
 2. I created an intermediate name to hold the result fromthe extract
 function because I'd like to create several dataframes with the output
 of the iterative (loop) extraction. I'd like to get the same result as
 when I do:
 
 PE.coords_01_1981 - extract(RR_1981_1, coords, df=T)
 PE.coords_01_1982 - extract(RR_1982_1, coords, df=T)
 PE.coords_01_1983 - extract(RR_1983_1, coords, df=T)
 PE.coords_01_1984 - extract(RR_1984_1, coords, df=T)
 [... this works no problem]
 
 3. 'coords' is a SpatialPointsDataFrame.
 
 4. I used assign in the loop becuase I thought it was the way forward to
 create new variables out of it. Isn't it?
 
 What I'd like to do is to use coordinate points ('coords') to extract
 raster pixel values (eg. 'RR_1981_1') on which the points are overlying.
 Then I'd like to build a bigger data frame including the data from all
 the outputs (i.e. PE.coords_01_1981, PE.coords_01_1982).
 
 Hope to have explained myself properly. Please let me know if anything
 else should be clarified.
 
 Best wishes,
 
 Bea
 
 
 On 21/04/2014 15:17, Frede Aakmann Tøgersen wrote:
  Hi Beatriz
 
  Did you read the help for extract{raster} carefully?
 
  Several things can be wrong.
 
  1) First argument to extract is not a file name but a raster object.
  2) In the loop you name an object extract as an intermediate name to hold
 the result from the extract function. Do you think there could be a name
 clash? R is clever but perhaps not clever enough.
  3) coords are of the right class (see ?extract).
  4) assign can be useful from time to time. But in a loop?
 
  I think the things you are doing are some intermediate results that needs
 more processing. Do you think this is the right way to do that. For instance
 instead of storing the immediate result as separate objects why not store
 those in a list.
 
  Perhaps if you tell us what you would like to do  overall, i.e. from first 
  to
 last, then we will be able to help you to become more efficient.
 
 
  Yours sincerely / Med venlig hilsen
 
 
  Frede Aakmann Tøgersen
  Specialist, M.Sc., Ph.D.
  Plant Performance  Modeling
 
  Technology  Service Solutions
  T +45 9730 5135
  M +45 2547 6050
  fr...@vestas.com
  http://www.vestas.com
 
  Company reg. name: Vestas Wind Systems A/S
  This e-mail is subject to our e-mail disclaimer statement.
  Please refer to www.vestas.com/legal/notice
  If you have received this e-mail in error please contact the sender.
 
 
  -Original Message-
  From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org]
  On Behalf Of Beatriz R. Gonzalez Dominguez
  Sent: 21. april 2014 14:53
  To: r-help@r-project.org
  Subject: [R] Loop to extract from variables in the workspace
 
  Dear all,
 
  I'm starting to work with loops and I'm stucked on something.
  I've been searching and trying different possibilities but I don't get
  to the solution.
  I'd be very grateful if you could share any ideas that you think may help.
 
  library(raster)
 
  # All my variables are in the workspace
 
  # This is what I'd like to obtain, but with a loop (I'm working with
  several years and variables).
  PE.coords_01_1981 - extract(RR_1981_1, coords, df=T

Re: [R] Loop through columns of outcomes

2013-11-12 Thread Rui Barradas

Hello,

Use nested lapply(). Like this:



m1 - lapply(varlist0,function(v) {
lapply(outcomes, function(o){
		f - sprintf(%s~ s(time,bs='cr',k=200)+s(temp,bs='cr') + 
Lag(%s,0:6), o, v)

gam(as.formula(f),family=quasipoisson,na.action=na.omit,data=df)
  })})

m1 - unlist(m1, recursive = FALSE)
m1


Hope this helps,

Rui Barradas


Em 12-11-2013 09:53, Kuma Raj escreveu:

I have asked this question on SO, but it attracted no response, thus I am
cross- posting it here with the hope that someone would help.

I want to estimate the effect of  pm10 and o3 on three outcome(death, cvd
and resp). What I want to do is run one model for each of the main
predictors  (pm10 and o3) and each outcome(death, cvd and resp). Thus I
expect to obtain 6 models. The script below works for one outcome (death)
and I wish to use it for more dependent variables.



library(quantmod)
library(mgcv)
library(dlnm)
df - chicagoNMMAPS
outcomes- c(death, cvd, resp )
varlist0 - c(pm10, o3)

 m1 - lapply(varlist0,function(v) {
 f - sprintf(death~ s(time,bs='cr',k=200)+s(temp,bs='cr') +
Lag(%s,0:6),v)
 gam(as.formula(f),family=quasipoisson,na.action=na.omit,data=df)
   })

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.



__
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] Loop through columns of outcomes

2013-11-12 Thread Kuma Raj
Thanks for the script which works perfectly. I am interested to do
model checking and also interested to extract the coefficients for
linear and spline terms. For model checkup I could run this script
which will give different plots to test model fit: gam.check(m2[[1]]).
Thanks to mnel from SO I could also extract the linear terms with the
following script:

m2 - unlist(m1, recursive = FALSE)   ## unlist

First extract the model elements:

mod1-m2[[1]]
mod2-m2[[2]]
mod3-m2[[3]]
mod4-m2[[4]]
mod5-m2[[5]]
mod6-m2[[6]]

And run the following:

mlist - list(mod1, mod2, mod3,mod4,mod5,mod6)  ##  Creates a list of models
names(mlist) - list(mod1, mod2, mod3,mod4,mod5,mod6)

 slist - lapply(mlist, summary)   ## obtain summaries

plist - lapply(slist, `[[`, 'p.table')   ## list of the coefficients
linear terms

For 6 models this is relatively easy to do, but how could I shorten
the process if I have large number of models?

Thanks


On 12 November 2013 12:32, Rui Barradas ruipbarra...@sapo.pt wrote:
 Hello,

 Use nested lapply(). Like this:



 m1 - lapply(varlist0,function(v) {
 lapply(outcomes, function(o){
 f - sprintf(%s~ s(time,bs='cr',k=200)+s(temp,bs='cr') +
 Lag(%s,0:6), o, v)

 gam(as.formula(f),family=quasipoisson,na.action=na.omit,data=df)
   })})

 m1 - unlist(m1, recursive = FALSE)
 m1


 Hope this helps,

 Rui Barradas


 Em 12-11-2013 09:53, Kuma Raj escreveu:

 I have asked this question on SO, but it attracted no response, thus I am
 cross- posting it here with the hope that someone would help.

 I want to estimate the effect of  pm10 and o3 on three outcome(death, cvd
 and resp). What I want to do is run one model for each of the main
 predictors  (pm10 and o3) and each outcome(death, cvd and resp). Thus I
 expect to obtain 6 models. The script below works for one outcome (death)
 and I wish to use it for more dependent variables.



 library(quantmod)
 library(mgcv)
 library(dlnm)
 df - chicagoNMMAPS
 outcomes- c(death, cvd, resp )
 varlist0 - c(pm10, o3)

  m1 - lapply(varlist0,function(v) {
  f - sprintf(death~ s(time,bs='cr',k=200)+s(temp,bs='cr') +
 Lag(%s,0:6),v)
  gam(as.formula(f),family=quasipoisson,na.action=na.omit,data=df)
})

 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.



__
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] Loop through columns of outcomes

2013-11-12 Thread Rui Barradas

Hello,

Once again, use lapply.

mlist - lapply(seq_along(m2), function(i) m2[[i]])
names(mlist) - paste0(mod, seq_along(mlist))

slist - lapply(mlist, summary)

plist - lapply(slist, `[[`, 'p.table')


Hope this helps,

Rui Barradas

Em 12-11-2013 13:28, Kuma Raj escreveu:

Thanks for the script which works perfectly. I am interested to do
model checking and also interested to extract the coefficients for
linear and spline terms. For model checkup I could run this script
which will give different plots to test model fit: gam.check(m2[[1]]).
Thanks to mnel from SO I could also extract the linear terms with the
following script:

m2 - unlist(m1, recursive = FALSE)   ## unlist

First extract the model elements:

mod1-m2[[1]]
mod2-m2[[2]]
mod3-m2[[3]]
mod4-m2[[4]]
mod5-m2[[5]]
mod6-m2[[6]]

And run the following:

mlist - list(mod1, mod2, mod3,mod4,mod5,mod6)  ##  Creates a list of models
names(mlist) - list(mod1, mod2, mod3,mod4,mod5,mod6)

  slist - lapply(mlist, summary)   ## obtain summaries

plist - lapply(slist, `[[`, 'p.table')   ## list of the coefficients
linear terms

For 6 models this is relatively easy to do, but how could I shorten
the process if I have large number of models?

Thanks


On 12 November 2013 12:32, Rui Barradas ruipbarra...@sapo.pt wrote:

Hello,

Use nested lapply(). Like this:



m1 - lapply(varlist0,function(v) {
 lapply(outcomes, function(o){
 f - sprintf(%s~ s(time,bs='cr',k=200)+s(temp,bs='cr') +
Lag(%s,0:6), o, v)

gam(as.formula(f),family=quasipoisson,na.action=na.omit,data=df)
   })})

m1 - unlist(m1, recursive = FALSE)
m1


Hope this helps,

Rui Barradas


Em 12-11-2013 09:53, Kuma Raj escreveu:


I have asked this question on SO, but it attracted no response, thus I am
cross- posting it here with the hope that someone would help.

I want to estimate the effect of  pm10 and o3 on three outcome(death, cvd
and resp). What I want to do is run one model for each of the main
predictors  (pm10 and o3) and each outcome(death, cvd and resp). Thus I
expect to obtain 6 models. The script below works for one outcome (death)
and I wish to use it for more dependent variables.



library(quantmod)
library(mgcv)
library(dlnm)
df - chicagoNMMAPS
outcomes- c(death, cvd, resp )
varlist0 - c(pm10, o3)

  m1 - lapply(varlist0,function(v) {
  f - sprintf(death~ s(time,bs='cr',k=200)+s(temp,bs='cr') +
Lag(%s,0:6),v)
  gam(as.formula(f),family=quasipoisson,na.action=na.omit,data=df)
})

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.





__
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] Loop through columns of outcomes

2013-11-12 Thread Kuma Raj
Very helpful, many thanks.

On 12 November 2013 16:09, Rui Barradas ruipbarra...@sapo.pt wrote:
 Hello,

 Once again, use lapply.

 mlist - lapply(seq_along(m2), function(i) m2[[i]])
 names(mlist) - paste0(mod, seq_along(mlist))

 slist - lapply(mlist, summary)


 plist - lapply(slist, `[[`, 'p.table')


 Hope this helps,

 Rui Barradas

 Em 12-11-2013 13:28, Kuma Raj escreveu:

 Thanks for the script which works perfectly. I am interested to do
 model checking and also interested to extract the coefficients for
 linear and spline terms. For model checkup I could run this script
 which will give different plots to test model fit: gam.check(m2[[1]]).
 Thanks to mnel from SO I could also extract the linear terms with the
 following script:

 m2 - unlist(m1, recursive = FALSE)   ## unlist

 First extract the model elements:

 mod1-m2[[1]]
 mod2-m2[[2]]
 mod3-m2[[3]]
 mod4-m2[[4]]
 mod5-m2[[5]]
 mod6-m2[[6]]

 And run the following:

 mlist - list(mod1, mod2, mod3,mod4,mod5,mod6)  ##  Creates a list of
 models
 names(mlist) - list(mod1, mod2, mod3,mod4,mod5,mod6)

   slist - lapply(mlist, summary)   ## obtain summaries

 plist - lapply(slist, `[[`, 'p.table')   ## list of the coefficients
 linear terms

 For 6 models this is relatively easy to do, but how could I shorten
 the process if I have large number of models?

 Thanks


 On 12 November 2013 12:32, Rui Barradas ruipbarra...@sapo.pt wrote:

 Hello,

 Use nested lapply(). Like this:



 m1 - lapply(varlist0,function(v) {
  lapply(outcomes, function(o){
  f - sprintf(%s~ s(time,bs='cr',k=200)+s(temp,bs='cr')
 +
 Lag(%s,0:6), o, v)

 gam(as.formula(f),family=quasipoisson,na.action=na.omit,data=df)
})})

 m1 - unlist(m1, recursive = FALSE)
 m1


 Hope this helps,

 Rui Barradas


 Em 12-11-2013 09:53, Kuma Raj escreveu:


 I have asked this question on SO, but it attracted no response, thus I
 am
 cross- posting it here with the hope that someone would help.

 I want to estimate the effect of  pm10 and o3 on three outcome(death,
 cvd
 and resp). What I want to do is run one model for each of the main
 predictors  (pm10 and o3) and each outcome(death, cvd and resp). Thus I
 expect to obtain 6 models. The script below works for one outcome
 (death)
 and I wish to use it for more dependent variables.



 library(quantmod)
 library(mgcv)
 library(dlnm)
 df - chicagoNMMAPS
 outcomes- c(death, cvd, resp )
 varlist0 - c(pm10, o3)

   m1 - lapply(varlist0,function(v) {
   f - sprintf(death~ s(time,bs='cr',k=200)+s(temp,bs='cr') +
 Lag(%s,0:6),v)

 gam(as.formula(f),family=quasipoisson,na.action=na.omit,data=df)
 })

 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.




__
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] Loop for R

2013-10-23 Thread Jeff Newmiller
As the Posting Guide ( http://www.R-project.org/posting-guide.html) indicates, 
this is not a homework help forum. Please use the resources provided by your 
educational institution for assistance.
---
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.

THIRU MANIAM thiru_maniam2...@yahoo.com wrote:
Hi,
I need kind help from you. I'm doing my assignment in IR and need to do
script in R programming and using R studio tool.I don't have any
knowledge in R but learning by Youtube. After so long,i successfully
came out with below script for precision 10(P@)
But i don know how to do loop for 105 system. below script was for 5
system but i define it  one by one.Can you help me ?




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


  1   2   3   4   5   >