Re: [R] how to change the class of a group of objects

2009-11-24 Thread Ben Mazzotta
Erik and others,

Many thanks for your assistance. Erik correctly points out that the
as.dist() coercion requires a different format than I was using. The
correct format for input to as.dist() is a square matrix, not a lower
triangle matrix. Once I had that in order, everything went much better.

I had been attempting to coerce a vector of output from lowertri()
directly to an object of class "dist." Instead, what I needed to do was
generate a zero matrix and replace the lower triangle of entries with
the output from lowertri().

In particular, thanks to Henrique Dallazuanna and Phil Spector.

Ben

Erik Iverson wrote:
> Ben, 
>
> In general, when you define a, b, and c as below, the c() wrapper is not 
> needed, since the : operator returns a vector anyway.  Also, best not to name 
> variables 'c' to avoid confusion with the function you're using. 
>
> Regarding your actual question, lapply(list(a, b, c), "class<-", "dist") 
> might work, but your objects in your example don't seem to be able to be 
> coerced to dist objects.  E.g., as.dist(b) gives an error.  If you have data 
> that can actually work as dist objects, you might try the above line though.
>
> Erik  
>
>   
>> -Original Message-
>> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
>> On Behalf Of Ben Mazzotta
>> Sent: Monday, November 23, 2009 4:51 PM
>> To: r-help@r-project.org
>> Subject: [R] how to change the class of a group of objects
>>
>> Dear R users,
>>
>> I would like to change the class of a group of objects in R memory from
>> "numeric" to "dist". I can manipulate the class using
>>
>> class(foo) <- bar
>>
>> but I cannot get the same command to work on groups of variables. When I
>> use for() loops and lists of names, inevitably I have to specify
>>
>> class(get("foo")) <- bar
>>
>> which causes class() to return an error. Could anyone offer some advice
>> how to manipulate classes and attributes of a list of objects in memory?
>>
>> Many thanks,
>> Ben
>>
>>
>>
>>
>> a <- c(1:5)
>> b <- c(1:10)
>> c <- c(1:20)
>>
>> # What I would like to do is coerce all of these to class "dist".
>> # How can I write a command that changes the class of a, b, AND c to
>> "dist"?
>> # Most of my answers include a list or a for() loop over (a, b, c)
>> # How can I write a command that changes attributes of a, b, and c all
>> at the same time?
>> # I can force the process to work for a few objects, but it is
>> time-consuming.
>> # For reference, I am including the following commands.
>>
>> class(a)
>> class(get(letters[1]))
>> class(a) <- "dist"
>> class(a) <- NULL
>> class(a)
>> class(get(letters[1])) <- "dist"
>>
>>
>> --
>> Ben Mazzotta
>> PhD Candidate
>> Fletcher School, Tufts University
>>
>> __
>> R-help@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.
>> 
>
>
>

__
R-help@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.


[R] how to change the class of a group of objects

2009-11-23 Thread Ben Mazzotta
Dear R users,

I would like to change the class of a group of objects in R memory from
"numeric" to "dist". I can manipulate the class using

class(foo) <- bar

but I cannot get the same command to work on groups of variables. When I
use for() loops and lists of names, inevitably I have to specify

class(get("foo")) <- bar

which causes class() to return an error. Could anyone offer some advice
how to manipulate classes and attributes of a list of objects in memory?

Many thanks,
Ben




a <- c(1:5)
b <- c(1:10)
c <- c(1:20)

# What I would like to do is coerce all of these to class "dist".
# How can I write a command that changes the class of a, b, AND c to 
"dist"?
# Most of my answers include a list or a for() loop over (a, b, c)
# How can I write a command that changes attributes of a, b, and c all
at the same time?
# I can force the process to work for a few objects, but it is
time-consuming.
# For reference, I am including the following commands.

class(a)
class(get(letters[1]))
class(a) <- "dist"
class(a) <- NULL
class(a)
class(get(letters[1])) <- "dist"


-- 
Ben Mazzotta
PhD Candidate
Fletcher School, Tufts University
160 Packard Ave, Medford MA 02155
benjamin.mazzo...@tufts.edu
+1.617.462.4486

__
R-help@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.


[R] multiple values for a factor using nodematch in ergm()

2009-08-18 Thread Ben Mazzotta
Greetings, and many thanks for any insight you can offer.

I have a network object on which I'd like to simulate an ergm() using
{statnet}. Since the network object is large, I have not copied it here.

Each vertex has an attribute "language" which can take on integer values
greater than or equal to 1.

Is there a way to use the nodematch argument of ergm(), such that it
searches for any matches in the vertex attributes, rather than a 1-to-1
match?

For example, given

mat <- matrix(1, ncol=4, nrow=4)
net <- network(mat)


lang <- list("A", c("A","B"), "B", "C")
net %v% "languages" <- lang


The truth table for the matches should read
H   T   V (for head, tail, value)
1   2   1
1   3   1
1   4   0
2   3   1
2   4   0
3   4   0



This is the standard implementation of ergm(... , nodematch()), I believe.
model2 <- ergm(net ~ nodematch(languages))



In the course of writing this post, I discovered that the net %v%
"languages" command above coerces the list "lang" to a vector.


I also have a dataset that specifies exhaustively whether dyads share a
language or not; but I am not sure how to enter dyad information
directly into the ergm() function.

-- 
Ben Mazzotta
PhD Candidate
Fletcher School, Tufts University
160 Packard Ave, Medford MA 02155
benjamin.mazzo...@tufts.edu
+1.617.462.4486

__
R-help@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.


[R] FYI conflict between statnet, igraph

2009-08-18 Thread Ben Mazzotta
Dear R users:

There appears to be a conflict between the statnet package and the
igraph package.

With igraph loaded into memory, the network() function of statnet
failed, returning the error to the effect that the matrix being
converted was "not a graph object." Removing igraph from memory restored
statnet to proper function.

Apologies if this is redundant. I could not find this documented in the
help archives or in the docs for either package.

Best,

-- 
Ben Mazzotta
PhD Candidate
Fletcher School, Tufts University
160 Packard Ave, Medford MA 02155
benjamin.mazzo...@tufts.edu
+1.617.462.4486

__
R-help@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.


[R] data frame from list of lists with unequal lengths

2009-07-20 Thread Ben Mazzotta
Hello,

I have a dataset with multiple entries in one field separated by "/"
characters. (The true dataset has long names, 20-odd variables, and
hundreds of observations.)


 v1 v2
1 A  L
2   A/B  M
3 C  N
4 D/E/F  O
5 A  P
6 C  L


What I would like is to have a dataset that looks like this instead:

> my.df
  v1 v2
1  A  L
2  A  M
3  B  M
4  C  N
5  D  O
6  E  O
7  F  O
8  A  P
9  C  L


My original thought was to break the string into variables using
strsplit(), create new columns in the data frame using cbind(), and then
reshape the dataset with the melt() function.

> v1.new <- as.character(my.df$v1)
> v1.new <- strsplit(v1.new, "/")
> v1.new
[[1]]
[1] "A"

[[2]]
[1] "A" "B"

[[3]]
[1] "C"

[[4]]
[1] "D" "E" "F"

[[5]]
[1] "A"

[[6]]
[1] "C"

My next thought was to coerce the list into a data frame, but  I ran
into an error because the list output from strsplit() does not contain
equal length vectors.

> v1.cols <- data.frame(v1.new, check.rows=FALSE)
Error in data.frame("A", c("A", "B"), "C", c("D", "E", "F"), "A", "C",  :
  arguments imply differing number of rows: 1, 2, 3


How can I create a data frame from the unequal length vectors that
result from strsplit(my.df$v1)?

Am I going about this the wrong way? I have also tried to use
colsplit{reshape} without success.

Thank you for any advice you can offer. I hope the answer to this
question is not too obvious.

__
R-help@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.