通过检查行的值将数据从宽格式转换为长格式

问题描述 投票:0回答:1

我有以下数据

data<-structure(list(id = "R_88j7lG37gLfxk22", t1_choice = "2", t2_choice = "1", 
    t3_choice = "1", t4_choice = "2"), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame"))

我想用这个逻辑将其转换为长格式,如下所示。对于每个参与者,有 12 行,因为每个任务中有 4 个任务(4 't's)和 3 个配置文件(3 'p's)。选择列是一个二进制文件,如果在该任务中选择了该行中的配置文件,则为 1;如果没有选择,则为 0,这是“tN_choice”列中包含的信息。

我的方法不对

tasks<-4
profiles<-3
#column position of first task
cpft<-2

#column position of last task
cplt<-5

# Extracting choices
choices <- as.numeric(unlist(long[, cpft:cplt]))

# Create the new dataframe with id and choice columns
new_df <- data.frame(
  id = rep(data$id, each = tasks*profiles),
  choice = rep(0, times = length(id))
)

# Replacing values based on original choices
for (i in 1:(tasks*profiles)) {
  idx <- (i - 1) * profiles + choices[i]
  new_df$choice[idx] <- 1
}
r dataframe reshape
1个回答
1
投票

也许你可以试试这个

data %>%
    pivot_longer(-id) %>%
    summarise(choice = list(+(seq(3) == value)), .by = c(id, name)) %>%
    unnest(choice)

这给出了

# A tibble: 12 × 3
   id                name      choice
   <chr>             <chr>      <int>
 1 R_88j7lG37gLfxk22 t1_choice      0
 2 R_88j7lG37gLfxk22 t1_choice      1
 3 R_88j7lG37gLfxk22 t1_choice      0
 4 R_88j7lG37gLfxk22 t2_choice      1
 5 R_88j7lG37gLfxk22 t2_choice      0
 6 R_88j7lG37gLfxk22 t2_choice      0
 7 R_88j7lG37gLfxk22 t3_choice      1
 8 R_88j7lG37gLfxk22 t3_choice      0
 9 R_88j7lG37gLfxk22 t3_choice      0
10 R_88j7lG37gLfxk22 t4_choice      0
11 R_88j7lG37gLfxk22 t4_choice      1
12 R_88j7lG37gLfxk22 t4_choice      0
© www.soinside.com 2019 - 2024. All rights reserved.