Dear List, I have this column/vector: vec <- c("function", "missing", "string") and want to compute a second column/vector: - value if the pattern "unc" is found: 1 - value if the pattern "iss" is found: 2 - value if none of the patterns is found: 0 This should be the result: > vec2 [1] 1 2 0 Any help? Tried it with grep, but the output is not as long as vec, so I'm lost a bit here. Thanks, Stefan
On 19/02/2009 9:26 AM, Stefan Uhmann wrote:> Dear List, > > I have this column/vector: > > vec <- c("function", "missing", "string") > > and want to compute a second column/vector: > - value if the pattern "unc" is found: 1 > - value if the pattern "iss" is found: 2 > - value if none of the patterns is found: 0 > > This should be the result: > > vec2 > [1] 1 2 0 > > Any help? Tried it with grep, but the output is not as long as vec, so > I'm lost a bit here.vec2 <- rep(0, length(vec)) vec2[grep("iss", vec)] <- 2 vec2[grep("unc", vec)] <- 1 Note that an entry containing both "unc" and "iss" will get a 1 according to this scheme. Duncan Murdoch
Seemingly Similar Threads
- Arules: R Crashes when running eclat with tidLists=TRUE
- Any better way of optimizing time for calculating distances in the mentioned scenario??
- findInterval and data resolution
- Help on creating a sequence of vectors
- transform 3 numeric vectors empty of 0/1