Re: [R] Defining partial list of variables

2021-01-05 Thread Bert Gunter
I may not I properly understand the context of this discussion, and, in
particular what the my.formula() function does. But if I do, the following,
from ?formula, seems relevant and would indicate that the discussion is
unnecessary:

"There are two special interpretations of . in a formula. The usual one is
in the context of a data argument of model fitting functions and means ‘all
columns not otherwise in the formula’:"

This means you can fit different models just by indexing the columns -- by
number --  you wish to use in a data argument, viz:

y <- runif(100)
dat <- data.frame(matrix(runif(500), ncol = 5))
names(dat) <- letters[1:5]
head(dat)

## Use columns 1,3, and 5 only
mdl1 <- lm(y ~ ., data = dat[,c(1,3,5)])

## Result:
 summary(mdl1)

Call:
lm(formula = y ~ ., data = dat[, c(1, 3, 5)])

Residuals:
 Min   1Q   Median   3Q  Max
-0.52334 -0.27494  0.01245  0.28637  0.51998

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)  0.514610.08236   6.248 1.14e-08 ***
a0.015160.10928   0.1390.890
c0.035170.10399   0.3380.736
e   -0.094370.10967  -0.8610.392
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.299 on 96 degrees of freedom
Multiple R-squared:  0.008256, Adjusted R-squared:  -0.02274
F-statistic: 0.2664 on 3 and 96 DF,  p-value: 0.8495


If I have misunderstood and this is unhelpful, just ignore without comment.
You don't need to waste time explaining it to me.

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 Tue, Jan 5, 2021 at 4:49 AM Heinz Tuechler  wrote:

