[R] Tying to underdressed the magic of lm redux

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Thu May 30 03:59:20 CEST 2019


Basically, huh?

> df <- data.frame(a = 1:3, b = letters[1:3])
> nm <- names(df)
> print(df[,nm[1]])
[1] 1 2 3
> print(df[,nm[2]])
[1] a b c
Levels: a b c

This can be done within a function, of course:

> demo <- function(df, colnames){
+    print(df[,colnames])
+ }
> demo(df,c("a","b"))
  a b
1 1 a
2 2 b
3 3 c

Am I missing something? (Apologies, if so).

Bert Gunter



On Wed, May 29, 2019 at 6:40 PM Sorkin, John <jsorkin using som.umaryland.edu>
wrote:

> Thanks to several kind people, I understand how to use
> deparse(substitute(paramter)) to get as text strings the arguments passed
> to an R function. What I still can't do is put the text strings recovered
> by deparse(substitute(parameter)) back together to get the columns of a
> dataframe passed to the function. What I want to do is pass a column name
> to a function along with the name of the dataframe and then, within the
> function access the column of the dataframe.
>
> I want the function below to print the columns of the dataframe testdata,
> i.e. testdata[,"FSG"] and testdata[,"GCM"]. I have tried several ways to
> tell the function to print the columns; none of them work.
>
> I thank everyone who has helped in the past, and those people who will
> help me now!
>
> John
>
> testdata <- structure(list(FSG = c(271L, 288L, 269L, 297L, 311L, 217L,
> 235L,
>
>                                    172L, 201L, 162L), CGM = c(205L, 273L,
> 226L, 235L, 311L, 201L,
>
>                                    203L, 155L, 182L, 163L)), row.names =
> c(NA, 10L), class = "data.frame")
>
> cat("This is the data frame")
>
> class(testdata)
>
> testdata
>
>
>
> BAPlot <- function(first,second,indata){
>
>   # these lines of code work
>
>     col1 <- deparse(substitute(first))
>
>     col2 <- deparse(substitute(second))
>
>     thedata <- deparse(substitute(third))
>
>     print(col1)
>
>     print(col2)
>
>     print(thedata)
>
>     cat("This gets the data, but not as a dataframe\n")
>
>     zoop<-paste(indata)
>
>     print(zoop)
>
>     cat("End This gets the data, but not as a dataframe\n")
>
>      # these lines do not work
>
>     print(indata[,first])
>
>     print(indata[,"first"])
>
>     print(thedata[,col1])
>
>     paste(zoop[,paste(first)])
>
>     paste(zoop[,first])
>
>     zap<-paste(first)
>
>     print(zap)
>
> }
>
>
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using 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]]



More information about the R-help mailing list