简体   繁体   English

R dplyr-根据特定值在另一列中的位置从一列中选择值

[英]R dplyr - select values from one column based on position of a specific value in another column

I am working with gait-cycle data. 我正在处理步态周期数据。 I have 8 events marked for each id and gait trial. 我为每个ID和步态试验标记了8个事件。 The values "LFCH" and "RFCH" occurs twice in each trial, as these represent the beginning and the end of the gait cycles from left and right leg. 在每个试验中,“ LFCH”和“ RFCH”值出现两次,因为它们代表从左腿和右腿开始的步态周期的开始和结束。

Sample Data Frame: 样本数据框:

df <- data.frame(ID = rep(1:5, each = 16),
                 Gait_nr = rep(1:2, each = 8, times=5),
                 Frame = rep(c(1,5,7,9,10,15,22,25), times = 10),
                 Marks = rep(c("LFCH", "LHL", "RFCH", "LTO", "RHL", "LFCH", "RTO", "RFCH"), times =10) 

head(df,8)
  ID Gait_nr Frame Marks
1  1       1     1  LFCH
2  1       1     5   LHL
3  1       1     7  RFCH
4  1       1     9   LTO
5  1       1    10   RHL
6  1       1    15  LFCH
7  1       1    22   RTO
8  1       1    25  RFCH

I wold like to create something like 我很想创造像

Total_gait_left = Frame[The last time Marks == "LFCH"] - Frame[The first time Marks == "LFCH"]

My current code solves the problem, but depends on the position of the Frame values rather than actual values in Marks. 我当前的代码解决了这个问题,但是取决于Frame值的位置,而不是Mark中的实际值。 Any individual not following the normal gait pattern will have wrong values produced by the code. 任何不遵循正常步态模式的人,其代码都会产生错误的值。

library(tidyverse)
l <- df %>% group_by(ID, Gait_nr) %>% filter(grepl("L.+", Marks)) %>%
  summarize(Total_gait = Frame[4] - Frame[1],
            Side = "left")

r <- df %>% group_by(ID, Gait_nr) %>% filter(grepl("R.+", Marks)) %>%
  summarize(Total_gait = Frame[4] - Frame[1],
            Side = "right")

val <- union(l,r, by=c("ID", "Gait_nr", "Side")) %>% arrange(ID, Gait_nr, Side)

Can you help me make my code more stable by helping me change eg Frame[4] to something like Frame[Marks=="LFCH" the last time ]? 您可以通过帮助我将例如Frame [4]更改为类似Frame [Marks ==“ LFCH”的代码来帮助我使代码更稳定吗?

If both LFCH and RFCH happen exactly twice, you can filter and then use diff in summarize : 如果两个LFCHRFCH发生两次完全相同,可以过滤,然后用diffsummarize

df %>% 
    group_by(ID, Gait_nr) %>% 
    summarise(
        left = diff(Frame[Marks == 'LFCH']), 
        right = diff(Frame[Marks == 'RFCH'])
    )

# A tibble: 10 x 4
# Groups:   ID [?]
#      ID Gait_nr  left right
#   <int>   <int> <dbl> <dbl>
# 1     1       1    14    18
# 2     1       2    14    18
# 3     2       1    14    18
# 4     2       2    14    18
# 5     3       1    14    18
# 6     3       2    14    18
# 7     4       1    14    18
# 8     4       2    14    18
# 9     5       1    14    18
#10     5       2    14    18

We can use first and last from the dplyr package. 我们可以使用dplyr包中的firstlast

library(dplyr)

df2 <- df %>%
  filter(Marks %in% "LFCH") %>%
  group_by(ID, Gait_nr) %>%
  summarise(Total_gait = last(Frame) - first(Frame)) %>%
  ungroup()
df2
# # A tibble: 10 x 3
#       ID Gait_nr Total_gait
#    <int>   <int>      <dbl>
#  1     1       1         14
#  2     1       2         14
#  3     2       1         14
#  4     2       2         14
#  5     3       1         14
#  6     3       2         14
#  7     4       1         14
#  8     4       2         14
#  9     5       1         14
# 10     5       2         14

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 r、dplyr:如何使用 gsub 根据另一列中的值转换一列中的值 - r, dplyr: how to transform values in one column based on value in another column using gsub R dplyr 根据以前的值和另一列的值添加值 - R dplyr add values based on previous value and value from another column 使用dplyr基于列值对R中的值求和 - Summing values in R based on column value with dplyr 根据 R 中另一列中的值范围按列值选择行 - Select rows by column value based on range of values in another column in R 用 R dplyr 中另一列的值替换一列的值 - Replace the values of one column with values of another column in R dplyr R从数据框中选择所有行,在该数据框中,一个值重复一列,但在另一列中具有特定值 - R select all rows from a dataframe where a value is duplicated one column but has a specific value in another column 在R中根据值以及另一列的频率使用dplyr创建列 - Create column with dplyr based on value and also frequency of another column, in R R dplyr根据乐趣指数汇总一个列值(另一列) - R dplyr summarise one column value based on index of fun(another column) 如何使用 dplyr 根据另一列中的值选择列? - How do I select column based on value in another column with dplyr? R:根据另一列的值对一列的值进行分箱 - R: Binning Values from One Column Based Upon the Value of Another Column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM