An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120622/cf0fc335/attachment.pl>
dropping variables from a data frame inside a function
5 messages · Joseph Boyer, David Winsemius, R. Michael Weylandt +1 more
On Jun 22, 2012, at 5:09 PM, Joseph Boyer wrote:
Why doesn't this work?
#Drop a variable name from a data frame
DropLikeSAS <- function(x,df) {
df[[x]] <- NULL
0
}
DropLikeSAS("VarName", DataFrameName)
Try it. The column VarName will not be deleted from the data frame
DataFrameName.
But
x <- "VarName"
DataFrameName[[x]] <- NULL
Works.
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD West Hartford, CT
On Jun 22, 2012, at 5:09 PM, Joseph Boyer wrote:
DropLikeSAS <- function(x,df) {
df[[x]] <- NULL
0
}
DropLikeSAS("VarName", DataFrameName)
Sorry for the blank message>
> DropLikeSAS <- function(x,df) {
+ df[[x]] <- NULL
+ return(df) }
>
> DropLikeSAS("b", df)
a
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
>
David Winsemius, MD
West Hartford, CT
I think the OP might also be tripped up on the fact that R is pass-by-value so effects on df inside DropLikeSAS won't have impact outside the function's scope. The df inside of DropLikeSAS() is changed as expected, but that has no effect on the df outside that function. To the OP: There are ways to get pass-by-reference behavior (which I think is what you are expecting), but the default is pass-by-value for good reason. Give it a shot for a while and see if you come to like it -- I think you will. Best, Michael
On Fri, Jun 22, 2012 at 4:22 PM, David Winsemius <dwinsemius at comcast.net> wrote:
On Jun 22, 2012, at 5:09 PM, Joseph Boyer wrote:
DropLikeSAS <- function(x,df) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? df[[x]] <- NULL
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
DropLikeSAS("VarName", DataFrameName)
Sorry for the blank message>
DropLikeSAS <- function(x,df) {
+ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?df[[x]] <- NULL + ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return(df) }
DropLikeSAS("b", df)
? ?a 1 ? 1 2 ? 2 3 ? 3 4 ? 4 5 ? 5 6 ? 6 7 ? 7 8 ? 8 9 ? 9 10 10
David Winsemius, MD West Hartford, CT
______________________________________________ R-help at 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.
On 12-06-22 5:09 PM, Joseph Boyer wrote:
Why doesn't this work?
David gave you a solution that works. The answer to this question is that unlike SAS, R is a computing language that has an idea of variable scoping: when you modify the argument df in DropLikeSAS, you are making local changes, not global ones. Duncan Murdoch
#Drop a variable name from a data frame
DropLikeSAS<- function(x,df) {
df[[x]]<- NULL
0
}
DropLikeSAS("VarName", DataFrameName)
Try it. The column VarName will not be deleted from the data frame DataFrameName.
But
x<- "VarName"
DataFrameName[[x]]<- NULL
Works.
[[alternative HTML version deleted]]
______________________________________________ R-help at 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.