r - Rename columns in data frame using "arithmetic progression" -
suppose i've got following data.frame
composed of multiple rows (not displayed here) , 31 columns. first 1 (and should remain) labelled "gene_id" and, second way thirtieth column have weird names, follows:
|gene_id | weird1| weird2|all_the_way_to | weird30| |:-------|------:|------:|:--------------|-------:| |bpk282x | 4| 1|... | 7| |bpk282y | 5| 2|... | 8| |bpk282z | 6| 3|... | 9|
i rename 30 columns names pattern this: tpm_1, tpm_2, tpm_3, tpm_4.....tpm_30, resulting in following table:
|gene_id | tpm_1| tpm_2|all_the_way_to | tpm_3| |:-------|-----:|-----:|:--------------|-----:| |bpk282x | 4| 1|... | 7| |bpk282y | 5| 2|... | 8| |bpk282z | 6| 3|... | 9|
i rename columns individually by:
names(data.frame) <- c("gene_id", "tpm_1", "tpm_2", "tpm_3", ..., "tpm_30")
yet wondering if there way automatize process using function employ arithmetic progression on columns' names. in other words, trying find way rename columns pattern (tpm_followed number) in name of columns "tpm_n", "tpm_n+1", "tpm_n+2", tpm_n+3", way "tpm_n+30"
you can use paste0
colnames(df) <- paste0("tpm_", 1:30) #[1] "tpm_1" "tpm_2" "tpm_3" "tpm_4" "tpm_5" "tpm_6" "tpm_7" "tpm_8" "tpm_9" #[10]"tpm_10" "tpm_11" "tpm_12" tpm_13" "tpm_14" "tpm_15" "tpm_16" "tpm_17" "tpm_18" #[19]"tpm_19" tpm_20" "tpm_21" "tpm_22" "tpm_23" "tpm_24" "tpm_25" "tpm_26" "tpm_27" #[28] "tpm_28" "tpm_29" "tpm_30"
you can specify indexes of columns want change
colnames(df)[2:31] <- paste0("tpm_", 1:30)
Comments
Post a Comment