dataframe - How to add two columns in a data frame to create a new third column, based on sub-strings of column names, in R? -


lets consider simple data frame follows :

id area1feature1 area1feature2 area2feature1 area2feature2 1  1             2             3             4 2  3             6             1             5 

now combine feature1 areas, feature2 areas , on, , create new sumoffeature1, sumoffeature2, etc.

so expected output :

id area1feature1 area1feature2 area2feature1 area2feature2 sumoffeature1 sumoffeature2 1  1             2             3             4             4             6 2  3             6             1             5             4             11 

how can match columns based on sub-string , combine them create new columns data frame?

the way did follows : let input data frame.

features_to_be_combined <- c('feature1', 'feature2') locations <- sapply(features_to_be_combined, grep, colnames(input)) feature1_locations <- locations[, 'feature1'] sumoffeature1 <- rep(0, dim(input)[1]) (i in 1:length(feature1_locations)) {     sumoffeature1 <- sumoffeature1 + input[, feature1_locations[i]] } 

now remains repeat same procedure feature2 , add newly created features, namely sumoffeature1 , sumoffeature2, input data frame. sure there better way (may using apply again on combined features), worked me expected.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -