2

Is it possible to aggregate or use subset whilst using a regular expression simultaneously in R?

The problem I am trying to solve is this: I have a data frame called 'wpbCellFeatures' with multiple columns including a unique identifier 'rowColFoVCell':

   rowColFoVCell wpbCount meanFeret meanPerim  meanCirc   meanAR meanRound meanSolidity
1   001001001001       38  1.182632  3.047368 0.7560526 1.948947 0.6036842    0.8289474
2   001001001002        8  1.886250  4.493750 0.7537500 2.365000 0.5350000    0.8325000

This column contains numbers '001001001001', '001001001002', '001001001003', ... ... , '001003004002', ... etc. The numbers forming this ID correspond to row number, column number, field of view and cell number, so for example '001003004002' is the first row, third column, fourth field of view and second cell.

I would like to select all identifiers with rows between 1 and 3 for example, and aggregate into a new data frame. How can I do this in R, I think it will involve using aggregate and regular expressions but I am not so familiar with this?

Thanks

2 Answers 2

8

Instead of fussing with regex, I would just split that first column into the respective columns by using read.fwf (or substr or a related function). Then, bind that back into your original dataset, and use aggregate and so on as you normally would.

toBind <- read.fwf(file = textConnection(as.character(mydf$rowColFoVCell)), 
                   widths = c(3, 3, 3, 3), colClasses = "character", 
                   col.names = c("Row", "Col", "FoV", "Cell"))
cbind(toBind, mydf)
#   Row Col FoV Cell rowColFoVCell wpbCount meanFeret meanPerim  meanCirc   meanAR meanRound
# 1 001 001 001  001  001001001001       38  1.182632  3.047368 0.7560526 1.948947 0.6036842
# 2 001 001 001  002  001001001002        8  1.886250  4.493750 0.7537500 2.365000 0.5350000
#   meanSolidity
# 1    0.8289474
# 2    0.8325000

Here, I'm starting with "mydf" as:

mydf <- structure(list(rowColFoVCell = c("001001001001", "001001001002"), 
                  wpbCount = c(38L, 8L), meanFeret = c(1.182632, 1.88625), 
                  meanPerim = c(3.047368, 4.49375), 
                  meanCirc = c(0.7560526, 0.75375), 
                  meanAR = c(1.948947, 2.365), 
                  meanRound = c(0.6036842, 0.535), 
                  meanSolidity = c(0.8289474, 0.8325)), 
                  .Names = c("rowColFoVCell", "wpbCount", "meanFeret", 
                             "meanPerim", "meanCirc", "meanAR", "meanRound",
                             "meanSolidity"), 
                  class = "data.frame", row.names = c(NA, -2L))
0

After some research, I have found that using subset and regular expressions was the way to go rather than aggregate. Here is how I solved this difficulty:

wpbCellFeaturesControl <- subset(wpbCellFeatures, grepl("^[0-9]{3}(00[1-3])[0-9]{6}", wpbCellFeatures$rowColFoVCell))

grepl matches the pattern within the quotation marks in the rowColFoVCell column,
^ indicates to search starting at the beginning of the string
[0-9]{3} digits zero to nine 3 times
(00[1-3]) search for pattern 001, 002, and 003
[0-9]{6} digits zero to nine six times

2
  • 2
    I still think this is overkill if your column is indeed fixed width, which it is. In the long run, you would also do better to have that information as separate columns. The separate columns can always be pasted together later if necessary. Dec 17, 2013 at 9:41
  • Thanks for your help and I agree your solution would work, but I am trying to keep the code as concise as possible so in this instance prefer to use regular expressions. Dec 17, 2013 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.