[R] Help

Christoph Buser buser at stat.math.ethz.ch
Fri Jul 28 11:13:00 CEST 2006


Dear Claire

Thank you for providing an example, but it is not totally clear
to me how you want to transform your data. For example in row 1,
you have value 1 in column f and n, but the results after
transformation are not similar "A G" and "A A". Similar problem
for column h and i. They transform both to "G G" although the
original values 0,2 differ?

Furthermore it is not clear if you want to have a vector with
two elements as result of the transformation ->  "A"  "G" 
or one character "A G".
In the first case you cannot work with data.frames or matrices,
but you must use a list. An entry of a data.frame is always a
single element.

Regarding changing elements if they fulfil a condition, it is
not necessary to work with loops. E.g. for a vector

v[v==5] <- 7

changes all values of v which are 5 to 7. 
Please read "An Introduction to R" on the R home page about
indexing vectors, matrices, and so on.

The following code can show you an example how I understood your
problem and one possible solution. I hope it helps you.

## Create data.frame
DF <- data.frame(a = c(14,58), b = c(24,42), c = c("rcvf", "grde"),
                 d = c("AG","AC"), e = c(5,2), f = c(2,5), g = c(1,0),
                 h = c(0,5), i = c(2,1), n = c(1,0))
## work with matrix to avoid problems with factors
DF <- as.matrix(DF)
## transformation function
myfun <- function(v)
{
  v[v=="5"] <- "00"
  v[v=="1"] <- v["d"]
  v[v=="0"] <- paste(rep(substr(v["d"],1,1), 2), collapse = "")
  v[v=="2"] <- paste(rep(substr(v["d"],2,2), 2), collapse = "")
  v
}
## apply transformation function to all rows
result <- t(apply(DF, 1, myfun))


One final point. Providing examples is a good way to obtain fast
help from others. Could you please try to give some R-code in
the example as well to ease the handling for others, e.g. if you
provide an example data.frame, please include the code to
produce it:

## Create data.frame
DF <- data.frame(a = c(14,58), b = c(24,42), c = c("rcvf", "grde"),
                 d = c("AG","AC"), e = c(5,2), f = c(2,5), g = c(1,0),
                 h = c(0,5), i = c(2,1), n = c(1,0))

Have a nice week and enjoy further working with R.

Regards,

Christoph Buser

--------------------------------------------------------------
Christoph Buser <buser at stat.math.ethz.ch>
Seminar fuer Statistik, LEO C13
ETH Zurich	8092 Zurich	 SWITZERLAND
phone: x-41-44-632-4673		fax: 632-1228
http://stat.ethz.ch/~buser/
--------------------------------------------------------------


claire pujoll writes:
 > Dear R members,
 > 
 > Sorry for this question. I have a dataframe (please see the example) and i
 > should do the following transformation:
 > 
 > DF
 > a    b    c       d      e  f   g   h   i   n
 > 14  24  rcvf    AG    5  2  1   0   2   1
 > 58  42  grde   AC    2  5  0   5   1   0
 > 
 > 
 > I should transforme my DF like :
 > 
 >  a    b    c        d       e        f         g       h       i        n
 > 14  24  rcvf     A G    0 0   G G    A G  G G   G G   A  A
 > 58  42  grde    A C    C C  0 0     A A    0 0    A C   A A
 > 
 > i.e: when DF[i,j]==5 =>  DF[i,j] <- 0 0
 >      When DF[i,j] == k (!=0) i should look to d column to do the
 > transormation
 > 
 > DF is 200000 * 10000 so i cant use loops.
 > 
 > Thanks for your help,
 > Claire
 > 
 > 	[[alternative HTML version deleted]]
 > 
 > ______________________________________________
 > 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
 > and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list