I would like to be able to refer to columns by name and index in one vector. As example I specify only:
EDIT: I changed the order of the original vector as I want the order to not matter.
columns <- c(1:7, "j", 8, "i")
I would then like to retrieve the names of the index 1 to 9 and add them to the vector (in the correct place). I have a general idea, but coding wise I am not getting very far:
library(data.table)
df <- fread(
"a b c d e f g h i j
1 2 3 4 5 6 7 8 9 10",
header = TRUE
)
function(data, columns){
nums <- as.numeric(columns)
named_columns <- ?
nums <- nums[!is.na(nums)]
name_nums <- colnames(df)[nums]
all_columns <- setdiff(named_colums, name_nums)
# Order of the original vector?
column_names <- result
}
and then return it to the vector so that the outcome will be:
column_names <- c("a", ..., "j", "h", "i")
Could anyone help me to get a bit further?