Dear All,
where can I find information about how to write an assigment form of a
function?
For curiosity I tried to write a different form of the levels()-function,
since the original method for factor deletes all other attributes of a factor.
Of course, the simple method would be to use instead of levels(x) <-
newlevels, attr(x, 'levels') <- newlevels.
I tried the following:
## example
x <- factor(c(1,1,NA,2,3,4,4,4,1,2)); y <- x
attr(x, 'levels') <- c('a', 'b', 'c', 'd') # does what I want
x
[1] a a <NA> b c d d d a b
Levels: a b c d
'levels.simple<-' <- function (x, value)
{
attr(x, 'levels') <- value
}
levels.simple(y) <- c('a', 'b', 'c', 'd') # does not what I want
y
[1] "a" "b" "c" "d"
Thanks,
Heinz T??chler
how to write assignment form of function
6 messages · Heinz Tuechler, Dimitris Rizopoulos, Brian Ripley
take a look at e.g., get("levels<-.factor"); you should return "x" in
your function, i.e.,
y <- x <- factor(c(1,1,NA,2,3,4,4,4,1,2))
############
'levels.simple<-' <- function(x, value){
attr(x, 'levels') <- value
x
}
levels.simple(y) <- c('a', 'b', 'c', 'd')
y
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Heinz Tuechler" <tuechler at gmx.at>
To: <r-help at stat.math.ethz.ch>
Sent: Wednesday, August 10, 2005 2:24 PM
Subject: [R] how to write assignment form of function
Dear All,
where can I find information about how to write an assigment form of a
function?
For curiosity I tried to write a different form of the
levels()-function,
since the original method for factor deletes all other attributes of a
factor.
Of course, the simple method would be to use instead of levels(x) <-
newlevels, attr(x, 'levels') <- newlevels.
I tried the following:
## example
x <- factor(c(1,1,NA,2,3,4,4,4,1,2)); y <- x
attr(x, 'levels') <- c('a', 'b', 'c', 'd') # does what I want
x
[1] a a <NA> b c d d d a b
Levels: a b c d
'levels.simple<-' <- function (x, value)
{
attr(x, 'levels') <- value
}
levels.simple(y) <- c('a', 'b', 'c', 'd') # does not what I want
y
[1] "a" "b" "c" "d"
Thanks,
Heinz T??chler
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
At 14:49 10.08.2005 +0200, Dimitris Rizopoulos wrote:
take a look at e.g., get("levels<-.factor"); you should return "x" in
your function, i.e.,
y <- x <- factor(c(1,1,NA,2,3,4,4,4,1,2))
############
'levels.simple<-' <- function(x, value){
attr(x, 'levels') <- value
x
}
levels.simple(y) <- c('a', 'b', 'c', 'd')
y
I hope it helps.
Best,
Dimitris
Dear Dimitris, Thank you a lot - it helped. Now it works. Heinz
---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Heinz Tuechler" <tuechler at gmx.at> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, August 10, 2005 2:24 PM Subject: [R] how to write assignment form of function Dear All, where can I find information about how to write an assigment form of a function? For curiosity I tried to write a different form of the levels()-function, since the original method for factor deletes all other attributes of a factor. Of course, the simple method would be to use instead of levels(x) <- newlevels, attr(x, 'levels') <- newlevels. I tried the following: ## example x <- factor(c(1,1,NA,2,3,4,4,4,1,2)); y <- x attr(x, 'levels') <- c('a', 'b', 'c', 'd') # does what I want x [1] a a <NA> b c d d d a b Levels: a b c d 'levels.simple<-' <- function (x, value) { attr(x, 'levels') <- value } levels.simple(y) <- c('a', 'b', 'c', 'd') # does not what I want y [1] "a" "b" "c" "d" Thanks, Heinz T??chler
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
On Wed, 10 Aug 2005, Heinz Tuechler wrote:
where can I find information about how to write an assigment form of a function?
In all good books on S programming, or by studying examples. But in this case the problem is actually about defining functions with the return value you expect.
For curiosity I tried to write a different form of the levels()-function, since the original method for factor deletes all other attributes of a factor. Of course, the simple method would be to use instead of levels(x) <- newlevels, attr(x, 'levels') <- newlevels.
And that would not do what the current function does, which is to merge levels as required. I suggest you look at levels<-.factor in R-devel, which does not drop attributes.
I tried the following:
## example
x <- factor(c(1,1,NA,2,3,4,4,4,1,2)); y <- x
attr(x, 'levels') <- c('a', 'b', 'c', 'd') # does what I want
x
[1] a a <NA> b c d d d a b
Levels: a b c d
'levels.simple<-' <- function (x, value)
{
attr(x, 'levels') <- value
}
This did not return anything! Try returning 'x'.
levels.simple(y) <- c('a', 'b', 'c', 'd') # does not what I want
y
[1] "a" "b" "c" "d"
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Dear Professor Ripley, thank you for your answer. Adding a return value, as also Dimitris Rizopoulos suggested the function does what I need, that is to rename factor levels. I tried to look at levels<-.factor in R-devel but I have to admit that I do not know exactly where to look and searching I did not find it. For the moment my problem is solved and I interpret your hint that way that in the future levels<-.factor will not more drop all other attributes. Thanks again Heinz T??chler
At 15:18 10.08.2005 +0100, Prof Brian Ripley wrote:
On Wed, 10 Aug 2005, Heinz Tuechler wrote:
where can I find information about how to write an assigment form of a function?
In all good books on S programming, or by studying examples. But in this case the problem is actually about defining functions with the return value you expect.
For curiosity I tried to write a different form of the levels()-function, since the original method for factor deletes all other attributes of a
factor.
Of course, the simple method would be to use instead of levels(x) <- newlevels, attr(x, 'levels') <- newlevels.
And that would not do what the current function does, which is to merge levels as required. I suggest you look at levels<-.factor in R-devel, which does not drop attributes.
I tried the following:
## example
x <- factor(c(1,1,NA,2,3,4,4,4,1,2)); y <- x
attr(x, 'levels') <- c('a', 'b', 'c', 'd') # does what I want
x
[1] a a <NA> b c d d d a b
Levels: a b c d
'levels.simple<-' <- function (x, value)
{
attr(x, 'levels') <- value
}
This did not return anything! Try returning 'x'.
levels.simple(y) <- c('a', 'b', 'c', 'd') # does not what I want
y
[1] "a" "b" "c" "d"
-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On Wed, 10 Aug 2005, Heinz Tuechler wrote:
Dear Professor Ripley, thank you for your answer. Adding a return value, as also Dimitris Rizopoulos suggested the function does what I need, that is to rename factor levels. I tried to look at levels<-.factor in R-devel but I have to admit that I do not know exactly where to look and searching I did not find it. For the
https://svn.r-project.org/R/trunk/src/library/base/R/factor.R
moment my problem is solved and I interpret your hint that way that in the future levels<-.factor will not more drop all other attributes.
Yes, that's true.
Thanks again Heinz T?chler At 15:18 10.08.2005 +0100, Prof Brian Ripley wrote:
On Wed, 10 Aug 2005, Heinz Tuechler wrote:
where can I find information about how to write an assigment form of a function?
In all good books on S programming, or by studying examples. But in this case the problem is actually about defining functions with the return value you expect.
For curiosity I tried to write a different form of the levels()-function, since the original method for factor deletes all other attributes of a
factor.
Of course, the simple method would be to use instead of levels(x) <- newlevels, attr(x, 'levels') <- newlevels.
And that would not do what the current function does, which is to merge levels as required. I suggest you look at levels<-.factor in R-devel, which does not drop attributes.
I tried the following:
## example
x <- factor(c(1,1,NA,2,3,4,4,4,1,2)); y <- x
attr(x, 'levels') <- c('a', 'b', 'c', 'd') # does what I want
x
[1] a a <NA> b c d d d a b
Levels: a b c d
'levels.simple<-' <- function (x, value)
{
attr(x, 'levels') <- value
}
This did not return anything! Try returning 'x'.
levels.simple(y) <- c('a', 'b', 'c', 'd') # does not what I want
y
[1] "a" "b" "c" "d"
-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595