Select specific rows based on previous row value (in the same column) - r
I've been trying to figure a way to script this through R, but just can't get it. I have a dataset like this:
Trial Type Correct Latency
1 55 0 0
3 30 1 766
4 10 1 344
6 40 1 716
7 10 1 326
9 30 1 550
10 10 1 350
11 64 0 0
13 30 1 683
14 10 1 270
16 30 1 666
17 10 1 297
19 40 1 616
20 10 1 315
21 64 0 0
23 40 1 850
24 10 1 322
26 30 1 566
27 20 0 766
28 40 1 500
29 20 1 230
which goes for much longer(around 1000 rows).
From this one dataset, I would like to create 4 separate data.frames/tables I can export tables with as well as do my own calculations
I would like to have a data.frame (4 in total), one for each of these bullet points:
type 10 rows which are preceded by a type 30 row
type 10 rows which are preceded by a type 40 row
type 20 rows which are preceded by a type 30 row
type 20 rows which are preceded by a type 40 row
I would like for all the columns in the relevant rows to be placed into these new tables, but only including the column info of row types 10 or 20.
For example, the first table (type 10 preceded by type 30) would like this based on the sample data:
Trial Type Correct Latency
4 10 1 344
10 10 1 350
14 10 1 270
17 10 1 297
Second table (type 10 preceded by type 40):
Trial Type Correct Latency
7 10 1 326
20 10 1 315
24 10 1 322
Third table (type 20 preceded by type 30):
Trial Type Correct Latency
27 20 0 766
Fourth table (table 20 preceded by type 40):
Trial Type Correct Latency
29 20 1 230
I can subset just fine to get one table only of type 10 rows and another for type 20 rows, but I can't figure out how to create different tables for type 10 and 20 rows based on the previous type value. Also, an issue is that "Trials" is not in order (skips numbers).
Any help would be greatly appreciated. Thank you.
Also, is there a way to include the previous row as well, so the output for the fourth table would look something like this:
Fourth table (table 20 preceded by type 40):
Trial Type Correct Latency
28 40 1 500
29 20 1 230
For the fourth example, you could use which() in combination with lag() from dplyr, to attain the indices that meet your criteria. Then you can use these to subset the data.frame.
# Get indices of rows that meet condition
ind2 <- which(df$Type==20 & dplyr::lag(df$Type)==40)
# Get indices of rows before the ones that meet condition
ind1 <- which(df$Type==20 & dplyr::lag(df$Type)==40)-1
# Subset data
> df[c(ind1,ind2)]
Trial Type Correct Latency
1: 28 40 1 500
2: 29 20 1 230
Here is an example code if you always want to delete the first trials of your data.
var1 <- c(1,2,1,2,1,2,1,2,1,2)
var2 <- c(1,1,1,2,2,2,2,3,3,3)
dat <- data.frame(var1, var2)
var1 var2
1 1 1
2 2 1
3 1 1
4 2 2
5 1 2
6 2 2
7 1 2
8 2 3
9 1 3
10 2 3
#delete only this line directly
filter(dat,lag(var2)==var2)
var1 var2
1 1 1
2 2 1
3 1 1
6 2 2
7 1 2
10 2 3
#delete the first 2 trials
#make a list of all rows where var2[n-1]!=var2[n] --> using lag from dplyr
drops <- c(1,2,which(lag(dat$var2)!=dat$var2), which(lag(dat$var2)!=dat$var2)+1)
if (!identical(drops,numeric(0))) { dat <- dat[-drops,] }
var1 var2
3 1 1
6 2 2
7 1 2
10 2 3
Related
how to remove error and post error trials
For my research, I have to remove certain trials to limit contamination in the data. Here are the rules: Remove the first trial. For RT calculation, remove error trials and trials following an error. Say, we have 20 trials and 3 errors, we have to remove 6 trials from the final data, i.e., mean RT will be calculated from 14 trials. If the errors are in a row, say 3 errors in a row, RT will be calculated based on 17 trials. For error rate calculation, remove trials following an error. Say, we have 20 trials and 3 errors, we have to remove 3 trials following errors from the final data, i.e., error rate will be calculated by 3/17. If the errors are in a row, say 3 errors in a row, only the first error retains and the next two errors are excluded, so the error rate will be calculated by 1/18. trial_no. correct 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 14 0 15 1 16 1 17 1 18 1 19 0 20 1 21 1 22 1 23 0 24 0 25 1 26 1 27 1 28 1 29 1 30 1 31 1 32 0 33 0 34 1 35 1 36 1 37 1 38 1 39 0 40 1
Creating an summary dataset with multiple objects and multiple observations per object
I have a dataset with the reports from a local shop, where each line has a client's ID, date of purchase and total value per purchase. I want to create a new plot where for each client ID I have all the purchases in the last month or even just sample purchases in a range of dates I choose. The main problem is that certain customers might buy once a month, while others can come daily - so the number of observations per period of time can vary. I have tried subsetting my dataset to a specific range of time, but either I choose a specific date - and then I only get a small % of all customers, or I choose a range and get multiple observations for certain customers. (In this case - I wouldn't mind getting the earliest observation) An important note: I know how to create a for loop to solve this problem, but since the dataset is over 4 million observations it isn't practical since it would take an extremely long time to run. A basic example of what the dataset looks like: ID Date Sum 1 1 1 234 2 1 2 45 3 1 3 1 4 2 4 223 5 3 5 546 6 4 6 12 7 2 1 20 8 4 3 30 9 6 2 3 10 3 5 45 11 7 6 456 12 3 7 65 13 8 8 234 14 1 9 45 15 3 2 1 16 4 3 223 17 6 6 546 18 3 4 12 19 8 7 20 20 9 5 30 21 11 6 3 22 12 6 45 23 14 9 456 24 15 10 65 .... And the new data set would look something like this: ID 1Date 1Sum 2Date 2Sum 3Date 3Sum 1 1 234 2 45 3 1 2 1 20 4 223 NA NA 3 2 1 5 546 5 45 ... Thanks for your help!
I think you can do this with a bit if help from dplyr and tidyr library(dplyr) library(tidyr) dd %>% group_by(ID) %>% mutate(seq=1:n()) %>% pivot_wider("ID", names_from="seq", values_from = c("Date","Sum")) Where dd is your sample data frame above.
Count next n rows that meets a condition in R
Let's say I have a df that looks like this ID X_Value 1 40 2 13 3 75 4 83 5 64 6 43 7 74 8 45 9 54 10 84 So what I would like to do, is to do a rolling function that if in the actual and last 4 rows, there are 2 or more values that are higher than X (let's say 70 for this example) then return 1, else 0. So the output would be something like the following: ID X_Value Next_4_2 1 40 0 2 13 0 3 75 0 4 83 1 5 64 1 6 43 1 7 24 1 8 45 0 9 74 0 10 84 1 I think this would be possible with a rolling function, but I have tried and not sure how to do it. Thank you in advance
Given your expected output, I suppose you meant "in the actual and previous 3 rows". Then using some rolling function indeed does the job: library(zoo) thr1 <- 70 thr2 <- 2 last <- 3 + 1 df$Next_4_2 <- 1 * (rollsum(df$X_Value > thr1, last, align = "right", fill = 0) >= thr2) df # ID X_Value Next_4_2 # 1 1 40 0 # 2 2 13 0 # 3 3 75 0 # 4 4 83 1 # 5 5 64 1 # 6 6 43 1 # 7 7 74 1 # 8 8 45 0 # 9 9 54 0 # 10 10 84 1
The indexing using max(1,i-3) is perhaps the only part of the code worth remembering. I might help in subsequent construction when a for-loop was really needed. dat$X_Next_4_2 <- integer( length(dat$X_Value) ) dat$ X_Next_4_2[1]=0 for (i in 2:length(dat$X_Value) ){ dat$ X_Next_4_2[i]= ( sum(dat$X_Value[i: (max(0, i-4) )] >=70) >=2 )} (Not very pretty and clearly inferior to the rollsum answer already posted.)
Split data when time intervals exceed a defined value
I have a data frame of GPS locations with a column of seconds. How can I split create a new column based on time-gaps? i.e. for this data.frame: df <- data.frame(secs=c(1,2,3,4,5,6,7,10,11,12,13,14,20,21,22,23,24,28,29,31)) I would like to cut the data frame when there is a time gap between locations of 3 or more seconds seconds and create a new column entitled 'bouts' which gives a running tally of the number of sections to give a data frame looking like this: id secs bouts 1 1 1 2 2 1 3 3 1 4 4 1 5 5 1 6 6 1 7 7 1 8 10 2 9 11 2 10 12 2 11 13 2 12 14 2 13 20 3 14 21 3 15 22 3 16 23 3 17 24 3 18 28 4 19 29 4 20 31 4
Use cumsum and diff: df$bouts <- cumsum(c(1, diff(df$secs) >= 3)) Remember that logical values get coerced to numeric values 0/1 automatically and that diff output is always one element shorter than its input.
summing a range of columns in data frame
I am having trouble summing select columns within a data frame, a basic problem that I've seen numerous similar, but not identical questions/answers for on StackOverflow. With this perhaps overly complex data frame: site<-c(223,257,223,223,257,298,223,298,298,211) moisture<-c(7,7,7,7,7,8,7,8,8,5) shade<-c(83,18,83,83,18,76,83,76,76,51) sampleID<-c(158,163,222,107,106,166,188,186,262,114) bluestm<-c(3,4,6,3,0,0,1,1,1,0) foxtail<-c(0,2,0,4,0,1,1,0,3,0) crabgr<-c(0,0,2,0,33,0,2,1,2,0) johnson<-c(0,0,0,7,0,8,1,0,1,0) sedge1<-c(2,0,3,0,0,9,1,0,4,0) sedge2<-c(0,0,1,0,1,0,0,1,1,1) redoak<-c(9,1,0,5,0,4,0,0,5,0) blkoak<-c(0,22,0,23,0,23,22,17,0,0) my.data<-data.frame(site,moisture,shade,sampleID,bluestm,foxtail,crabgr,johnson,sedge1,sedge2,redoak,blkoak) I want to sum the counts of each plant species (bluestem, foxtail, etc. - columns 4-12 in this example) within each site, by summing rows that have the same site number. I also want to keep information about moisture and shade (these are consistant withing site, but may also be the same between sites), and want a new column that is the count of number of rows summed. the result would look like this site,moisture,shade,NumSamples,bluestm,foxtail,crabgr,johnson,sedge1,sedge2,redoak,blkoak 211,5,51,1,0,0,0,0,0,1,0,0 223,7,83,4,13,5,4,8,6,1,14,45 257,7,18,2,4,2,33,0,0,1,1,22 298,8,76,3,2,4,3,9,13,2,9,40 The problem I am having is that, my real data sets (and I have several of them) have from 50 to 300 plant species, and I want refer a range of columns (in this case, [5:12] ) instead of my.data$foxtail, my.data$sedge1, etc., which is going to be very difficult with 300 species. I know I can start off by deleting the column I don't need (SampleID) my.data$SampleID <- NULL but then how do I get the sums? I've messed with the aggregate command and with ddply, and have seen lots of examples which call particular column names, but just haven't gotten anything to work. I recognize this is a variant of a commonly asked and simple type of question, but I've spent hours without resolving it on my own. So, apologies for my stupidity!
This works ok: x <- aggregate(my.data[,5:12], by=list(site=my.data$site, moisture=my.data$moisture, shade=my.data$shade), FUN=sum, na.rm=T) library(dplyr) my.data %>% group_by(site) %>% tally %>% left_join(x) site n moisture shade bluestm foxtail crabgr johnson sedge1 sedge2 redoak blkoak 1 211 1 5 51 0 0 0 0 0 1 0 0 2 223 4 7 83 13 5 4 8 6 1 14 45 3 257 2 7 18 4 2 33 0 0 1 1 22 4 298 3 8 76 2 4 3 9 13 2 9 40 Or to do it all in dplyr my.data %>% group_by(site) %>% tally %>% left_join(my.data) %>% group_by(site,moisture,shade,n) %>% summarise_each(funs(sum=sum)) %>% select(-sampleID) site moisture shade n bluestm foxtail crabgr johnson sedge1 sedge2 redoak blkoak 1 211 5 51 1 0 0 0 0 0 1 0 0 2 223 7 83 4 13 5 4 8 6 1 14 45 3 257 7 18 2 4 2 33 0 0 1 1 22 4 298 8 76 3 2 4 3 9 13 2 9 40
Try following using base R: outdf<-data.frame(site=numeric(),moisture=numeric(),shade=numeric(),bluestm=numeric(),foxtail=numeric(),crabgr=numeric(),johnson=numeric(),sedge1=numeric(),sedge2=numeric(),redoak=numeric(),blkoak=numeric()) my.data$basic = with(my.data, paste(site, moisture, shade)) for(b in unique(my.data$basic)) { outdf[nrow(outdf)+1,1:3] = unlist(strsplit(b,' ')) for(i in 4:11) outdf[nrow(outdf),i]= sum(my.data[my.data$basic==b,i]) } outdf site moisture shade bluestm foxtail crabgr johnson sedge1 sedge2 redoak blkoak 1 223 7 83 13 5 4 8 6 1 14 45 2 257 7 18 4 2 33 0 0 1 1 22 3 298 8 76 2 4 3 9 13 2 9 40 4 211 5 51 0 0 0 0 0 1 0 0