Monday, 15 March 2010

grep - Systematic replace part of variable name with 1st element of an associated R vector -


I have dataframe in which the first element of an associated 'name' vector is related to the respective named vectors. I am trying to change the meaningless number with the first element of the respective name vector.

Here is an example dataframe:

  df < - data.frame (data .0.name = c ("A", "A", "A"), data 2.0.one_minute_ago = c (1,2,1), data 2.0.one_hour_ago = c (2,2 , 3), Data.1.name = c ("B", "B", "B"), data .1.one_minute_ago = c (3,3,2), data .1.one_hour_ago = c (5, 6,2)) ` 

Each number.name is associated with the formation of vector (in this case either A or B) and each Number.time is associated with a time dimension. So, data.0.one_minute_ago in fact you have a_mint_go number.

What I would like to do (because I have a large dataset with a lot of changes) to change the position of the number to construct.dimension To change from, and definitely do this for each number. to 0: 9

I have written some grip codes to start with this work, but to no avail (I got stuck with retaining everything after the number Grep ("data [0-9] .name", name (df), perl = TRUE) as.character (df [1, 1]) as Ascharchar (df [1, 4]) as.character (name (df [2]) as.character (name (df [3]) as.character (name (df [5]) as.character ( Name (df [6]) df.1 <- (df [1, grep ("data. [0-9] .name", name (df))] df.1 & lt; - (df [1, grep ("Data. [0-9] .name", name (df))) df.1 & lt; - data.frame (loop (df.1, as.character), stringsfactors = FALSE) & lt; - as.character (df.1 [1, c (1: 2))]

here first and second element of cleverness 0.name / 0.dimension and 1.name / 1.dimension are the structures / code> respectively [2] < / Code>

From there, I'm doing a lot of code to contain some names (df) [/ p>

 ]  

edit Do: Here is the desired variable name output: When changing the variable name (and of course, it is to maintain the value associated with variable names:

  data.A.name data.A.one_minute_ Before data.A.Or_Hor_go data.b.name.data.b. edit 2: In my true dataset, the number of iterations per dimension (i.e., min_ege_go, a_home_go, e_de_go) can vary in the construction (i.e., two dimensions For constructing one one and 3 for the other, and 9 for the other). I would like the solution to take it into consideration.  

Here is a modified sample dataset to reflect this subtlety:

  d F & LT; - data.frame (data.0.name = c ("a", "a", "a"), data 2.0.one_minute_ago = c (1,2,1), da ta.0.one_hour_ago = C (2,2,3), data1.name = c ("b", "b", "b"), data .1.one_minute_ago = c (3,3,2), data1. On_hor_guo = c (5, 6,2), data 2.2.name = c ("c", "c", "c"), data 2. 2. Forest_mint_go = c (3,3,2), data2.a_hour_ago = c (5,6,2), data2.on_de_go = c (3, 2, 3))  

We make a group 'index' based on 'number' in column names. Get an element from the column with the suffix ('r1') as the column name partition 'name' based on 'indx' ('lst'), with 'R1', each element of 'lst' Use 'map' and gsub to change 'number' in

  indx < - gsub ('[0-9] +', '', name (df)) lst & lt; - Division (Name (DF), Indx) r1 & lt; - as.character (unlisted (df [1, grep ('name', names (df))))) lst2 & lt; - Map (function (x, y) gsub ('[0-9] +', y, x), LCT, R1) Name (df)  

No comments:

Post a Comment