Sunday 15 August 2010

r - Selecting column sequences and creating variables -


I was wondering whether there was a way of selecting specific columns through a sequence and to create a new variable from it

For example, if I have 8 columns with 8 observations, how can I create 4 variables which gradually selects 2 rows? This is very big compared to my datasets and I have pasted 1416 variables (I have pasted a link to the spreadsheet below, which represents the first column and row names) with 62 comments. I want to create new dataframes by the name of this site 1-12. Then site 1 = DF [, 1: 117]; Site 2 = DF [, 119: 237] etc.

I plan to use this code for future datasets with even more variables so any type of loop or sequence function will be very effective if anybody receives it on the light ?

Thanks in advance

James

ps @nrussell I copied the production of specified address your bottom and pasted, It is as follows as a series of numbers displayed.

dput (z [, 1:10]) structure (list ( 1 = c (0, 0, 0, 0, 0, 0, 0, 0) ), .0311410340342049, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 , .0207444023791158, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, .0312971643732546, 0, 0, 0, 0, 0, 0, 0, 0, 0, .0376287494579976, 0 , 0, 0, 0, 0, 0, 0), ......... 10 = c (0, 0, 0, 0, 0.119280313679 9, 16, 0, 0, 0.301029995663981, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.715681882079494, 0.136831816210901, 0, 0, 0, .0273663632421801, 0, 0, 0, .0547327264843602, 0, 0, 0, 0, .0231561535126139, 0, 0,0, 0,03,08,9986991944, 0, 0.0752574989159953, 0.159368821233872, 0.0272640716982664, 0.0177076468037636, 0, 0.120411998265592, 0, 0, 0.0322532138211408, 0.0250858329719984, 0, 0, 0, 0.119280313679916, 0, 0.172922500085254, 0.225772496747986, 0, 0, 0, Namankna = c ( "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"), class = "data.frame", pun = name = C (NA, -62L))

Split dataset setNames (lapply (partition -; GL

  lst & lt By creating a group index, the column is equal to '118' with '1416' column ('df') (1: ncol ( DF), as.numeric (GL (ncol (DF), 118, ncol (DF))), function (i) df [, i]), paste0 ('site', 1:12))  

Or you can create 'lst' without using partition

 . - setNames (lapply (sec (1, ncol (df), = 118), function (i) df [i: (i + 117)], paste 0 ('site', 1:12))  < / Pre> 

If we need to create 12 dataset objects in the global environment, then list2env is an option (I myself prefer to work within 'lst')

< pre> list2env ((8) using a small dataset ( 'df1') with columns
  lst1 & lt; - setNames (lapply () split (1: ncol ( df1), as.numeric (gl (ncol (df1), 2, N Call (df1)))), function (i) df1 [, i]), paste 0 ( 'site', 1: 4)) list2env (lst1 , Envir = globelEnv) head (site1, 3) # v1v2 # 1 6 12 # 2 4 7 # 3 14 14 s (Site 4,3) # V7V8 # 1 10 2 # 2 5 4 # 3 5 0  

Data
  set.seed ( 24) df1 & lt; - as.data.frame (matrix (sample (0:20, 8 * 10, location = TRUE), ncol = 8))  


No comments:

Post a Comment