0

I have two data frames:

 ch1 <- read.csv(file, header=TRUE, sep=";", skip=7)
 ch2 <- read.csv(file2, header=TRUE, sep=";", skip=7)

The second frame has values between 0 and 3 and the X axis corresponds to the one on the first frame (its time).

I want to get only the values of ch1 where the x axis of ch2 is < 1.0 These values i want in seperate lists (matrix preferred)

Example channel 2 data:

Relative time;Data collector12
s;V
2.2079991;0.011083123
2.2079992;0.028211586
2.2079993;-0.0020151134
2.2079994;0.001511335
2.2079995;0.016120907
2.2079996;3.025188917

Example channel 1 data:

Relative time;Data collector1
s;V
2.2079992;-0.0109
2.2079993;-0.0133
2.2079994;-0.01055
2.2079995;-0.0071999999
2.2079996;-0.0043500001

In this case I would like all values except last.

I have no idea where to begin, thanks

J. Daniel
  • 373
  • 3
  • 18
  • You could begin with posting a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so we can understand what kind of data you have. – LyzandeR Jan 08 '18 at 13:25
  • @LyzandeR its pretty simple, both channels have X as time and Y as some value (voltage). im only interested in the voltages of ch1 where the voltages of ch2 are < 1.0 – J. Daniel Jan 08 '18 at 13:27
  • So, you are asking us not only for the solution but to also make the two data sets on our own in order to test our code. – LyzandeR Jan 08 '18 at 13:29
  • Ive added data @LyzandeR – J. Daniel Jan 08 '18 at 13:35
  • 1
    `ch <- merge(ch1, ch2, all = TRUE, by = "s" )` and then `ch[ ch[ , 3 ] < 1, ]`. In order to display the data as shown above, you might have to set`options( digits = 8 )`. and make sure that your data frame has only one header, not two as shown. – vaettchen Jan 08 '18 at 14:05

1 Answers1

0

Here's what you can use:

library(data.table)
# c1 <- fread("filename.csv") #Reading from .csv file
# c2 <- fread("filtename.csv") #Reading from .csv file

c1 <- data.table(relative_time = c(2.2079991, 2.2079992, 2.2079993, 2.2079994, 2.2079995, 2.2079996),
                 voltage = c(0.011083123, 0.028211586, -0.0020151134, 0.001511335, 0.016120907, 3.025188917))

c2 <- data.table(relative_time = c(2.2079992, 2.2079993, 2.2079994, 2.2079995, 2.2079996),
                 voltage = c(-0.0109, -0.0133, -0.01055, -0.0071999999, -0.0043500001))

dat <- merge(c1, c2, by = "relative_time", all = T, suffixes = c("_ch1", "_ch2"))

OUTPUT

> dat
   relative_time  voltage_ch1 voltage_ch2
1:      2.207999  0.011083123          NA
2:      2.207999  0.028211586    -0.01090
3:      2.207999 -0.002015113    -0.01330
4:      2.207999  0.001511335    -0.01055
5:      2.208000  0.016120907    -0.00720
6:      2.208000  3.025188917    -0.00435

> dat[voltage_ch2 <0, ]
   relative_time  voltage_ch1 voltage_ch2
1:      2.207999  0.028211586    -0.01090
2:      2.207999 -0.002015113    -0.01330
3:      2.207999  0.001511335    -0.01055
4:      2.208000  0.016120907    -0.00720
5:      2.208000  3.025188917    -0.00435

I used data.table but it is not necessary - data.frame should give the same result. I thought I'd add it since I find it is really convenient to use on large data sets where more complex manipulation may be required. The by feature is particularly useful.

Gautam
  • 2,597
  • 1
  • 28
  • 51