[R] names wise flge values

arun smartpink111 at yahoo.com
Fri Mar 21 23:04:16 CET 2014


Hi,
dat <- structure(list(Cname = c("chiyyan", "saikiran", "niranjan", "yeswanth", 
"anitha", "krishna"), Wname = c("chyyan", "saikira", "nirnjan", 
"eswanth", "anita", "krisna")), .Names = c("Cname", "Wname"), class = "data.frame", row.names = c(NA, 
-6L))
output <- structure(list(Cname = c("chiyyan", "saikiran", "niranjan", "yeswanth", 
"anitha", "krishna"), Wname = c("chyyan", "saikira", "nirnjan", 
"eswanth", "anita", "krisna"), starting = c(1L, 1L, 0L, 1L, 0L, 
0L), middle = c(0L, 0L, 1L, 0L, 0L, 1L), ending = c(0L, 0L, 0L, 
0L, 1L, 0L)), .Names = c("Cname", "Wname", "starting", "middle", 
"ending"), class = "data.frame", row.names = c(NA, -6L))



#Here, I am not following the logic. 

output[1:2,]
#     Cname   Wname starting middle ending
#1  chiyyan  chyyan        1      0      0
#2 saikiran saikira        1      0      0  ##1 for "ending"?

You may try:
library(Biostrings)

res <- cbind(dat, setNames(as.data.frame(t( sapply(seq_len(nrow(dat)),function(i) {p1 <-pairwiseAlignment(dat[i,2],dat[i,1]); m1 <- as.matrix(p1);  m2 <- matrix(0,nrow=1,ncol=3,dimnames=list(NULL,c("starting","middle","ending"))); me <- median(seq(ncol(m1))); vec1 <- c(floor(me),ceiling(me)); indx <- which(m1=="-"); if(indx < vec1[1]) {m2[,"starting"] <-1; m2} else if(indx > vec1[2]) {m2[,"ending"] <- 1; m2} else m2[,"middle"] <- 1; m2 }))), c("starting","middle","ending")))


 res
     Cname   Wname starting middle ending
1  chiyyan  chyyan        1      0      0
2 saikiran saikira        0      0      1
3 niranjan nirnjan        0      1      0
4 yeswanth eswanth        1      0      0
5   anitha   anita        0      0      1
6  krishna  krisna        0      0      1

output
     Cname   Wname starting middle ending
1  chiyyan  chyyan        1      0      0 #here "i" 3rd letter missing from Wname. 
2 saikiran saikira        1      0      0
3 niranjan nirnjan        0      1      0
4 yeswanth eswanth        1      0      0
5   anitha   anita        0      0      1
6  krishna  krisna        0      1      0 ## here "h" is the 5th letter.  
So, I am not sure about your classificiation of middle.  In both "chiyyan" and "krishna", 4th letter is the middle one.  So, I don't understand how you got 1 for "starting" in the case of "chiyyan" while 1 is "middle" for "krishna".
A.K.














Hi,Users I have troble in one task is give below plz...help me Cname Wname
chiyyan chyyan
saikiran saikira
niranjan nirnjan
yeswanth eswanth
anitha anita
krishna krisna here Cname means Correct name and Wname means Wrong name it's letter's
missing,so in Wname column identify which place letter missing like 
starting,middle,ending place.if starting letter missing given numeric
1 remainings 0's.finaly out put is Cname Wname         starting       middle     ending
chiyyan chyyan         1             0           0  
saikiran saikira       1             0           0
niranjan nirnjan       0             1           0 
yeswanth eswanth       1             0           0
anitha anita           0             0           1
krishna krisna         0             1           0 plz...solve this



More information about the R-help mailing list