[R] eliminate characters of one data.frame col, using another column

2009-10-21 Thread Luis F
Dear Maling list,

I have a data.frame with three columns. I want to produce a fourth
column which is result of eliminating the characters present in the
second and third colum from the first.

Example:
a b   c
1 f f
2  j h
3  gg

I want the result:
ff
hhh


I can get what I want using the code below. But it's slow for big files
(which I have) and most likely there's a better way to do this

1) is there a function that would do the same as FUN.remove2ndCol
2) is there a way to avoid the apply for every row?

Thanks,
Tiago

###

dfIn - data.frame(a=c('', '', ''), b=c('f', 'j', 'g'),
c=c('f', 'h', 'g'))

FUN.remove2ndCol - function(vec){

 vec.sub - sub(vec['b'], '', vec['a'])
 vec.sub - sub(vec['c'], '', vec.sub)

 return(vec.sub)
}

dfIn$Output - apply(dfIn, 1, FUN.remove2ndCol)

[[alternative HTML version deleted]]

__
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] only T becomes logical colum with read.table

2008-07-18 Thread Luis F
Hello,

I am a biologist and I am dealing with files composed of columns with T or A
or C or G (example below)

Sometimes a column is composed of only T
and in that case, the columns becomes a logical column TRUE  (example below)

I know that the argument as.is controls the turns off turning a column
into a factor, instead becoming a character
Is there something similar for Iogical? (I could not find it)

I can solve this problem by doing something like:
#
FUN.changeLogi - function(df){
  if(identical(all.equal(all(df), T), T)) df - 'T'
  return(df)
}
xx - do.call('cbind', lapply(xx, FUN.changeLogi))

but it seems very cumbersomeÂ…

Thanks,
Tiago Magalhães

produce a text file example.txt

A C A T C G F
A A C T C G F
A C A T C G F
A G A T C G F
T A C T C G F

xx - read.table('example.txt')
str(xx)
xx - read.table('~/Desktop/temp.txt')
 $ V1: chr  A A A A ...
 $ V2: chr  C A C G ...
 $ V3: chr  A C A A ...
 $ V4: logi  TRUE TRUE TRUE TRUE TRUE
 $ V5: chr  C C C C ...
 $ V6: chr  G G G G ...
 $ V7: logi  FALSE FALSE FALSE FALSE FALSE

[[alternative HTML version deleted]]

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