简体   繁体   中英

R get subset of data based on another data frame's x

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM