Reputation:
There is a vector and a data frame. The vector is a raw data that recorded the respondent's response from the survey (they had to choose one choicest among three) so it indicates from 1-3. The data frame is to organize the result from the first d.f. What I need is to drag the result from the vector for each Trial and indicate in the d.f with a new column 'chosen'.
I am going to create a new column 'chosen' in the d.f. For each trial, the chosen alternative from a respondent would have 1 in the 'chosen' column and 0 otherwise. I first need to find the chosen value that matches d.f$trial with the digit that comes right after the column name "conjoint_full_info" in the vector. After finding the value, I need to indicate with "1" in "chosen" column along with the corresponding alternative row. (By looking at the vector, in trial 1, the respondent chose alternative 1. So, indicate "1" in the chosen column along with column alternative=1 row. And the remaining 2 rows with "0") I am looking for a way to apply to every set below but I am not sure how to code this in an efficient way. Maybe using for loops? Sorry for the unclear explanations and thanks in advance!
These are how the two datasets
Vector
conjoint_full_info.1. conjoint_full_info.2. conjoint_full_info.3. conjoint_full_info.4.
1 2 2 2
d.f
Ind Trial alternative price privacy battery stars
1 R_2Xb32PAT3WjGBnc 1 1 2 3 1 1
2 R_2Xb32PAT3WjGBnc 1 2 3 1 2 2
3 R_2Xb32PAT3WjGBnc 1 3 1 2 3 3
4 R_2Xb32PAT3WjGBnc 2 1 1 2 2 1
5 R_2Xb32PAT3WjGBnc 2 2 2 3 3 2
6 R_2Xb32PAT3WjGBnc 2 3 3 1 1 3
7 R_2Xb32PAT3WjGBnc 3 1 3 1 3 1
8 R_2Xb32PAT3WjGBnc 3 2 1 2 1 2
9 R_2Xb32PAT3WjGBnc 3 3 2 3 2 3
10 R_2Xb32PAT3WjGBnc 4 1 1 1 1 2
11 R_2Xb32PAT3WjGBnc 4 2 2 2 2 3
12 R_2Xb32PAT3WjGBnc 4 3 3 3 3 1
and this is what I want
d.f
Ind Trial alternative price privacy battery stars chosen
1 R_2Xb32PAT3WjGBnc 1 1 2 3 1 1 1
2 R_2Xb32PAT3WjGBnc 1 2 3 1 2 2 0
3 R_2Xb32PAT3WjGBnc 1 3 1 2 3 3 0
4 R_2Xb32PAT3WjGBnc 2 1 1 2 2 1 0
5 R_2Xb32PAT3WjGBnc 2 2 2 3 3 2 1
6 R_2Xb32PAT3WjGBnc 2 3 3 1 1 3 0
7 R_2Xb32PAT3WjGBnc 3 1 3 1 3 1 0
8 R_2Xb32PAT3WjGBnc 3 2 1 2 1 2 1
9 R_2Xb32PAT3WjGBnc 3 3 2 3 2 3 0
10 R_2Xb32PAT3WjGBnc 4 1 1 1 1 2 1
11 R_2Xb32PAT3WjGBnc 4 2 2 2 2 3 0
12 R_2Xb32PAT3WjGBnc 4 3 3 3 3 1 0
Upvotes: 1
Views: 346
Reputation: 47008
your data:
Vector <- c(conjoint_full_info.1. = 1, conjoint_full_info.2. = 2, conjoint_full_info.3. = 2,
conjoint_full_info.4. = 2)
d.f <- structure(list(Ind = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = "R_2Xb32PAT3WjGBnc", class = "factor"),
Trial = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L),
alternative = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L), price = c(2L, 3L, 1L, 1L, 2L, 3L, 3L, 1L, 2L, 1L, 2L,
3L), privacy = c(3L, 1L, 2L, 2L, 3L, 1L, 1L, 2L, 3L, 1L,
2L, 3L), battery = c(1L, 2L, 3L, 2L, 3L, 1L, 3L, 1L, 2L,
1L, 2L, 3L), stars = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L,
2L, 3L, 1L)), class = "data.frame", row.names = c("1", "2",
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12"))
We create a mapping vector:
mapVector = Vector
names(mapVector) = sapply(strsplit(names(Vector),"[.]"),"[[",2)
# now mapVector has names that match trial
mapVector
1 2 3 4
1 2 2 2
If we do mapVector[as.character(d.f$Trial)]
, we get the chosen alternative for each row:
head(cbind(d.f,mapVector[as.character(d.f$Trial)]))
Ind Trial alternative price privacy battery stars
1 R_2Xb32PAT3WjGBnc 1 1 2 3 1 1
2 R_2Xb32PAT3WjGBnc 1 2 3 1 2 2
3 R_2Xb32PAT3WjGBnc 1 3 1 2 3 3
4 R_2Xb32PAT3WjGBnc 2 1 1 2 2 1
5 R_2Xb32PAT3WjGBnc 2 2 2 3 3 2
6 R_2Xb32PAT3WjGBnc 2 3 3 1 1 3
mapVector[as.character(d.f$Trial)]
1 1
2 1
3 1
4 2
5 2
6 2
So it's a matter of creating another column that checks whether this, agrees with the alternative column:
library(dplyr)
d.f %>%
mutate(chosen=as.numeric(alternative == mapVector[as.character(Trial)]))
Ind Trial alternative price privacy battery stars chosen
1 R_2Xb32PAT3WjGBnc 1 1 2 3 1 1 1
2 R_2Xb32PAT3WjGBnc 1 2 3 1 2 2 0
3 R_2Xb32PAT3WjGBnc 1 3 1 2 3 3 0
4 R_2Xb32PAT3WjGBnc 2 1 1 2 2 1 0
5 R_2Xb32PAT3WjGBnc 2 2 2 3 3 2 1
6 R_2Xb32PAT3WjGBnc 2 3 3 1 1 3 0
7 R_2Xb32PAT3WjGBnc 3 1 3 1 3 1 0
8 R_2Xb32PAT3WjGBnc 3 2 1 2 1 2 1
9 R_2Xb32PAT3WjGBnc 3 3 2 3 2 3 0
10 R_2Xb32PAT3WjGBnc 4 1 1 1 1 2 0
11 R_2Xb32PAT3WjGBnc 4 2 2 2 2 3 1
12 R_2Xb32PAT3WjGBnc 4 3 3 3 3 1 0
Upvotes: 1