英文:
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, 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, 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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 |>
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)
# A 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论