vendredi 17 avril 2015

Converting a exponential a string

I am trying to assign names in a new column based on the values of an adjacent column. I am using a function which takes a pattern and replaces it with another:



sub<-function(pattern, replacement, x, ...) {
result <- x
for (i in 1:length(pattern)) {
result <- gsub(pattern[i], replacement[i], result, ...)
}
result
}


I can take the following:



df$x<-as.character(df$x)

df$y<-(sub(c("1A1","1B2", "1.00E+01"),
c("P1", "P2", "P3"), df$x))


So, df$y will now contain P1, P2 where 1A1, 1B2 are present in df$x. However, "1.00E+02" does not give P3 in df$y, 1.00E+02 is copied to it.


Am I missing something about exponents and how they are read as patterns?


Here is an example of my data table where df$x is ID and df$y is Name:



Name ID
p1 1A1
p2 1B2
1.00E+01 1.00E+01


Any help would be appreciated. Thanks MF


Aucun commentaire:

Enregistrer un commentaire