-1

I've ran a small pilot study and I'm currently analysing my reaction time data.

In my dataframe, I'm looking at the reaction time (RT) across my columns some rows have just a singular value like this [0.8414999999999964]

Others have multiple like this [1.0113000000119143,1.3689999999999998] or [0.7324999999999875,1.1204999999999927,1.3931000000238214]

Is there a way I can keep the final value in rows with mutiple RT, so the row with this RT [0.7324999999999875,1.1204999999999927,1.3931000000238214]

would then become

[1.3931000000238214]

Thanks in advance!

Example data-

I have a data frame of 1 column of RT values

looks sort of like this

RT

[0.8414999999999964]

[2.1925999999642443]

[1.810899999976158]

[2.2231999999880827]

[1.0113000000119143,1.3689999999999998]

[1.0415000000000134]

[1.3503999999761618]

Image showing structure of the df

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
RH4818
  • 1
  • 1
  • 1
    Welcome to SO, RH4818! Questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically), perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Sep 29 '22 at 12:53
  • Does this answer your question? [How to select only the last row among the subset of rows satisfying a condition in R programming](https://stackoverflow.com/questions/23123190/how-to-select-only-the-last-row-among-the-subset-of-rows-satisfying-a-condition) – jpsmith Sep 29 '22 at 12:56

1 Answers1

0

This should do:

df = data.frame(RT = c('[0.8414999999999964]', '[1.0113000000119143,1.3689999999999998]')) %>% mutate(RT = str_remove_all(RT, '\\[|\\]')) 

df2 = str_split_fixed(string = df$RT, pattern = ',', n = Inf)  %>% as.data.frame() %>%
  mutate(across(everything(), ~replace(.x %>% as.character, .x == '', NA)))
cols = names(df2) %>% rev
df_final = df2 %>% mutate(RT_final = coalesce(!!!syms(cols)))

Output:

                  V1                 V2           RT_final
1 0.8414999999999964               <NA> 0.8414999999999964
2 1.0113000000119143 1.3689999999999998 1.3689999999999998

Totally changed my answer based on new info, let me know if it helps. You could also only select RT_final if needed

Juan C
  • 5,846
  • 2
  • 17
  • 51