Reputation: 409
I have a user-defined function to replace text patterns. This seems to work on a data frame but not a tibble.
fixcontents <- function(mydf, mypattern1, mypattern2, mycol) {
mydf[ ,mycol] <- sub(mypattern1, mypattern2, mydf[ ,mycol])
return(mydf)
}
mydf1 <- data.frame(col1 = c(1,2), col2 = c("aaa", "bbb"))
mytbl1 <- tibble(col1 = c(1,2), col2 = c("aaa", "bbb"))
fixcontents(mydf1, "(b{3})", "\\1X", 2) # works
mydf1
col1 col2
1 1 aaa
2 2 bbbX
fixcontents(mytbl1, "(b{3})", "\\1_", 2) # does not work (??)
mytbl1
# A tibble: 2 x 2
col1 col2
<dbl> <chr>
1 1 aaa
2 2 bbb
Why this behavior, and how do you manipulate data in a tibble?
Upvotes: 1
Views: 35
Reputation: 388982
Subsetting using [
works differently on tibbles. Subsetting dataframe returns a vector whereas tibble returns tibble back.
mydf1[, 2]
#[1] aaa bbb
#Levels: aaa bbb
mytbl1[, 2]
# A tibble: 2 x 1
# col2
# <chr>
#1 aaa
#2 bbb
Try using [[
to subset
fixcontents <- function(mydf, mypattern1, mypattern2, mycol) {
mydf[[mycol]] <- sub(mypattern1, mypattern2, mydf[[mycol]])
return(mydf)
}
fixcontents(mydf1, "(b{3})", "\\1X", 2)
# col1 col2
#1 1 aaa
#2 2 bbbX
fixcontents(mytbl1, "(b{3})", "\\1_", 2)
# A tibble: 2 x 2
# col1 col2
# <dbl> <chr>
#1 1 aaa
#2 2 bbb_
Upvotes: 1