> What about the Cs()-function in Hmisc?
> library(Hmisc)
> Cs(a,b,c)
> [1] "a" "b" "c"
>
> Steven Yen wrote/hat geschrieben on/am 05.01.2021 13:29:
> > Thanks Eric. Yes, "unlist" makes a difference. Below, I am doing not
> > regression but summary to keep the example simple.
> >
> >  > set.seed(123)
> >  > data<-matrix(runif(1:25),nrow=5)
> >  > colnames(data)<-c("x1","x2","x3","x4","x5"); data
> >  x1x2x3 x4x5
> > [1,] 0.2875775 0.0455565 0.9568333 0.89982497 0.8895393
> > [2,] 0.7883051 0.5281055 0.4533342 0.24608773 0.6928034
> > [3,] 0.4089769 0.8924190 0.6775706 0.04205953 0.6405068
> > [4,] 0.8830174 0.5514350 0.5726334 0.32792072 0.9942698
> > [5,] 0.9404673 0.4566147 0.1029247 0.95450365 0.6557058
> >  > j<-strsplit(gsub("[\n ]","","x1,x3,x5"),",")
> >  > j<-unlist(j); j
> > [1] "x1" "x3" "x5"
> >  > summary(data[,j])
> > x1   x3   x5
> >   Min.   :0.2876   Min.   :0.1029   Min.   :0.6405
> >   1st Qu.:0.4090   1st Qu.:0.4533   1st Qu.:0.6557
> >   Median :0.7883   Median :0.5726   Median :0.6928
> >   Mean   :0.6617   Mean   :0.5527   Mean   :0.7746
> >   3rd Qu.:0.8830   3rd Qu.:0.6776   3rd Qu.:0.8895
> >   Max.   :0.9405   Max.   :0.9568   Max.   :0.9943
> >
> > On 2021/1/5 下午 07:08, Eric Berger wrote:
> >> wrap it in unlist
> >>
> >> xx <- unlist(strsplit(  ))
> >>
> >>
> >>
> >> On Tue, Jan 5, 2021 at 12:59 PM Steven Yen  >> > wrote:
> >>
> >> Thanks Eric. Perhaps I should know when to stop. The approach
> >> produces a slightly different variable list (note the [[1]]).
> >> Consequently, I was not able to use xx in defining my regression
> >> formula.
> >>
> >> > x<-colnames(subset(mydata,select=c(
> >>
> >> +hhsize,urban,male,
> >> +age3045,age4659,age60, # age1529
> >> +highsc,tert,   # primary
> >> +gov,nongov,# unemp
> >> +married))); x
> >>  [1] "hhsize"  "urban"   "male""age3045" "age4659" "age60"
> >> "highsc"  "tert"
> >>  [9] "gov" "nongov"  "married"
> >> > xx<-strsplit(gsub("[\n ]","",
> >> +"hhsize,urban,male,
> >> + age3045,age4659,age60,
> >> + highsc,tert,
> >> + gov,nongov,
> >> + married"
> >> + ),","); xx
> >> [[1]]
> >>  [1] "hhsize"  "urban"   "male""age3045" "age4659" "age60"
> >> "highsc"  "tert"
> >>  [9] "gov" "nongov"  "married"
> >>
> >> > eq1<-my.formula(y="cig",x=x); eq1
> >> cig ~ hhsize + urban + male + age3045 + age4659 + age60 + highsc +
> >> tert + gov + nongov + married
> >> > eq2<-my.formula(y="cig",x=xx); eq2
> >> cig ~ c("hhsize", "urban", "male", "age3045", "age4659", "age60",
> >> "highsc", "tert", "gov", "nongov", "married")
> >>
> >> On 2021/1/5 下午 06:01, Eric Berger wrote:
> >>> If your column names have no spaces the following should work
> >>>
> >>>  x<-strsplit(gsub("[\n ]","",
> >>>  "hhsize,urban,male,
> >>> + gov,nongov,married"),","); x
> >>>
> >>> On Tue, Jan 5, 2021 at 11:47 AM Steven Yen  >>> > wrote:
> >>>
> >>> Here we go! BUT, it

[R] Error Running Bootstrap Function within Wrapper Function

2021-01-05 Thread Kevin Egan
Hello,

I am currently trying to solve a problem with the boot package and writing
a function within a function in R. I have developed several functions to
perform the lasso but continue to receive an error when bootstrapping these
functions within a wrapper function. When I perform these methods using
tsboot outside the wrapper function, I do not get an error. However, when
placed within my function I continue to get the error "Error in t[r, ] <-
res[[r]] : number of items to replace is not a multiple of length."

I've attached an example of my functions as well as a data file of the data
I am using. I'm sorry the file is so large, but I do not get a problem with
a smaller number of observations.


library(boot)
library(glmnet)
library(np)
foo1 <- function(data,index){ #index is the bootstrap sample index
  x <- data[index, -1] %>%
as.matrix() %>%
unname()
  y <- data[index, 1] %>%
scale(center = TRUE, scale = FALSE) %>%
as.matrix() %>%
unname()
  ols <- lm(y ~ x)
  # The intercept estimate should be dropped.
  ols.coef <- as.numeric(coef(ols))[-1]
  ols.coef[is.na(ols.coef)] <- 0
  ## The intercept estimate should be dropped.
  lasso <- cv.glmnet(x, y, alpha = 1,
 penalty.factor = 1 / abs(ols.coef))
  # Select nonzero coefficients from bic.out
  coef <- as.vector(coef(lasso,
 s = lasso$lambda.min))[-1]
  return(coef)
}
foo2 <- function(data, index){ #index is the bootstrap sample index
  x <- data[index, -1] %>%
as.matrix() %>%
unname()
  y <- data[index, 1] %>%
scale(center = TRUE, scale = FALSE) %>%
as.matrix() %>%
unname()
  # ic.glmnet provides coefficients with lowest BIC
  ols <- lm(y ~ x)
  # The intercept estimate should be dropped.
  ols.coef <- as.numeric(coef(ols))[-1]
  ols.coef[is.na(ols.coef)] <- 0
  lasso <- cv.glmnet(x, y, alpha = 1,
 penalty.factor = 1 / abs(ols.coef))
  # Select nonzero coefficients from bic.out
  coef <- as.vector(coef(lasso,
 s = lasso$lambda.min))[-1]
  coef_nonzero <- coef != 0
  if(sum(coef_nonzero) > 0) {
ls_obj <- lm(y ~ x[, coef_nonzero, drop = FALSE])
ls_coef <- as.vector(coef(ls_obj))[-1]
coef[coef_nonzero] <- ls_coef
  }
  return(coef)
}
foo3 <- function(data, num_samples) {
  bstar <- b.star(data[, 1], round = TRUE)
  # Select Block Length of circular block result
  blocklength <- bstar[, 2]
  init_boot_ts <- tsboot(tseries = data,
 statistic = foo1,
 R = num_samples, l = blocklength,
 sim = "fixed")
  final_boot_ts <- tsboot(tseries = data,
  statistic = foo2,
  R = num_samples,
  l = blocklength, sim = "fixed")
  # point estimates
  final_boot_t0 <- final_boot_ts$t0
  return(list(point_estimates = final_boot_t0))
}
num_samples <- 50
test.foo3 <- foo3(data, num_samples = num_samples)

Which *sometimes *works, however, sometimes I get the error: “Error in t[r,
] <- res[[r]] : number of items to replace is not a multiple of length".
I've also gotten the error "Error in x[, coef_nonzero, drop = FALSE]
:(subscript) logical subscript too long" at times, when running foo2 within
foo3. Which seems to be unclear as there should never be an index which is
greater than the number of columns in x.
As I stated, this error particularly occurs when I increase the number of
bootstrap samples I try to run, and only when I run foo3. When I run foo1
and foo2 separately, I don’t get an error at all. I’m wondering if there is
something I need to add to my function “foo3” to ensure that an error like
this doesn’t occur since I am calling several other functions within this
function, ie., tsboot, foo1, and foo2.

Lastly, although I have not provided an example here, I get an error for
this code when running with lapply for several dataframes similar to the
attached.

Again, I apologise for attaching such a large data frame but the function
seems to work with fewer observations. Perhaps it is a data issue?

Thanks
__
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] Defining partial list of variables

2021-01-05 Thread Heinz Tuechler

What about the Cs()-function in Hmisc?
library(Hmisc)
Cs(a,b,c)
[1] "a" "b" "c"

Steven Yen wrote/hat geschrieben on/am 05.01.2021 13:29:

Thanks Eric. Yes, "unlist" makes a difference. Below, I am doing not
regression but summary to keep the example simple.

 > set.seed(123)
 > data<-matrix(runif(1:25),nrow=5)
 > colnames(data)<-c("x1","x2","x3","x4","x5"); data
 x1x2x3 x4x5
[1,] 0.2875775 0.0455565 0.9568333 0.89982497 0.8895393
[2,] 0.7883051 0.5281055 0.4533342 0.24608773 0.6928034
[3,] 0.4089769 0.8924190 0.6775706 0.04205953 0.6405068
[4,] 0.8830174 0.5514350 0.5726334 0.32792072 0.9942698
[5,] 0.9404673 0.4566147 0.1029247 0.95450365 0.6557058
 > j<-strsplit(gsub("[\n ]","","x1,x3,x5"),",")
 > j<-unlist(j); j
[1] "x1" "x3" "x5"
 > summary(data[,j])
x1   x3   x5
  Min.   :0.2876   Min.   :0.1029   Min.   :0.6405
  1st Qu.:0.4090   1st Qu.:0.4533   1st Qu.:0.6557
  Median :0.7883   Median :0.5726   Median :0.6928
  Mean   :0.6617   Mean   :0.5527   Mean   :0.7746
  3rd Qu.:0.8830   3rd Qu.:0.6776   3rd Qu.:0.8895
  Max.   :0.9405   Max.   :0.9568   Max.   :0.9943

On 2021/1/5 下午 07:08, Eric Berger wrote:

wrap it in unlist

xx <- unlist(strsplit(  ))



On Tue, Jan 5, 2021 at 12:59 PM Steven Yen mailto:st...@ntu.edu.tw>> wrote:

Thanks Eric. Perhaps I should know when to stop. The approach
produces a slightly different variable list (note the [[1]]).
Consequently, I was not able to use xx in defining my regression
formula.

> x<-colnames(subset(mydata,select=c(

+hhsize,urban,male,
+age3045,age4659,age60, # age1529
+highsc,tert,   # primary
+gov,nongov,# unemp
+married))); x
 [1] "hhsize"  "urban"   "male""age3045" "age4659" "age60"
"highsc"  "tert"
 [9] "gov" "nongov"  "married"
> xx<-strsplit(gsub("[\n ]","",
+"hhsize,urban,male,
+ age3045,age4659,age60,
+ highsc,tert,
+ gov,nongov,
+ married"
+ ),","); xx
[[1]]
 [1] "hhsize"  "urban"   "male""age3045" "age4659" "age60"
"highsc"  "tert"
 [9] "gov" "nongov"  "married"

> eq1<-my.formula(y="cig",x=x); eq1
cig ~ hhsize + urban + male + age3045 + age4659 + age60 + highsc +
tert + gov + nongov + married
> eq2<-my.formula(y="cig",x=xx); eq2
cig ~ c("hhsize", "urban", "male", "age3045", "age4659", "age60",
"highsc", "tert", "gov", "nongov", "married")

On 2021/1/5 下午 06:01, Eric Berger wrote:

If your column names have no spaces the following should work

 x<-strsplit(gsub("[\n ]","",
 "hhsize,urban,male,
+ gov,nongov,married"),","); x

On Tue, Jan 5, 2021 at 11:47 AM Steven Yen mailto:st...@ntu.edu.tw>> wrote:

Here we go! BUT, it works great for a continuous line. With
line break(s), I got the nuisance "\n" inserted.

> x<-strsplit("hhsize,urban,male,gov,nongov,married",","); x
[[1]]
[1] "hhsize"  "urban"   "male""gov" "nongov"  "married"

> x<-strsplit("hhsize,urban,male,
+ gov,nongov,married",","); x
[[1]]
[1] "hhsize""urban" "male"
"\ngov"
[5] "nongov""married"

On 2021/1/5 下午 05:34, Eric Berger wrote:


zx<-strsplit("age,exercise,income,white,black,hispanic,base,somcol,grad,employed,unable,homeowner,married,divorced,widowed",",")



On Tue, Jan 5, 2021 at 11:01 AM Steven Yen mailto:st...@ntu.edu.tw>> wrote:

Thank you, Jeff. IMO, we are all here to make R work
better to suit our
various needs. All I am asking is an easier way to
define variable list
zx, differently from the way z0 , x0, and treat are defined.

 > zx<-colnames(subset(mydata,select=c(
+
age,exercise,income,white,black,hispanic,base,somcol,grad,employed,
+ unable,homeowner,married,divorced,widowed)))
 > z0<-c("fruit","highblood")
 > x0<-c("vgood","poor")
 > treat<-"depression"
 > eq1 <-my.formula(y="depression",x=zx,z0)
 > eq2 <-my.formula(y="bmi", x=zx,x0)
 > eq2t<-my.formula(y="bmi", x=zx,treat)
 > eqs<-list(eq1,eq2); eqs
[[1]]
depression ~ age + exercise + income + white + black +
hispanic +
 base + somcol + grad + employed + unable +
homeowner + married +
 divorced + widowed + fruit + highblood

[[2]]
bmi ~ age + exercise + income + white + black + hispanic
+ base +
 somcol + grad + employed + unable + homeowner +
married +
 divorced + widowed + vgood + poor

 > eqt<-list(eq1,eq2t); eqt
[[1]]
depression ~ age + exercise + income + white +

Re: [R] Defining partial list of variables

2021-01-05 Thread Steven Yen
Thanks Eric. Yes, "unlist" makes a difference. Below, I am doing not 
regression but summary to keep the example simple.

 > set.seed(123)
 > data<-matrix(runif(1:25),nrow=5)
 > colnames(data)<-c("x1","x2","x3","x4","x5"); data
     x1    x2    x3 x4    x5
[1,] 0.2875775 0.0455565 0.9568333 0.89982497 0.8895393
[2,] 0.7883051 0.5281055 0.4533342 0.24608773 0.6928034
[3,] 0.4089769 0.8924190 0.6775706 0.04205953 0.6405068
[4,] 0.8830174 0.5514350 0.5726334 0.32792072 0.9942698
[5,] 0.9404673 0.4566147 0.1029247 0.95450365 0.6557058
 > j<-strsplit(gsub("[\n ]","","x1,x3,x5"),",")
 > j<-unlist(j); j
[1] "x1" "x3" "x5"
 > summary(data[,j])
    x1   x3   x5
  Min.   :0.2876   Min.   :0.1029   Min.   :0.6405
  1st Qu.:0.4090   1st Qu.:0.4533   1st Qu.:0.6557
  Median :0.7883   Median :0.5726   Median :0.6928
  Mean   :0.6617   Mean   :0.5527   Mean   :0.7746
  3rd Qu.:0.8830   3rd Qu.:0.6776   3rd Qu.:0.8895
  Max.   :0.9405   Max.   :0.9568   Max.   :0.9943

On 2021/1/5 下午 07:08, Eric Berger wrote:
> wrap it in unlist
>
> xx <- unlist(strsplit(  ))
>
>
>
> On Tue, Jan 5, 2021 at 12:59 PM Steven Yen  > wrote:
>
> Thanks Eric. Perhaps I should know when to stop. The approach
> produces a slightly different variable list (note the [[1]]).
> Consequently, I was not able to use xx in defining my regression
> formula.
>
> > x<-colnames(subset(mydata,select=c(
>
> +    hhsize,urban,male,
> +    age3045,age4659,age60, # age1529
> +    highsc,tert,   # primary
> +    gov,nongov,    # unemp
> +    married))); x
>  [1] "hhsize"  "urban"   "male"    "age3045" "age4659" "age60"  
> "highsc"  "tert"
>  [9] "gov" "nongov"  "married"
> > xx<-strsplit(gsub("[\n ]","",
> +    "hhsize,urban,male,
> + age3045,age4659,age60,
> + highsc,tert,
> + gov,nongov,
> + married"
> + ),","); xx
> [[1]]
>  [1] "hhsize"  "urban"   "male"    "age3045" "age4659" "age60"  
> "highsc"  "tert"
>  [9] "gov" "nongov"  "married"
>
> > eq1<-my.formula(y="cig",x=x); eq1
> cig ~ hhsize + urban + male + age3045 + age4659 + age60 + highsc +
>     tert + gov + nongov + married
> > eq2<-my.formula(y="cig",x=xx); eq2
> cig ~ c("hhsize", "urban", "male", "age3045", "age4659", "age60",
>     "highsc", "tert", "gov", "nongov", "married")
>
> On 2021/1/5 下午 06:01, Eric Berger wrote:
>> If your column names have no spaces the following should work
>>
>>  x<-strsplit(gsub("[\n ]","",
>>  "hhsize,urban,male,
>> + gov,nongov,married"),","); x
>>
>> On Tue, Jan 5, 2021 at 11:47 AM Steven Yen > > wrote:
>>
>> Here we go! BUT, it works great for a continuous line. With
>> line break(s), I got the nuisance "\n" inserted.
>>
>> > x<-strsplit("hhsize,urban,male,gov,nongov,married",","); x
>> [[1]]
>> [1] "hhsize"  "urban"   "male"    "gov" "nongov"  "married"
>>
>> > x<-strsplit("hhsize,urban,male,
>> + gov,nongov,married",","); x
>> [[1]]
>> [1] "hhsize"    "urban" "male" 
>> "\n    gov"
>> [5] "nongov"    "married"
>>
>> On 2021/1/5 下午 05:34, Eric Berger wrote:
>>> 
>>> zx<-strsplit("age,exercise,income,white,black,hispanic,base,somcol,grad,employed,unable,homeowner,married,divorced,widowed",",")
>>>
>>>
>>>
>>> On Tue, Jan 5, 2021 at 11:01 AM Steven Yen >> > wrote:
>>>
>>> Thank you, Jeff. IMO, we are all here to make R work
>>> better to suit our
>>> various needs. All I am asking is an easier way to
>>> define variable list
>>> zx, differently from the way z0 , x0, and treat are defined.
>>>
>>>  > zx<-colnames(subset(mydata,select=c(
>>> +
>>> 
>>> age,exercise,income,white,black,hispanic,base,somcol,grad,employed,
>>> + unable,homeowner,married,divorced,widowed)))
>>>  > z0<-c("fruit","highblood")
>>>  > x0<-c("vgood","poor")
>>>  > treat<-"depression"
>>>  > eq1 <-my.formula(y="depression",x=zx,z0)
>>>  > eq2 <-my.formula(y="bmi", x=zx,x0)
>>>  > eq2t<-my.formula(y="bmi", x=zx,treat)
>>>  > eqs<-list(eq1,eq2); eqs
>>> [[1]]
>>> depression ~ age + exercise + income + white + black +
>>> hispanic +
>>>  base + somcol + grad + employed + unable +
>>> homeowner + married +
>>>  divorced + widowed + fruit + highblood
>>>
>>> [[2]]
>>> bmi ~ age + exercise + income + white + black + hispanic
>>> + base +
>>>  somcol + grad + employed + unable + homeowner +
>>>

Re: [R] Defining partial list of variables

2021-01-05 Thread Eric Berger
wrap it in unlist

xx <- unlist(strsplit(  ))



On Tue, Jan 5, 2021 at 12:59 PM Steven Yen  wrote:

> Thanks Eric. Perhaps I should know when to stop. The approach produces a
> slightly different variable list (note the [[1]]). Consequently, I was not
> able to use xx in defining my regression formula.
>
> > x<-colnames(subset(mydata,select=c(
>
> +hhsize,urban,male,
> +age3045,age4659,age60, # age1529
> +highsc,tert,   # primary
> +gov,nongov,# unemp
> +married))); x
>  [1] "hhsize"  "urban"   "male""age3045" "age4659" "age60"   "highsc"
> "tert"
>  [9] "gov" "nongov"  "married"
> > xx<-strsplit(gsub("[\n ]","",
> +"hhsize,urban,male,
> + age3045,age4659,age60,
> + highsc,tert,
> + gov,nongov,
> + married"
> + ),","); xx
> [[1]]
>  [1] "hhsize"  "urban"   "male""age3045" "age4659" "age60"   "highsc"
> "tert"
>  [9] "gov" "nongov"  "married"
>
> > eq1<-my.formula(y="cig",x=x); eq1
> cig ~ hhsize + urban + male + age3045 + age4659 + age60 + highsc +
> tert + gov + nongov + married
> > eq2<-my.formula(y="cig",x=xx); eq2
> cig ~ c("hhsize", "urban", "male", "age3045", "age4659", "age60",
> "highsc", "tert", "gov", "nongov", "married")
>
> On 2021/1/5 下午 06:01, Eric Berger wrote:
>
> If your column names have no spaces the following should work
>
>  x<-strsplit(gsub("[\n ]","",
>  "hhsize,urban,male,
> + gov,nongov,married"),","); x
>
> On Tue, Jan 5, 2021 at 11:47 AM Steven Yen  wrote:
>
>> Here we go! BUT, it works great for a continuous line. With line
>> break(s), I got the nuisance "\n" inserted.
>>
>> > x<-strsplit("hhsize,urban,male,gov,nongov,married",","); x
>> [[1]]
>> [1] "hhsize"  "urban"   "male""gov" "nongov"  "married"
>>
>> > x<-strsplit("hhsize,urban,male,
>> + gov,nongov,married",","); x
>> [[1]]
>> [1] "hhsize""urban" "male"
>> "\ngov"
>> [5] "nongov""married"
>> On 2021/1/5 下午 05:34, Eric Berger wrote:
>>
>>
>> zx<-strsplit("age,exercise,income,white,black,hispanic,base,somcol,grad,employed,unable,homeowner,married,divorced,widowed",",")
>>
>>
>>
>> On Tue, Jan 5, 2021 at 11:01 AM Steven Yen  wrote:
>>
>>> Thank you, Jeff. IMO, we are all here to make R work better to suit our
>>> various needs. All I am asking is an easier way to define variable list
>>> zx, differently from the way z0 , x0, and treat are defined.
>>>
>>>  > zx<-colnames(subset(mydata,select=c(
>>> + age,exercise,income,white,black,hispanic,base,somcol,grad,employed,
>>> + unable,homeowner,married,divorced,widowed)))
>>>  > z0<-c("fruit","highblood")
>>>  > x0<-c("vgood","poor")
>>>  > treat<-"depression"
>>>  > eq1 <-my.formula(y="depression",x=zx,z0)
>>>  > eq2 <-my.formula(y="bmi",   x=zx,x0)
>>>  > eq2t<-my.formula(y="bmi",   x=zx,treat)
>>>  > eqs<-list(eq1,eq2); eqs
>>> [[1]]
>>> depression ~ age + exercise + income + white + black + hispanic +
>>>  base + somcol + grad + employed + unable + homeowner + married +
>>>  divorced + widowed + fruit + highblood
>>>
>>> [[2]]
>>> bmi ~ age + exercise + income + white + black + hispanic + base +
>>>  somcol + grad + employed + unable + homeowner + married +
>>>  divorced + widowed + vgood + poor
>>>
>>>  > eqt<-list(eq1,eq2t); eqt
>>> [[1]]
>>> depression ~ age + exercise + income + white + black + hispanic +
>>>  base + somcol + grad + employed + unable + homeowner + married +
>>>  divorced + widowed + fruit + highblood
>>>
>>> [[2]]
>>> bmi ~ age + exercise + income + white + black + hispanic + base +
>>>  somcol + grad + employed + unable + homeowner + married +
>>>  divorced + widowed + depression
>>>
>>> On 2021/1/5 下午 04:18, Jeff Newmiller wrote:
>>> > IMO if you want to hardcode a formula then simply hardcode a formula.
>>> If you want 20 formulas, write 20 formulas. Is that really so bad?
>>> >
>>> > If you want to have an abbreviated way to specify sets of variables
>>> without conforming to R syntax then put them into data files and read them
>>> in using a format of your choice.
>>> >
>>> > But using NSE to avoid using quotes for entering what amounts to
>>> in-script data is abuse of the language justified by laziness... the amount
>>> of work you put yourself and anyone else who reads your code through is
>>> excessive relative to the benefit gained.
>>> >
>>> > NSE has its strengths... but as a method of creating data objects it
>>> sucks. Note that even the tidyverse (now) requires you to use quotes when
>>> you are not directly referring to something that already exists. And if you
>>> were... you might as well be creating a formula.
>>> >
>>> > On January 4, 2021 11:14:54 PM PST, Steven Yen 
>>> wrote:
>>> >> I constantly define variable lists from a data frame (e.g., to define
>>> a
>>> >>
>>> >> regression equation). Line 3 below does just that. Placing each
>>> >> variable
>>> >> name in quotation marks is too much work especially fo

Re: [R] Defining partial list of variables

2021-01-05 Thread Steven Yen
Thanks Eric. Perhaps I should know when to stop. The approach produces a 
slightly different variable list (note the [[1]]). Consequently, I was 
not able to use xx in defining my regression formula.

 > x<-colnames(subset(mydata,select=c(

+    hhsize,urban,male,
+    age3045,age4659,age60, # age1529
+    highsc,tert,   # primary
+    gov,nongov,    # unemp
+    married))); x
  [1] "hhsize"  "urban"   "male"    "age3045" "age4659" "age60" 
"highsc"  "tert"
  [9] "gov" "nongov"  "married"
 > xx<-strsplit(gsub("[\n ]","",
+    "hhsize,urban,male,
+ age3045,age4659,age60,
+ highsc,tert,
+ gov,nongov,
+ married"
+ ),","); xx
[[1]]
  [1] "hhsize"  "urban"   "male"    "age3045" "age4659" "age60" 
"highsc"  "tert"
  [9] "gov" "nongov"  "married"

 > eq1<-my.formula(y="cig",x=x); eq1
cig ~ hhsize + urban + male + age3045 + age4659 + age60 + highsc +
     tert + gov + nongov + married
 > eq2<-my.formula(y="cig",x=xx); eq2
cig ~ c("hhsize", "urban", "male", "age3045", "age4659", "age60",
     "highsc", "tert", "gov", "nongov", "married")

On 2021/1/5 下午 06:01, Eric Berger wrote:
> If your column names have no spaces the following should work
>
>  x<-strsplit(gsub("[\n ]","",
>  "hhsize,urban,male,
> + gov,nongov,married"),","); x
>
> On Tue, Jan 5, 2021 at 11:47 AM Steven Yen  > wrote:
>
> Here we go! BUT, it works great for a continuous line. With line
> break(s), I got the nuisance "\n" inserted.
>
> > x<-strsplit("hhsize,urban,male,gov,nongov,married",","); x
> [[1]]
> [1] "hhsize"  "urban"   "male"    "gov" "nongov" "married"
>
> > x<-strsplit("hhsize,urban,male,
> + gov,nongov,married",","); x
> [[1]]
> [1] "hhsize"    "urban" "male"  "\n   
> gov"
> [5] "nongov"    "married"
>
> On 2021/1/5 下午 05:34, Eric Berger wrote:
>> 
>> zx<-strsplit("age,exercise,income,white,black,hispanic,base,somcol,grad,employed,unable,homeowner,married,divorced,widowed",",")
>>
>>
>>
>> On Tue, Jan 5, 2021 at 11:01 AM Steven Yen > > wrote:
>>
>> Thank you, Jeff. IMO, we are all here to make R work better
>> to suit our
>> various needs. All I am asking is an easier way to define
>> variable list
>> zx, differently from the way z0 , x0, and treat are defined.
>>
>>  > zx<-colnames(subset(mydata,select=c(
>> +
>> age,exercise,income,white,black,hispanic,base,somcol,grad,employed,
>> + unable,homeowner,married,divorced,widowed)))
>>  > z0<-c("fruit","highblood")
>>  > x0<-c("vgood","poor")
>>  > treat<-"depression"
>>  > eq1 <-my.formula(y="depression",x=zx,z0)
>>  > eq2 <-my.formula(y="bmi",   x=zx,x0)
>>  > eq2t<-my.formula(y="bmi",   x=zx,treat)
>>  > eqs<-list(eq1,eq2); eqs
>> [[1]]
>> depression ~ age + exercise + income + white + black + hispanic +
>>  base + somcol + grad + employed + unable + homeowner +
>> married +
>>  divorced + widowed + fruit + highblood
>>
>> [[2]]
>> bmi ~ age + exercise + income + white + black + hispanic + base +
>>  somcol + grad + employed + unable + homeowner + married +
>>  divorced + widowed + vgood + poor
>>
>>  > eqt<-list(eq1,eq2t); eqt
>> [[1]]
>> depression ~ age + exercise + income + white + black + hispanic +
>>  base + somcol + grad + employed + unable + homeowner +
>> married +
>>  divorced + widowed + fruit + highblood
>>
>> [[2]]
>> bmi ~ age + exercise + income + white + black + hispanic + base +
>>  somcol + grad + employed + unable + homeowner + married +
>>  divorced + widowed + depression
>>
>> On 2021/1/5 下午 04:18, Jeff Newmiller wrote:
>> > IMO if you want to hardcode a formula then simply hardcode
>> a formula. If you want 20 formulas, write 20 formulas. Is
>> that really so bad?
>> >
>> > If you want to have an abbreviated way to specify sets of
>> variables without conforming to R syntax then put them into
>> data files and read them in using a format of your choice.
>> >
>> > But using NSE to avoid using quotes for entering what
>> amounts to in-script data is abuse of the language justified
>> by laziness... the amount of work you put yourself and anyone
>> else who reads your code through is excessive relative to the
>> benefit gained.
>> >
>> > NSE has its strengths... but as a method of creating data
>> objects it sucks. Note that even the tidyverse (now) requires
>> you to use quotes when you are not directly referring to
>> something that already exists. And if you were... you might
>> as well be

Re: [R] Defining partial list of variables

2021-01-05 Thread Eric Berger
If your column names have no spaces the following should work

 x<-strsplit(gsub("[\n ]","",
 "hhsize,urban,male,
+ gov,nongov,married"),","); x

On Tue, Jan 5, 2021 at 11:47 AM Steven Yen  wrote:

> Here we go! BUT, it works great for a continuous line. With line break(s),
> I got the nuisance "\n" inserted.
>
> > x<-strsplit("hhsize,urban,male,gov,nongov,married",","); x
> [[1]]
> [1] "hhsize"  "urban"   "male""gov" "nongov"  "married"
>
> > x<-strsplit("hhsize,urban,male,
> + gov,nongov,married",","); x
> [[1]]
> [1] "hhsize""urban" "male"
> "\ngov"
> [5] "nongov""married"
> On 2021/1/5 下午 05:34, Eric Berger wrote:
>
>
> zx<-strsplit("age,exercise,income,white,black,hispanic,base,somcol,grad,employed,unable,homeowner,married,divorced,widowed",",")
>
>
>
> On Tue, Jan 5, 2021 at 11:01 AM Steven Yen  wrote:
>
>> Thank you, Jeff. IMO, we are all here to make R work better to suit our
>> various needs. All I am asking is an easier way to define variable list
>> zx, differently from the way z0 , x0, and treat are defined.
>>
>>  > zx<-colnames(subset(mydata,select=c(
>> + age,exercise,income,white,black,hispanic,base,somcol,grad,employed,
>> + unable,homeowner,married,divorced,widowed)))
>>  > z0<-c("fruit","highblood")
>>  > x0<-c("vgood","poor")
>>  > treat<-"depression"
>>  > eq1 <-my.formula(y="depression",x=zx,z0)
>>  > eq2 <-my.formula(y="bmi",   x=zx,x0)
>>  > eq2t<-my.formula(y="bmi",   x=zx,treat)
>>  > eqs<-list(eq1,eq2); eqs
>> [[1]]
>> depression ~ age + exercise + income + white + black + hispanic +
>>  base + somcol + grad + employed + unable + homeowner + married +
>>  divorced + widowed + fruit + highblood
>>
>> [[2]]
>> bmi ~ age + exercise + income + white + black + hispanic + base +
>>  somcol + grad + employed + unable + homeowner + married +
>>  divorced + widowed + vgood + poor
>>
>>  > eqt<-list(eq1,eq2t); eqt
>> [[1]]
>> depression ~ age + exercise + income + white + black + hispanic +
>>  base + somcol + grad + employed + unable + homeowner + married +
>>  divorced + widowed + fruit + highblood
>>
>> [[2]]
>> bmi ~ age + exercise + income + white + black + hispanic + base +
>>  somcol + grad + employed + unable + homeowner + married +
>>  divorced + widowed + depression
>>
>> On 2021/1/5 下午 04:18, Jeff Newmiller wrote:
>> > IMO if you want to hardcode a formula then simply hardcode a formula.
>> If you want 20 formulas, write 20 formulas. Is that really so bad?
>> >
>> > If you want to have an abbreviated way to specify sets of variables
>> without conforming to R syntax then put them into data files and read them
>> in using a format of your choice.
>> >
>> > But using NSE to avoid using quotes for entering what amounts to
>> in-script data is abuse of the language justified by laziness... the amount
>> of work you put yourself and anyone else who reads your code through is
>> excessive relative to the benefit gained.
>> >
>> > NSE has its strengths... but as a method of creating data objects it
>> sucks. Note that even the tidyverse (now) requires you to use quotes when
>> you are not directly referring to something that already exists. And if you
>> were... you might as well be creating a formula.
>> >
>> > On January 4, 2021 11:14:54 PM PST, Steven Yen 
>> wrote:
>> >> I constantly define variable lists from a data frame (e.g., to define a
>> >>
>> >> regression equation). Line 3 below does just that. Placing each
>> >> variable
>> >> name in quotation marks is too much work especially for a long list so
>> >> I
>> >> do that with line 4. Is there an easier way to accomplish thisto
>> >> define a list of variable names containing "a","c","e"? Thank you!
>> >>
>> >>> data<-as.data.frame(matrix(1:30,nrow=6))
>> >>> colnames(data)<-c("a","b","c","d","e"); data
>> >>a  b  c  d  e
>> >> 1 1  7 13 19 25
>> >> 2 2  8 14 20 26
>> >> 3 3  9 15 21 27
>> >> 4 4 10 16 22 28
>> >> 5 5 11 17 23 29
>> >> 6 6 12 18 24 30
>> >>> x1<-c("a","c","e"); x1 # line 3
>> >> [1] "a" "c" "e"
>> >>> x2<-colnames(subset(data,select=c(a,c,e))); x2 # line 4
>> >> [1] "a" "c" "e"
>> >>
>> >> __
>> >> 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.
>>
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To

Re: [R] Defining partial list of variables

2021-01-05 Thread Steven Yen
Here we go! BUT, it works great for a continuous line. With line 
break(s), I got the nuisance "\n" inserted.

 > x<-strsplit("hhsize,urban,male,gov,nongov,married",","); x
[[1]]
[1] "hhsize"  "urban"   "male"    "gov" "nongov"  "married"

 > x<-strsplit("hhsize,urban,male,
+ gov,nongov,married",","); x
[[1]]
[1] "hhsize"    "urban" "male" "\n    gov"
[5] "nongov"    "married"

On 2021/1/5 下午 05:34, Eric Berger wrote:
> zx<-strsplit("age,exercise,income,white,black,hispanic,base,somcol,grad,employed,unable,homeowner,married,divorced,widowed",",")
>
>
>
> On Tue, Jan 5, 2021 at 11:01 AM Steven Yen  > wrote:
>
> Thank you, Jeff. IMO, we are all here to make R work better to
> suit our
> various needs. All I am asking is an easier way to define variable
> list
> zx, differently from the way z0 , x0, and treat are defined.
>
>  > zx<-colnames(subset(mydata,select=c(
> + age,exercise,income,white,black,hispanic,base,somcol,grad,employed,
> + unable,homeowner,married,divorced,widowed)))
>  > z0<-c("fruit","highblood")
>  > x0<-c("vgood","poor")
>  > treat<-"depression"
>  > eq1 <-my.formula(y="depression",x=zx,z0)
>  > eq2 <-my.formula(y="bmi",   x=zx,x0)
>  > eq2t<-my.formula(y="bmi",   x=zx,treat)
>  > eqs<-list(eq1,eq2); eqs
> [[1]]
> depression ~ age + exercise + income + white + black + hispanic +
>  base + somcol + grad + employed + unable + homeowner + married +
>  divorced + widowed + fruit + highblood
>
> [[2]]
> bmi ~ age + exercise + income + white + black + hispanic + base +
>  somcol + grad + employed + unable + homeowner + married +
>  divorced + widowed + vgood + poor
>
>  > eqt<-list(eq1,eq2t); eqt
> [[1]]
> depression ~ age + exercise + income + white + black + hispanic +
>  base + somcol + grad + employed + unable + homeowner + married +
>  divorced + widowed + fruit + highblood
>
> [[2]]
> bmi ~ age + exercise + income + white + black + hispanic + base +
>  somcol + grad + employed + unable + homeowner + married +
>  divorced + widowed + depression
>
> On 2021/1/5 下午 04:18, Jeff Newmiller wrote:
> > IMO if you want to hardcode a formula then simply hardcode a
> formula. If you want 20 formulas, write 20 formulas. Is that
> really so bad?
> >
> > If you want to have an abbreviated way to specify sets of
> variables without conforming to R syntax then put them into data
> files and read them in using a format of your choice.
> >
> > But using NSE to avoid using quotes for entering what amounts to
> in-script data is abuse of the language justified by laziness...
> the amount of work you put yourself and anyone else who reads your
> code through is excessive relative to the benefit gained.
> >
> > NSE has its strengths... but as a method of creating data
> objects it sucks. Note that even the tidyverse (now) requires you
> to use quotes when you are not directly referring to something
> that already exists. And if you were... you might as well be
> creating a formula.
> >
> > On January 4, 2021 11:14:54 PM PST, Steven Yen  > wrote:
> >> I constantly define variable lists from a data frame (e.g., to
> define a
> >>
> >> regression equation). Line 3 below does just that. Placing each
> >> variable
> >> name in quotation marks is too much work especially for a long
> list so
> >> I
> >> do that with line 4. Is there an easier way to accomplish
> thisto
> >> define a list of variable names containing "a","c","e"? Thank you!
> >>
> >>> data<-as.data.frame(matrix(1:30,nrow=6))
> >>> colnames(data)<-c("a","b","c","d","e"); data
> >>    a  b  c  d  e
> >> 1 1  7 13 19 25
> >> 2 2  8 14 20 26
> >> 3 3  9 15 21 27
> >> 4 4 10 16 22 28
> >> 5 5 11 17 23 29
> >> 6 6 12 18 24 30
> >>> x1<-c("a","c","e"); x1 # line 3
> >> [1] "a" "c" "e"
> >>> x2<-colnames(subset(data,select=c(a,c,e))); x2 # line 4
> >> [1] "a" "c" "e"
> >>
> >> __
> >> 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
> 

Re: [R] Defining partial list of variables

2021-01-05 Thread Eric Berger
zx<-strsplit("age,exercise,income,white,black,hispanic,base,somcol,grad,employed,unable,homeowner,married,divorced,widowed",",")



On Tue, Jan 5, 2021 at 11:01 AM Steven Yen  wrote:

> Thank you, Jeff. IMO, we are all here to make R work better to suit our
> various needs. All I am asking is an easier way to define variable list
> zx, differently from the way z0 , x0, and treat are defined.
>
>  > zx<-colnames(subset(mydata,select=c(
> + age,exercise,income,white,black,hispanic,base,somcol,grad,employed,
> + unable,homeowner,married,divorced,widowed)))
>  > z0<-c("fruit","highblood")
>  > x0<-c("vgood","poor")
>  > treat<-"depression"
>  > eq1 <-my.formula(y="depression",x=zx,z0)
>  > eq2 <-my.formula(y="bmi",   x=zx,x0)
>  > eq2t<-my.formula(y="bmi",   x=zx,treat)
>  > eqs<-list(eq1,eq2); eqs
> [[1]]
> depression ~ age + exercise + income + white + black + hispanic +
>  base + somcol + grad + employed + unable + homeowner + married +
>  divorced + widowed + fruit + highblood
>
> [[2]]
> bmi ~ age + exercise + income + white + black + hispanic + base +
>  somcol + grad + employed + unable + homeowner + married +
>  divorced + widowed + vgood + poor
>
>  > eqt<-list(eq1,eq2t); eqt
> [[1]]
> depression ~ age + exercise + income + white + black + hispanic +
>  base + somcol + grad + employed + unable + homeowner + married +
>  divorced + widowed + fruit + highblood
>
> [[2]]
> bmi ~ age + exercise + income + white + black + hispanic + base +
>  somcol + grad + employed + unable + homeowner + married +
>  divorced + widowed + depression
>
> On 2021/1/5 下午 04:18, Jeff Newmiller wrote:
> > IMO if you want to hardcode a formula then simply hardcode a formula. If
> you want 20 formulas, write 20 formulas. Is that really so bad?
> >
> > If you want to have an abbreviated way to specify sets of variables
> without conforming to R syntax then put them into data files and read them
> in using a format of your choice.
> >
> > But using NSE to avoid using quotes for entering what amounts to
> in-script data is abuse of the language justified by laziness... the amount
> of work you put yourself and anyone else who reads your code through is
> excessive relative to the benefit gained.
> >
> > NSE has its strengths... but as a method of creating data objects it
> sucks. Note that even the tidyverse (now) requires you to use quotes when
> you are not directly referring to something that already exists. And if you
> were... you might as well be creating a formula.
> >
> > On January 4, 2021 11:14:54 PM PST, Steven Yen  wrote:
> >> I constantly define variable lists from a data frame (e.g., to define a
> >>
> >> regression equation). Line 3 below does just that. Placing each
> >> variable
> >> name in quotation marks is too much work especially for a long list so
> >> I
> >> do that with line 4. Is there an easier way to accomplish thisto
> >> define a list of variable names containing "a","c","e"? Thank you!
> >>
> >>> data<-as.data.frame(matrix(1:30,nrow=6))
> >>> colnames(data)<-c("a","b","c","d","e"); data
> >>a  b  c  d  e
> >> 1 1  7 13 19 25
> >> 2 2  8 14 20 26
> >> 3 3  9 15 21 27
> >> 4 4 10 16 22 28
> >> 5 5 11 17 23 29
> >> 6 6 12 18 24 30
> >>> x1<-c("a","c","e"); x1 # line 3
> >> [1] "a" "c" "e"
> >>> x2<-colnames(subset(data,select=c(a,c,e))); x2 # line 4
> >> [1] "a" "c" "e"
> >>
> >> __
> >> 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.
>

[[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] Defining partial list of variables

2021-01-05 Thread Steven Yen
Thank you, Jeff. IMO, we are all here to make R work better to suit our 
various needs. All I am asking is an easier way to define variable list 
zx, differently from the way z0 , x0, and treat are defined.


> zx<-colnames(subset(mydata,select=c(
+ age,exercise,income,white,black,hispanic,base,somcol,grad,employed,
+ unable,homeowner,married,divorced,widowed)))
> z0<-c("fruit","highblood")
> x0<-c("vgood","poor")
> treat<-"depression"
> eq1 <-my.formula(y="depression",x=zx,z0)
> eq2 <-my.formula(y="bmi",   x=zx,x0)
> eq2t<-my.formula(y="bmi",   x=zx,treat)
> eqs<-list(eq1,eq2); eqs
[[1]]
depression ~ age + exercise + income + white + black + hispanic +
    base + somcol + grad + employed + unable + homeowner + married +
    divorced + widowed + fruit + highblood

[[2]]
bmi ~ age + exercise + income + white + black + hispanic + base +
    somcol + grad + employed + unable + homeowner + married +
    divorced + widowed + vgood + poor

> eqt<-list(eq1,eq2t); eqt
[[1]]
depression ~ age + exercise + income + white + black + hispanic +
    base + somcol + grad + employed + unable + homeowner + married +
    divorced + widowed + fruit + highblood

[[2]]
bmi ~ age + exercise + income + white + black + hispanic + base +
    somcol + grad + employed + unable + homeowner + married +
    divorced + widowed + depression

On 2021/1/5 下午 04:18, Jeff Newmiller wrote:

IMO if you want to hardcode a formula then simply hardcode a formula. If you 
want 20 formulas, write 20 formulas. Is that really so bad?

If you want to have an abbreviated way to specify sets of variables without 
conforming to R syntax then put them into data files and read them in using a 
format of your choice.

But using NSE to avoid using quotes for entering what amounts to in-script data 
is abuse of the language justified by laziness... the amount of work you put 
yourself and anyone else who reads your code through is excessive relative to 
the benefit gained.

NSE has its strengths... but as a method of creating data objects it sucks. 
Note that even the tidyverse (now) requires you to use quotes when you are not 
directly referring to something that already exists. And if you were... you 
might as well be creating a formula.

On January 4, 2021 11:14:54 PM PST, Steven Yen  wrote:

I constantly define variable lists from a data frame (e.g., to define a

regression equation). Line 3 below does just that. Placing each
variable
name in quotation marks is too much work especially for a long list so
I
do that with line 4. Is there an easier way to accomplish thisto
define a list of variable names containing "a","c","e"? Thank you!


data<-as.data.frame(matrix(1:30,nrow=6))
colnames(data)<-c("a","b","c","d","e"); data

   a  b  c  d  e
1 1  7 13 19 25
2 2  8 14 20 26
3 3  9 15 21 27
4 4 10 16 22 28
5 5 11 17 23 29
6 6 12 18 24 30

x1<-c("a","c","e"); x1 # line 3

[1] "a" "c" "e"

x2<-colnames(subset(data,select=c(a,c,e))); x2 # line 4

[1] "a" "c" "e"

__
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] Search list of igraph objects to return some with certain values

2021-01-05 Thread Jeff Newmiller
Standard logical indexing. Write a function that returns TRUE or FALSE given an 
igraph object. Use sapply or Vectorize to make a logical vector as long as your 
list. Then use that vector to index the list with single bracket indexing.

On January 4, 2021 6:08:43 PM PST, Chris Buddenhagen  
wrote:
>Hi all
>
>I stochastically simulated thousands of directed igraph objects in list
>format.  I want to find and plot an example graph that meets a certain
>criteria (a link to a certain node). Does anyone have pointers on how
>to
>subset a list of igraph objects?
>
>Sincerely,
>
>
>Chris Buddenhagen
>cbuddenha...@gmail.com
>
>   [[alternative HTML version deleted]]
>
>__
>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

-- 
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] Defining partial list of variables

2021-01-05 Thread Jeff Newmiller
IMO if you want to hardcode a formula then simply hardcode a formula. If you 
want 20 formulas, write 20 formulas. Is that really so bad?

If you want to have an abbreviated way to specify sets of variables without 
conforming to R syntax then put them into data files and read them in using a 
format of your choice.

But using NSE to avoid using quotes for entering what amounts to in-script data 
is abuse of the language justified by laziness... the amount of work you put 
yourself and anyone else who reads your code through is excessive relative to 
the benefit gained.

NSE has its strengths... but as a method of creating data objects it sucks. 
Note that even the tidyverse (now) requires you to use quotes when you are not 
directly referring to something that already exists. And if you were... you 
might as well be creating a formula.

On January 4, 2021 11:14:54 PM PST, Steven Yen  wrote:
>I constantly define variable lists from a data frame (e.g., to define a
>
>regression equation). Line 3 below does just that. Placing each
>variable 
>name in quotation marks is too much work especially for a long list so
>I 
>do that with line 4. Is there an easier way to accomplish thisto 
>define a list of variable names containing "a","c","e"? Thank you!
>
> > data<-as.data.frame(matrix(1:30,nrow=6))
> > colnames(data)<-c("a","b","c","d","e"); data
>
>   a  b  c  d  e
>1 1  7 13 19 25
>2 2  8 14 20 26
>3 3  9 15 21 27
>4 4 10 16 22 28
>5 5 11 17 23 29
>6 6 12 18 24 30
> > x1<-c("a","c","e"); x1 # line 3
>[1] "a" "c" "e"
> > x2<-colnames(subset(data,select=c(a,c,e))); x2 # line 4
>
>[1] "a" "c" "e"
>
>__
>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] Defining partial list of variables

2021-01-05 Thread Heinz Tuechler

see below

Steven Yen wrote/hat geschrieben on/am 05.01.2021 08:14:

I constantly define variable lists from a data frame (e.g., to define a
regression equation). Line 3 below does just that. Placing each variable
name in quotation marks is too much work especially for a long list so I
do that with line 4. Is there an easier way to accomplish thisto
define a list of variable names containing "a","c","e"? Thank you!


data<-as.data.frame(matrix(1:30,nrow=6))
colnames(data)<-c("a","b","c","d","e"); data


  a  b  c  d  e
1 1  7 13 19 25
2 2  8 14 20 26
3 3  9 15 21 27
4 4 10 16 22 28
5 5 11 17 23 29
6 6 12 18 24 30

x1<-c("a","c","e"); x1 # line 3

[1] "a" "c" "e"

x2<-colnames(subset(data,select=c(a,c,e))); x2 # line 4


[1] "a" "c" "e"


What about:
x3 <- names(data)[c(1,3,5)]
x3
[1] "a" "c" "e"

If I have to compile longer vectors of variable names I do it as follows:
First I use:
dput(names(data))
resulting in a vector of names.
c("a", "b", "c", "d", "e")
Then I edit the output by hand, e.g.
x4 <- c("a", "b", "c", "d", "e")
x4 <- c("a", "c", "e")
This is especially useful with long names, where I could easily make
typing errors.

regards,
Heinz

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