Sunday, 11 August 2013

creating a new column in data frame conditoned on values of a previous column using a loop

creating a new column in data frame conditoned on values of a previous
column using a loop

Lets say I have a data frame that has three columns named "SNP" (unique
IDs), "A1" and "P".
SNP | A1 | P
rs12 A 0.3426
rs16 B 0.0083
rs18 B 0.0046
I am trying to create new column in the data named "A2" based on if column
"A1" has an A then R will put a B in column "A2" and if column "A1" has a
B to put an A in column "A2"
This is what I have, I'm not sure if I'm tackling this the right way but
here's a try.
data<-(read data in)
allele<-function(x) {
isOkay<-all(data[data$A2=="A"])
flag<- ifelse(isOkay, A, B)
return(flag)
}
data <- split(data, data$SNP)
A2 <- sapply(data, allele)

No comments:

Post a Comment