Damon Wischik | 22 Dec 01:38 2003
Picon
Picon

Re: [R] Factor names & levels


> I agree it may not be 100% clear but ?names does say
> "The default methods get and set the '"names"' attribute 
> of a vector or list." and if you issue the command:
>    methods("names")
> you find that the only non-default method is names.dist.

I still want to know how I should understand the following:

> x <- factor(c("one","three"))
> names(x) <- c("fred","jim")
> names(x)
[1] "fred" "jim" 
class(x)
[1] "factor"

Given that names seems to work on factors, I can see two possibilities:
1. It is a bug that it acts as it does;
2. the default method does what it says in the help page, but also does
more than just this.

I don't know enough to look at the source code to find out what is going
on.

Damon.

______________________________________________
R-help <at> stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help

(Continue reading)

Gabor Grothendieck | 22 Dec 02:02 2003

Re: [R] Factor names & levels


The effect of names() on factors is undefined.    The fact
that it coincidentally partially works on factors is just
chance.

For it to be well defined, there would need to be a names
method and a names<- method for the factor class or else 
the default methods would have to be able to handle factors.

Its a bit dangerous to rely on the coincidental behavior of
functionality, but in the absence of explicit R support, if 
using names with factors were important to you then you could 
define your own methods like this:

"names<-.factor" <- function( x, value ) {
	attr(x, "levels") <- value
	x
}

names.factor <- function(x) attr( x, "levels" )

# with the above, this works:

x <- factor(c("one","three"))
names(x) <- c("fred","jim")   # implicitly invokes "names<-.factor"
names(x) # implicitly invokes names.factor

Hope this clears it up for you.

--- 
(Continue reading)

Peter Dalgaard | 22 Dec 02:11 2003
Picon

Re: [R] Factor names & levels

Damon Wischik <djw1005 <at> cam.ac.uk> writes:

> I still want to know how I should understand the following:
> 
> > x <- factor(c("one","three"))
> > names(x) <- c("fred","jim")
> > names(x)
> [1] "fred" "jim" 
> class(x)
> [1] "factor"
> 
> Given that names seems to work on factors, I can see two possibilities:
> 1. It is a bug that it acts as it does;
> 2. the default method does what it says in the help page, but also does
> more than just this.

The behaviour is *undefined*, for crying out loud! That means that if
you try to do it anyway, you may get an error, something
semi-sensible, or find that the code transfers a large sum of money to
a Danish bank account...

In the present implementation, the names attribute gets attached to
the factor just as to an integer vector, but it is not shown when
printing and there are several parts of the code where no attempt is
made to preserve names since there are not expected to be any. 

--

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
(Continue reading)

Peter Dalgaard | 22 Dec 02:30 2003
Picon

Re: [R] Factor names & levels

"Gabor Grothendieck" <ggrothendieck <at> myway.com> writes:

> For it to be well defined, there would need to be a names
> method and a names<- method for the factor class or else 
> the default methods would have to be able to handle factors.

Not only that but other methods for factors need to know about the
names and be able to modify them accordingly, e.g.

> getS3method("levels<-","factor")
function (x, value)
{
    xlevs <- levels(x)
    if (is.list(value))  #something
...
    else {
...
        nlevs <- xlevs <- as.character(value)
    }
    factor(xlevs[x], levels = unique(nlevs))
}

Here, xlevs[x] will not have the same names as x (it gets names from
xlevs if anything) so you'd have to have extra code for setting the
names on the result. 

(Rather interestingly, the factor() function does explicitly retain
names, so there are not quite as many places where they will be lost
as I would have expected.)

(Continue reading)

Gabor Grothendieck | 22 Dec 03:27 2003

Re: [R] Factor names & levels


Based on Peter's response, I think I may have misinterpreted 
Damon's query.   The methods I displayed in my last post in 
this thread were intended to make name a synonym for level. If
its desired that name act on factors in the same way that names 
act on vectors and lists then the methods I provided would not 
be correct and, as Peter points out, the other factor methods 
would have to be examined, as well, to ensure that they all 
work properly with names. 

I do have one other idea in terms of a workaround.  You could
represent your factor as a one column data frame.  The data
frame could then have row names which could be interpreted as
names of the factor.

For example,

f <- data.frame(f = c("A","B","A","C"))
row.names(f) <- letters[1:4]

You can now refer to the factor as f$f and the names as row.names(f).
For example,

> f <- data.frame(f = factor(c("A","B","A","C")))
> row.names(f) <- letters[1:4]
> f
  f
a A
b B
c A
(Continue reading)

Mathieu | 22 Dec 08:46 2003
Picon
Picon

Re: [R] won't install properly on Mac 10.3.2


Le 19 déc. 03, à 23:44, Thomas Lumley a écrit :

> On Fri, 19 Dec 2003, Hank Stevens wrote:
>
>> R version 1.8.1, Mac OS X 10.3.2
>>
>> I have tried searching for this  problem and its fix, but to no avail.
>> -Everything seems to download and unpack fine. I double click on 
>> StartR,
>> however, and it just winks and fails.
>> Any thoughts?
>
> If you open up a Console window  (go to Applications, then Utilities, 
> then
> select Console) you may get some more helpful error messages.

The problem may appear if you have installed libiconv (a tex associated 
package), see the Raqua-FAQ : 
http://cran.at.r-project.org/bin/macosx/RAqua-FAQ.html

HTH,

--Mathieu

______________________________________________
R-help <at> stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help

(Continue reading)

Petr Pikal | 22 Dec 09:54 2003
Picon

Re: [R] [Mailman] question: contour plot for discrete data

Hi

library(akima)
function
contour(interp(x,y,z))

is one possibility

Cheers

Petr

On 19 Jan 2058 at 48:211, L Z wrote:

> Question:
>  I have matrix (n x3) that represents discrete data.
> Each row of matrix is 3-D point (x,y,z). I would like
> to get contour map (z value) at two dimension
> (x,y). How can I use related contour function to do
> this job?
> I am not sure if I clarify this question. For example,
> we can get point (x,y)
> at 2 dimension according to first two columns of
> matrix. Then I want to connect
> same value z=(x,y). Thanks!
>   zhang
> 
> The data file looks like this:
> X          y               z
> 4           0.33          0.99
(Continue reading)

Helmut Kudrnovsky | 22 Dec 10:29 2003
Picon

[R] Problems with read.table()

R version 1.8.1, OS Windows 98

Dear colleagues,

if I import vegetation data (first row with column labels and first column 
with row labels) like

7MYRGERM;7AGRGIGA;7DRYOCTO;5MYRGERM;7SALELEA;7CHOCHON;7SALNIG°;.......
t401;5;2;2;3;4;2;2;2;1;2;1;2;2;1;2;2;2;1;2;1;0;0;......
t403;3;0;0;6;4;0;3;0;0;3;0;0;0;0;3;0;0;0;2;0;2;0;.....

with read.table("data.file", header=TRUE, separator=";"), the R program 
crashes with following error message:

RGUI verursachte einen Fehler durch eine ungültige Seite
in Modul R.DLL bei 015f:6b515b87.
Register:
EAX=0075007a CS=015f EIP=6b515b87 EFLGS=00010246
EBX=01bd3f57 SS=0167 ESP=0073ed20 EBP=0073ed48
ECX=01bd3ad8 DS=0167 ESI=00000001 FS=2237
EDX=ffffffb0 ES=0167 EDI=00000008 GS=0000
Bytes bei CS:EIP:
0f b7 04 50 83 e0 08 eb ce 89 5d ec eb 8b 89 74
Stapelwerte:
00000000 0073ee20 019ea008 00000007 00002000 0000003b 000000b0 0073ee20 
00000006 01bd5f58 0073edb8 6b5162ef 00000010 00000001 0073ed8c 0073ee20

with greetings from Tyrol
Helmut Kudrnovsky

(Continue reading)

Eric ESPOSITO | 22 Dec 10:51 2003

[R] runif and sample with reproducibility

Hello,
I would like to sample a population but the result needs to be
reproducible, using 'runif' or 'sample' is the good way to do it but I
can't manage to make the results reproducible even with the 'set.seed'
function.
My aim is that th call to 'sample(1:100,10)' gives always the same result,
how can I do that?
Thanks!

Eric Esposito

______________________________________________
R-help <at> stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help

Philippe Glaziou | 22 Dec 11:23 2003

Re: [R] runif and sample with reproducibility

Eric ESPOSITO <eric.esposito <at> gazdefrance.com> wrote:
> I would like to sample a population but the result needs to be
> reproducible, using 'runif' or 'sample' is the good way to do it but I
> can't manage to make the results reproducible even with the 'set.seed'
> function.
> My aim is that th call to 'sample(1:100,10)' gives always the same result,
> how can I do that?

Can you give an example of set.seed not giving reproducible samples?

set.seed(101)
sample(1:100,10)
 [1] 38  5 70 64 24 29 55 32 58 50
set.seed(101)
sample(1:100,10)
 [1] 38  5 70 64 24 29 55 32 58 50

--

-- 
Philippe Glaziou
Epidemiologist

______________________________________________
R-help <at> stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help


Gmane