Replace all columns with a specific prefix with a new column which will have values the numeric part of these columns

huangapple go评论52阅读模式
英文:

Replace all columns with a specific prefix with a new column which will have values the numeric part of these columns

问题

Participant Level Place or articulation    VOT response
1    Valentin    A1              Bilabial  0.000        0
2    Valentin    A1              Bilabial  0.005        0
3    Valentin    A1              Bilabial  0.010        0
4    Valentin    A1              Bilabial  0.015        0
5    Valentin    A1              Bilabial  0.020        0
6    Valentin    A1              Bilabial -0.010        1
...
英文:

I have the data frame below in which I want to replace all the columns with "VOT" with one column named "VOT" which will include values (-60, -50, -40, -30, -20, -10, 0, 5, 10, 15, 20). The values that now exist in the columns after the VOT. Also I want to create a new column named response which will have the relative values (0,1). It will be like (the vales of VOT are just divided by 1000):

data<-structure(list(Participant = c("Valentin", "Valentin", "Valentin", 
"Valentin", "Valentin", "Valentin", "Valentin", "Valentin", "Valentin", 
"Valentin", "Valentin", "Valentin", "Valentin", "Valentin", "Valentin", 
"Andres", "Andres", "Andres", "Andres", "Andres", "Andres", "Andres", 
"Andres", "Andres", "Andres", "Andres", "Andres", "Andres", "Andres", 
"Andres", "Nora", "Nora", "Nora", "Nora", "Nora", "Nora", "Nora", 
"Nora", "Nora", "Nora", "Nora", "Nora", "Nora", "Nora", "Nora", 
"Ivan", "Ivan", "Ivan", "Ivan", "Ivan"), Number = c(1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
4, 4, 4, 4, 4), Level = c("A1", "A1", "A1", "A1", "A1", "A1", 
"A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", 
"A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", 
"A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", 
"A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1"
), `Time abroad` = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), `Spanish use` = c(42, 
42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 27, 27, 
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 35, 35, 35, 
35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 44, 44, 44, 44, 
44), Block = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 
2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 
3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5), `Place or articulation` = c("Bilabial", 
"Bilabial", "Bilabial", "Bilabial", "Bilabial", "Dental", "Dental", 
"Dental", "Dental", "Dental", "Velar", "Velar", "Velar", "Velar", 
"Velar", "Bilabial", "Bilabial", "Bilabial", "Bilabial", "Bilabial", 
"Dental", "Dental", "Dental", "Dental", "Dental", "Velar", "Velar", 
"Velar", "Velar", "Velar", "Bilabial", "Bilabial", "Bilabial", 
"Bilabial", "Bilabial", "Dental", "Dental", "Dental", "Dental", 
"Dental", "Velar", "Velar", "Velar", "Velar", "Velar", "Bilabial", 
"Bilabial", "Bilabial", "Bilabial", "Bilabial"), `VOT 0` = c(0, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0), `VOT 5` = c(0, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 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1, 1, 1, 1, 
    1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1), `VOT -40` = c(1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0), 
    `VOT -50` = c(1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0), `VOT -60` = c(1, 
    1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), row.names = c(NA, -50L
), class = c("tbl_df", "tbl", "data.frame"))

Participant Level Place or articulation    VOT response
1    Valentin    A1              Bilabial  0.000        0
2    Valentin    A1              Bilabial  0.005        0
3    Valentin    A1              Bilabial  0.010        0
4    Valentin    A1              Bilabial  0.015        0
5    Valentin    A1              Bilabial  0.020        0
6    Valentin    A1              Bilabial -0.010        1

答案1

得分: 1

Here is the translated code:

# 如果你的数据框名字是d,你可以尝试这样做:
library(dplyr)
library(tidyr)

d %>%
  pivot_longer(starts_with('VOT'),
               names_to = 'VOT',
               values_to = 'response'
               ) %>%
  mutate(VOT = 1e-3 * (VOT %>%
                gsub(pattern = '^VOT\\W*', replacement = '') %>%
                as.numeric()
                )
         ) %>%
  select(Participant, Level, 'Place or articulation', VOT:response)
# 一个 tibble: 550 x 5
   Participant Level 'Place or articulation'   VOT response
   <chr>       <chr> <chr>                   <dbl>    <dbl>
 1 Valentin    A1    Bilabial                0            0
 2 Valentin    A1    Bilabial                0.005        0
 3 Valentin    A1    Bilabial                0.01         0
 4 Valentin    A1    Bilabial                0.015        0
 5 Valentin    A1    Bilabial                0.02         0
 6 Valentin    A1    Bilabial                0.01         1
 7 Valentin    A1    Bilabial                0.02         1
 8 Valentin    A1    Bilabial                0.03         1

Please note that I've translated the code only, as per your request.

英文:

d being your dataframe, you could try this:

library(dplyr)
library(tidyr)

d |&gt;
  pivot_longer(starts_with(&#39;VOT&#39;),
               names_to = &#39;VOT&#39;,
               values_to = &#39;response&#39;
               ) |&gt;
  mutate(VOT = 1e-3 * (VOT |&gt;
                gsub(pattern = &#39;^VOT\\W*&#39;, replacement = &#39;&#39;) |&gt;
                as.numeric()
                )
         ) |&gt;
  select(Participant, Level, `Place or articulation`, VOT:response)
# A tibble: 550 x 5
   Participant Level `Place or articulation`   VOT response
   &lt;chr&gt;       &lt;chr&gt; &lt;chr&gt;                   &lt;dbl&gt;    &lt;dbl&gt;
 1 Valentin    A1    Bilabial                0            0
 2 Valentin    A1    Bilabial                0.005        0
 3 Valentin    A1    Bilabial                0.01         0
 4 Valentin    A1    Bilabial                0.015        0
 5 Valentin    A1    Bilabial                0.02         0
 6 Valentin    A1    Bilabial                0.01         1
 7 Valentin    A1    Bilabial                0.02         1
 8 Valentin    A1    Bilabial                0.03         1

huangapple
  • 本文由 发表于 2023年5月15日 04:50:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定