Hello, Using R, I've loaded a .cvs file comprised of several hundred rows and 3 columns of data. The data within maps the output of a triaxial accelerometer, a sensor which measures an object's acceleration along the x,y and z axes. The data for each respective column sequentially oscillates, and ranges numerically from 100 to 500. I want create a function that parses the data and detects patterns across the three columns. For instance, I would like to detect instances when the values for the x,y and z columns equal 150, 200, 300 respectively. Additionally, when a match is detected, I would like to know how many times the pattern appears. I have been successful using str_detect to provide a Boolean, however it seems to only work on a single vector, i.e, "400" , not a range of values i.e "400 - 450". See below: # this works> vals <- str_detect (string = data_log$x_reading, pattern = "400")# this also works, but doesn't detect the particular range, rather the existence of the numbers> vals <- str_detect (string = data_log$x_reading, pattern = "[400-450]")Also, it appears that I can only apply it to a single column, not to all three columns. However I may be mistaken. Any advice on my current approach or alternativea I should consider is greatly appreciated. Many thanks, Vincent [[alternative HTML version deleted]]
You could define a simple function to detect whether a value is within a given range. For example, inrange <- function(vec, range) { !is.na(vec) & vec >= range[1] & vec <= range[2] } x <- 1:30 inrange(x, c(5, 20)) If you wanted to apply this function to all three columns at once, you could use apply(). For example, apply(data_log, 2, inrange) Jean On Thu, Jun 26, 2014 at 11:17 AM, VINCENT DEAN BOYCE < vincentdeanboyce@gmail.com> wrote:> Hello, > > Using R, I've loaded a .cvs file comprised of several hundred rows and 3 > columns of data. The data within maps the output of a triaxial > accelerometer, a sensor which measures an object's acceleration along the > x,y and z axes. The data for each respective column sequentially > oscillates, and ranges numerically from 100 to 500. > > I want create a function that parses the data and detects patterns across > the three columns. > > For instance, I would like to detect instances when the values for the x,y > and z columns equal 150, 200, 300 respectively. Additionally, when a match > is detected, I would like to know how many times the pattern appears. > > I have been successful using str_detect to provide a Boolean, however it > seems to only work on a single vector, i.e, "400" , not a range of values > i.e "400 - 450". See below: > > > # this works > > vals <- str_detect (string = data_log$x_reading, pattern = "400") > > # this also works, but doesn't detect the particular range, rather the > existence of the numbers > > vals <- str_detect (string = data_log$x_reading, pattern = "[400-450]") > > Also, it appears that I can only apply it to a single column, not to all > three columns. However I may be mistaken. > > Any advice on my current approach or alternativea I should consider is > greatly appreciated. > > Many thanks, > > Vincent > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Hi, On Thu, Jun 26, 2014 at 12:17 PM, VINCENT DEAN BOYCE <vincentdeanboyce at gmail.com> wrote:> Hello, > > Using R, I've loaded a .cvs file comprised of several hundred rows and 3 > columns of data. The data within maps the output of a triaxial > accelerometer, a sensor which measures an object's acceleration along the > x,y and z axes. The data for each respective column sequentially > oscillates, and ranges numerically from 100 to 500.If your data are numeric, why are you using stringr? It would be easier to provide you with an answer if we knew what your data looked like. dput(head(yourdata, 20)) and paste that into your non-HTML email.> I want create a function that parses the data and detects patterns across > the three columns. > > For instance, I would like to detect instances when the values for the x,y > and z columns equal 150, 200, 300 respectively. Additionally, when a match > is detected, I would like to know how many times the pattern appears.That's easy enough: fakedata <- data.frame(matrix(c( 100, 100, 200, 150, 200, 300, 100, 350, 100, 400, 200, 300, 200, 500, 200, 150, 200, 300, 150, 200, 300), ncol=3, byrow=TRUE)) v.to.match <- c(150, 200, 300) v.matches <- apply(fakedata, 1, function(x)all(x == v.to.match)) # which rows match which(v.matches) # how many rows match sum(v.matches)> I have been successful using str_detect to provide a Boolean, however it > seems to only work on a single vector, i.e, "400" , not a range of values > i.e "400 - 450". See below:This is where I get confused, and where we need sample data. Are your data numeric, as you state above, or some other format? If your data are character, and like "400 - 450", you can still match them with the code I suggested above.> # this works >> vals <- str_detect (string = data_log$x_reading, pattern = "400") > > # this also works, but doesn't detect the particular range, rather the > existence of the numbers >> vals <- str_detect (string = data_log$x_reading, pattern = "[400-450]")Are you trying to match any numeric value in the range 400-450? Again, actual data.> Also, it appears that I can only apply it to a single column, not to all > three columns. However I may be mistaken.You answer your own question unwittingly - apply(). Sarah -- Sarah Goslee http://www.functionaldiversity.org
Hi, May be you can use ?cut or ?findInterval for the range dat1 <- read.table(text="100, 100, 200 250, 300, 350 100, 350, 100 400, 250, 300 200, 450, 200 150, 501, 300 150, 250, 300",sep=",",header=F) sapply(dat1, findInterval, c(400,500))==1 #??????? V1??? V2??? V3 #[1,] FALSE FALSE FALSE #[2,] FALSE FALSE FALSE #[3,] FALSE FALSE FALSE #[4,]? TRUE FALSE FALSE #[5,] FALSE? TRUE FALSE #[6,] FALSE FALSE FALSE #[7,] FALSE FALSE FALSE A.K. On Thursday, June 26, 2014 4:11 PM, VINCENT DEAN BOYCE <vincentdeanboyce at gmail.com> wrote: Hello, Using R,? I've loaded a .cvs file comprised of several hundred rows and 3 columns of data. The data within maps the output of a triaxial accelerometer, a sensor which measures an object's acceleration along the x,y and z axes. The data for each respective column sequentially oscillates, and ranges numerically from 100 to 500. I want create a function that parses the data and detects patterns across the three columns. For instance, I would like to detect instances when the values for the x,y and z columns equal 150, 200, 300 respectively. Additionally, when a match is detected, I would like to know how many times the pattern appears. I have been successful using str_detect to provide a Boolean, however it seems to only work on a single vector, i.e, "400" , not a range of values i.e "400 - 450". See below: # this works> vals <- str_detect (string = data_log$x_reading, pattern = "400")# this also works, but doesn't detect the particular range, rather the existence of the numbers> vals <- str_detect (string = data_log$x_reading, pattern = "[400-450]")Also, it appears that I can only apply it to a single column, not to all three columns. However I may be mistaken. Any advice on my current approach or alternativea I should consider is greatly appreciated. Many thanks, Vincent ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.