Here is one way of doing it:
> x <- c("C/G", "CC", "GG", "CG",
"G/T", "GG", "TT", "GT",
"C/T", "CC",
+ "TT", "CT", "A/G", "AA",
"GG", "AG", "A/C", "AA", "CC",
"AC",
+ "A/T", "AA", "TT",
"AT")> # convert to a matrix with 4 columns
> x <- matrix(x, ncol=4, byrow=TRUE)
> x
[,1] [,2] [,3] [,4]
[1,] "C/G" "CC" "GG" "CG"
[2,] "G/T" "GG" "TT" "GT"
[3,] "C/T" "CC" "TT" "CT"
[4,] "A/G" "AA" "GG" "AG"
[5,] "A/C" "AA" "CC" "AC"
[6,] "A/T" "AA" "TT"
"AT"> # now substitute for the last 3 columns
> result <- apply(x, 1, function(a){
+ # substitute for the first character
+ d1 <- gsub(substr(a[1], 1, 1), '1', a[2:4])
+ # substitute for the second
+ gsub(substr(a[1], 3, 3), '2', d1)
+ })> result
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] "11" "11" "11" "11" "11"
"11"
[2,] "22" "22" "22" "22" "22"
"22"
[3,] "12" "12" "12" "12" "12"
"12"> # output as vector
> as.integer(as.vector(result))
[1] 11 22 12 11 22 12 11 22 12 11 22 12 11 22 12 11 22
12>
On 5/22/06, yohannes alazar <hannesalazar@gmail.com>
wrote:>
> I have a file that has 90 columns and 20,000 rows and looks like
> C/G CC GG CG G/T GG TT GT C/T CC TT CT A/G AA GG AG A/C AA CC AC A/T AA
> TT AT
>
> I want to write a code that will read through each row first the first
> looks
> at the first column and then replace the three columns with 12 if it is
> the
> same as the first column e.g. third column 11 if it is a repeat of the
> first
> alphabet like the second column and 22 if it a repeat of the second
> alphabet.
>
> Can you please give some hint how I can start coding for this, like
> command
> names, I am not a programmer and an R beginner. my out put should look
> like
> this
>
> 11 22 12 11 22 12 11 22 12 11 22 12 11 22 12 11 22 12
>
> Thank you in advance.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390 (Cell)
+1 513 247 0281 (Home)
What is the problem you are trying to solve?
[[alternative HTML version deleted]]