I have a data table which has some columns with long, ~20 letter names such as GTEX.12WSJ.1226.SM.5LU91. I have other columns with shorter, truncated versions of those names, like GTEX.12WSJ. I need to extract the long-named columns into a separate table from the short named columns. I want both types of columns to exist on separate columns, and I want the original table to basically disappear, like rm(dt) or something.
How do I go about doing this? The closest I've come is which(nchar(names(dt)) > 9) which creates a vector of numbers going from 1 to the number of columns which fit this condition.
You could use split.default and split the columns based on number of characters
list_df <- split.default(df, nchar(names(df)) > 9)
where individual dataframes can be extracted with list_df[[1]] and list_df[[2]].
For example, on this dataset we divide the data into two sets. One with columns greater than 2 characters and another with less than equal to 2 characters.
df <- data.frame(abc = 1:5, ab = 1:5, a = 6:10, abcd = 11:15)
split.default(df, nchar(names(df)) > 2)
#$`FALSE`
# ab a
#1 1 6
#2 2 7
#3 3 8
#4 4 9
#5 5 10
#$`TRUE`
# abc abcd
#1 1 11
#2 2 12
#3 3 13
#4 4 14
#5 5 15
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With