We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hello.
I want to modify the columns containing a given pattern in their names.
Say I have this toy example: myDT <- data.table(cod1=1:20, cod2=20:1, other1=rnorm(20), other2=rnorm(20))
myDT <- data.table(cod1=1:20, cod2=20:1, other1=rnorm(20), other2=rnorm(20))
And I want to convert the content of the columns whose name start by "cod" to factors.
With dplyr I can use this compact notation: myDT %>% mutate(across(contains("cod"),as.factor))
myDT %>% mutate(across(contains("cod"),as.factor))
With data.table I would use this:
cols <- grep("cod", names(myDT), value = T) myDT[,(cols):= lapply(.SD, as.factor), .SDcols = cols]
It's much longer and it generates new variables we don't need anymore. Is there any shorter/simpler/faster alternative?
I have tried this: myDT[, (names(.SD)) := lapply(.SD, as.factor) , .SDcols = patterns("cod")] but it doesn't work.
myDT[, (names(.SD)) := lapply(.SD, as.factor) , .SDcols = patterns("cod")]
The text was updated successfully, but these errors were encountered:
There is FR for that already, and maybe even PR as well.
Sorry, something went wrong.
duplicate of #795, will be fixed in #4163
No branches or pull requests
Hello.
I want to modify the columns containing a given pattern in their names.
Say I have this toy example:
myDT <- data.table(cod1=1:20, cod2=20:1, other1=rnorm(20), other2=rnorm(20))
And I want to convert the content of the columns whose name start by "cod" to factors.
With dplyr I can use this compact notation:
myDT %>% mutate(across(contains("cod"),as.factor))
With data.table I would use this:
It's much longer and it generates new variables we don't need anymore.
Is there any shorter/simpler/faster alternative?
I have tried this:
myDT[, (names(.SD)) := lapply(.SD, as.factor) , .SDcols = patterns("cod")]
but it doesn't work.
The text was updated successfully, but these errors were encountered: