[R] create variables through a loop

Dennis Murphy djmuser at gmail.com
Thu Sep 22 23:54:03 CEST 2011


Hi:

Here's one approach with package reshape2. I copied the same data
frame 10 times to illustrate the idea, but as long as your data frames
have the same structure (same variables, same dimension), this should
work.

library('plyr')
library('reshape2')

# Use your example data as the template:
ds <- read.table(textConnection("
 probe_name chr_id position      y
1    C-7SARK      1   849467     10
2    C-4WYLN      1   854278     10
3    C-3BFNY      1   854471     10
4    C-7ONNE      1   874460     10
5    C-6HYCN      1   874571     10
6    C-7SCGC      1   874609     10"), header = TRUE, stringsAsFactors = FALSE)
closeAllConnections()

# Note how I use y for the response instead of arrayxx.

# Copy the data frame 10 times and put it into a list; these are
# placeholders for the real data frames
L <- lapply(seq_len(10), function(i) ds)

# Name the list components array01  - array10, ...
names(L) <- c(paste('array0', 1:9, sep = ''), 'array10')

# The advantage of using rbind in conjunction with ldply() is that it outputs
# the list names as an .id variable
u <- ldply(L, rbind)
head(u)

# Reshape the data so that the y values in each component are associated
# with the component name:
dcast(u, probe_name + chr_id + position ~ .id, value_var = 'y')
  probe_name chr_id position array01 array02 array03 array04 array05 array06
1    C-3BFNY      1   854471      10      10      10      10      10      10
2    C-4WYLN      1   854278      10      10      10      10      10      10
3    C-6HYCN      1   874571      10      10      10      10      10      10
4    C-7ONNE      1   874460      10      10      10      10      10      10
5    C-7SARK      1   849467      10      10      10      10      10      10
6    C-7SCGC      1   874609      10      10      10      10      10      10
  array07 array08 array09 array10
1      10      10      10      10
2      10      10      10      10
3      10      10      10      10
4      10      10      10      10
5      10      10      10      10
6      10      10      10      10

HTH,
Dennis

On Thu, Sep 22, 2011 at 10:07 AM, Changbin Du <changbind at gmail.com> wrote:
> HI, Dear R community,
>
> I am trying to created new variables and put into a data frame through a
> loop.
>
> My original data set:
>
> head(first)
>  probe_name chr_id position array1
> 1    C-7SARK      1   849467     10
> 2    C-4WYLN      1   854278     10
> 3    C-3BFNY      1   854471     10
> 4    C-7ONNE      1   874460     10
> 5    C-6HYCN      1   874571     10
> 6    C-7SCGC      1   874609     10
>
>
> I have 48 other array data from a list result.fun
> array2=result.fun[[1]]
> array3=result.fun[[2]]
> .
> .
>
> I want the following results:
>
>  probe_name chr_id position array1 array2 array3
> 1    C-7SARK      1   849467     10     10       10
> 2    C-4WYLN      1   854278     10     10       10
> 3    C-3BFNY      1   854471     10      10       10
> 4    C-7ONNE      1   874460     10     10       10
> 5    C-6HYCN      1   874571     10     10       10
> 6    C-7SCGC      1   874609     10     10       10
>
>
> I used the following codes:
>
> for (i in 2:3) {
>
> assign(lab=paste("array", i, sep=""), value=result.fun[[i-1]])
>
> first <-cbind(first, lab)
>
> }
>
> *Error in assign(lab = paste("array", i, sep = ""), value = result.fun[[i -
> :
>  unused argument(s) (lab = paste("array", i, sep = ""))*
>
>
> Can anyone give some hits or helps?
>
> Thanks so much!
>
>
>
>
>
>
> --
> Sincerely,
> Changbin
> --
>
>        [[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