Sunday 15 September 2013

Creating new set of columns in a dataframe in R from existing columns with names in a vector -


I have dataframe in areas such as X-ray, Y, A, B in R:

  D & LT; - data.frame (x = cic (1,26), y = sic (1,26, = 2), a = letters [sq (1,26)], b = letter [sq (126)]  

and a vector of the column:

  colnames <- c ('a', 'b')  

I would like to add additional fields with values ​​based on the dataframe A_mod and B_mod to a function which processes the value of resp A and B of the same line.

That's why I am X, Y, A, B And would like to end with a data frame with A_m and B_mode in A_mod and B_mod Calculations are calculated by resp.

  A_mod = tolower (A)  

and

  B_mod = example To type (B) (only a duplicate example)  

I enter the column A , B and A_mod And B_mod , but instead use vector colnames. In reality, my dataframe has 30 columns, so it is hard to write all by hand (and the amount of column will increase).

(colnames, '_ mod')] & lt; - lapply (d [colnames], tolower head (d, 3) # xy ab a_mod b_mod # 1 1a aha # 2 2 3b bbb # 3 ccc


No comments:

Post a Comment