[R] Having some Trouble Data Structures

arun smartpink111 at yahoo.com
Sun Oct 28 17:55:50 CET 2012


Hi,

In addition to using paste(), you can also try this:
dat1<-data.frame(ID=formatC(0001:0010,width=4,flag="0"),No_of_Effectors=rep(3,10))
dat1<-within(dat1,{ID<-as.character(ID)})
list1<-lapply(1:nrow(dat1),function(x) sample(1:10000,3,replace=TRUE))
dat2<-data.frame(dat1,Effectors=I(list1))
 str(dat2)
#'data.frame':    10 obs. of  3 variables:
# $ ID             : chr  "0001" "0002" "0003" "0004" ...
# $ No_of_Effectors: num  3 3 3 3 3 3 3 3 3 3
# $ Effectors      :List of 10
 # ..$ : int  6155 979 3079
  #..$ : int  690 5515 9469
 # ..$ : int  903 7439 7582
  #..$ : int  9788 5930 7456
  #..$ : int  8106 8319 2396
  #..$ : int  8050 5299 264
  #..$ : int  5558 7401 8865
  #..$ : int  7178 7273 4065
  #..$ : int  2135 75 7571
  #..$ : int  6652 9900 2313
  #..- attr(*, "class")= chr "AsIs"
head(dat2)
#    ID No_of_Effectors    Effectors
#1 0001               3 6155, 97....
#2 0002               3 690, 551....
#3 0003               3 903, 743....
#4 0004               3 9788, 59....
#5 0005               3 8106, 83....
#6 0006               3 8050, 52....


BTW, I had a line of code in my previous reply which was not required and it will not work.  


#list1<-lapply(1:nrow(dat1),function(x) paste(sample(1:10000,3,replace=TRUE)),sep=",")
A.K.





----- Original Message -----
From: Benjamin Ward (ENV) <B.Ward at uea.ac.uk>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Sunday, October 28, 2012 5:32 AM
Subject: [R] Having some Trouble Data Structures

Hi All,

I'm trying to run a simulation of host-pathogen evolution based around individuals.
What I need to have is a dataframe or table of some description - describing all the individuals of a pathogen population (so far I've implemented this as a matrix):

         ID         No_of_Effectors                   Effectors (Sequences)
  [1,] 0001              3                   ##   3 Random Numbers ##

There will be many such rows for many individuals. They have something called effectors, the number of which is randomly generated, so say you get 3 in the No_of_Effectors column. Then I make R generate 3 numbers from between 1 and 10,000, this gives me three numerical representations of genes. These numbers will be compared to a similar data structure of the host individuals who have their immune genes with similar numbers.

My problem is that obviously I can't stick 3 numbers in one "cell" of the matrix (I've tried) :

Pathogen_Individuals[1,3] <- c(2,3,4)
Error in Pathogen_Individuals[1, 3] <- c(345, 567, 678) :
  number of items to replace is not a multiple of replacement length

In future I'm also going to have more variables such as whether a gene is expressed. Such information may require a matrix in itself - something like:


        Effector ID             Sequence                  Expressed?
  [1,]     0001              345,567,678                       1 (or 0).

Is there a way then I can put more than one value in the cell like a list of values, or a way to put objects in a cell of a data frame, matrix or table etc. Almost an inception deal - data structures nested in a data structure? If I search for things like "insert list into matrix" I get results like how to turn one into another, which is not what I think I need to be doing.

I have been considering having several data structures not nested in each other, something like for every individual create a new matrix object with the name Effectors_[Individual_ID] and some how get my simulation loops operating on those objects but I find it hard to see how to tell R all of those matrices are to be included in an operation, as you can all lines of a data frame for example with for loops.
This is strange for me because this model was written in a macro-code for another program which handles data in a different format and layout to R.

My problem is I think, each individual in the model has many variables - in this case representations of genes. So I'm having trouble getting my head about this.

Hopefully someone more experienced will be able to offer advice or a solution, it will be very appreciated.

Many Thanks,
Ben Ward (ENV, UEA & The Sainsbury Lab, JIC).

P.S. I have searched previous queries to the list, and I'm not sure but this may be useful for relevant:


Have you thought of using a list?

> a <- matrix(1:10, nrow=2)
> b <- 1:5
> x <- list(a=a, b=b)
> x
$a
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

$b
[1] 1 2 3 4 5

> x$a
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10
> x$b
[1] 1 2 3 4 5

oliveoil and yarn datasets have been mentioned.





    [[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.





More information about the R-help mailing list