我有一个空的数据框

new_df<-structure(list(id = c("R_88j7lG37gLfxk22", "R_88j7lG37gLfxk22", 
"R_88j7lG37gLfxk22", "R_88j7lG37gLfxk22", "R_88j7lG37gLfxk22", 
"R_88j7lG37gLfxk22", "R_88j7lG37gLfxk22", "R_88j7lG37gLfxk22", 
"R_88j7lG37gLfxk22", "R_88j7lG37gLfxk22", "R_88j7lG37gLfxk22", 
"R_88j7lG37gLfxk22", "R_6DK8lERVf8lSQf4", "R_6DK8lERVf8lSQf4", 
"R_6DK8lERVf8lSQf4", "R_6DK8lERVf8lSQf4", "R_6DK8lERVf8lSQf4", 
"R_6DK8lERVf8lSQf4", "R_6DK8lERVf8lSQf4", "R_6DK8lERVf8lSQf4", 
"R_6DK8lERVf8lSQf4", "R_6DK8lERVf8lSQf4", "R_6DK8lERVf8lSQf4", 
"R_6DK8lERVf8lSQf4"), choice = c(0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 
1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1), low_env = c(NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA), mid_env = c(NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA), high_env = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), low_eth = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA), mid_eth = c(NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA), high_eth = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), 
    `low_pri($25)` = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), 
    `mid_pri($75)` = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), 
    `high_pri($125)` = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
    )), row.names = c(NA, 24L), class = "data.frame")

使用上面的数据和代码,我根据long数据集的第一行填充了其中的一半.我如何使用for()循环才能将此方法应用于long数据集中将填充另一半的其余行?

long<-structure(list(id = c("R_88j7lG37gLfxk22", "R_6DK8lERVf8lSQf4"
), t1_choice = c("2", "3"), t2_choice = c("1", "3"), t3_choice = c("1", 
"2"), t4_choice = c("2", "1"), t1_p1_env = c("high_env", "mid_env"
), t1_p1_eth = c("low_eth", "mid_eth"), t1_p1_pri = c("$125", 
"$25"), t1_p2_env = c("mid_env", "high_env"), t1_p2_eth = c("high_eth", 
"low_eth"), t1_p2_pri = c("$25", "$75"), t1_p3_env = c("low_env", 
"low_env"), t1_p3_eth = c("mid_eth", "low_eth"), t1_p3_pri = c("$75", 
"$75"), t2_p1_env = c("high_env", "mid_env"), t2_p1_eth = c("low_eth", 
"high_eth"), t2_p1_pri = c("$75", "$125"), t2_p2_env = c("mid_env", 
"low_env"), t2_p2_eth = c("mid_eth", "low_eth"), t2_p2_pri = c("$125", 
"$75"), t2_p3_env = c("mid_env", "high_env"), t2_p3_eth = c("mid_eth", 
"high_eth"), t2_p3_pri = c("$75", "$75"), t3_p1_env = c("high_env", 
"mid_env"), t3_p1_eth = c("high_eth", "mid_eth"), t3_p1_pri = c("$125", 
"$125"), t3_p2_env = c("mid_env", "high_env"), t3_p2_eth = c("low_eth", 
"low_eth"), t3_p2_pri = c("$25", "$25"), t3_p3_env = c("low_env", 
"low_env"), t3_p3_eth = c("high_eth", "high_eth"), t3_p3_pri = c("$25", 
"$75"), t4_p1_env = c("low_env", "high_env"), t4_p1_eth = c("low_eth", 
"low_eth"), t4_p1_pri = c("$75", "$125"), t4_p2_env = c("high_env", 
"mid_env"), t4_p2_eth = c("mid_eth", "mid_eth"), t4_p2_pri = c("$125", 
"$25"), t4_p3_env = c("low_env", "low_env"), t4_p3_eth = c("high_eth", 
"mid_eth"), t4_p3_pri = c("$25", "$125")), row.names = c(NA, 
-2L), class = c("tbl_df", "tbl", "data.frame"))

#working
# Loop through the first three rows of new_df
for (i in 1:3) {
  # Extracting the required values from long1 for each row
  env <- long[1,][paste0("t1_p", i, "_env")][1]
  eth <- long[1,][paste0("t1_p", i, "_eth")][1]
  pri <- long[1,][paste0("t1_p", i, "_pri")][1]
  
  # Matching values from long[1,] to new_df columns in the corresponding row
  new_df[i, "low_env"] <- as.numeric(env == "low_env")
  new_df[i, "mid_env"] <- as.numeric(env == "mid_env")
  new_df[i, "high_env"] <- as.numeric(env == "high_env")
  new_df[i, "low_eth"] <- as.numeric(eth == "low_eth")
  new_df[i, "mid_eth"] <- as.numeric(eth == "mid_eth")
  new_df[i, "high_eth"] <- as.numeric(eth == "high_eth")
  new_df[i, "low_pri($25)"] <- as.numeric(pri == "$25")
  new_df[i, "mid_pri($75)"] <- as.numeric(pri == "$75")
  new_df[i, "high_pri($125)"] <- as.numeric(pri == "$125")
}

# Loop through the second three rows of new_df
for (i in 1:3) {
  # Extracting the required values from long[1,] for each row
  env <- long[1,][paste0("t2_p", i, "_env")][1]
  eth <- long[1,][paste0("t2_p", i, "_eth")][1]
  pri <- long[1,][paste0("t2_p", i, "_pri")][1]
  
  # Matching values from long[1,] to new_df columns in the corresponding row
  new_df[i + 3, "low_env"] <- as.numeric(env == "low_env")
  new_df[i + 3, "mid_env"] <- as.numeric(env == "mid_env")
  new_df[i + 3, "high_env"] <- as.numeric(env == "high_env")
  new_df[i + 3, "low_eth"] <- as.numeric(eth == "low_eth")
  new_df[i + 3, "mid_eth"] <- as.numeric(eth == "mid_eth")
  new_df[i + 3, "high_eth"] <- as.numeric(eth == "high_eth")
  new_df[i + 3, "low_pri($25)"] <- as.numeric(pri == "$25")
  new_df[i + 3, "mid_pri($75)"] <- as.numeric(pri == "$75")
  new_df[i + 3, "high_pri($125)"] <- as.numeric(pri == "$125")
  # Adjusting the choice column
  new_df[i + 3, "choice"] <- as.numeric(long[1,][paste0("t2_choice")][1] == i)
}
  # Loop through the second three rows of new_df
  for (i in 1:3) {
    # Extracting the required values from long[1,] for each row
    env <- long[1,][paste0("t3_p", i, "_env")][1]
    eth <- long[1,][paste0("t3_p", i, "_eth")][1]
    pri <- long[1,][paste0("t3_p", i, "_pri")][1]
    
    # Matching values from long[1,] to new_df columns in the corresponding row
    new_df[i + 6, "low_env"] <- as.numeric(env == "low_env")
    new_df[i + 6, "mid_env"] <- as.numeric(env == "mid_env")
    new_df[i + 6, "high_env"] <- as.numeric(env == "high_env")
    new_df[i + 6, "low_eth"] <- as.numeric(eth == "low_eth")
    new_df[i + 6, "mid_eth"] <- as.numeric(eth == "mid_eth")
    new_df[i + 6, "high_eth"] <- as.numeric(eth == "high_eth")
    new_df[i + 6, "low_pri($25)"] <- as.numeric(pri == "$25")
    new_df[i + 6, "mid_pri($75)"] <- as.numeric(pri == "$75")
    new_df[i + 6, "high_pri($125)"] <- as.numeric(pri == "$125")
    # Adjusting the choice column
    new_df[i + 6, "choice"] <- as.numeric(long[1,][paste0("t3_choice")][1] == i)
  }
for (i in 1:3) {
  # Extracting the required values from long[1,] for each row
  env <- long[1,][paste0("t4_p", i, "_env")][1]
  eth <- long[1,][paste0("t4_p", i, "_eth")][1]
  pri <- long[1,][paste0("t4_p", i, "_pri")][1]
  
  # Matching values from long[1,] to new_df columns in the corresponding row
  new_df[i + 9, "low_env"] <- as.numeric(env == "low_env")
  new_df[i + 9, "mid_env"] <- as.numeric(env == "mid_env")
  new_df[i + 9, "high_env"] <- as.numeric(env == "high_env")
  new_df[i + 9, "low_eth"] <- as.numeric(eth == "low_eth")
  new_df[i + 9, "mid_eth"] <- as.numeric(eth == "mid_eth")
  new_df[i + 9, "high_eth"] <- as.numeric(eth == "high_eth")
  new_df[i + 9, "low_pri($25)"] <- as.numeric(pri == "$25")
  new_df[i + 9, "mid_pri($75)"] <- as.numeric(pri == "$75")
  new_df[i + 9, "high_pri($125)"] <- as.numeric(pri == "$125")
  

  # Adjusting the choice column
  new_df[i + 9, "choice"] <- as.numeric(long[1,][paste0("t4_choice")][1] == i)
}

预期结果

enter image description here

推荐答案

你的方法目前看起来并不有趣.相反,您可以将更有利的names(即时间作为后缀,而不是奇怪的前缀格式)分配给所需的长格式,然后将数据两次指定为reshape,并使用model.matrix进行一次热编码.不知道您是如何获得CHOICE变量的,因为您已经对它进行了硬编码,但是您会知道的.要获得所有级别,我们需要到type.convert(as.is=FALSE)才能获得factor,然后将contrasts设置为FALSE.

> names(long)[-1] <- 
+   strsplit(names(long)[-1], '_') |>
+   sapply(\(x) paste(paste(c(rev(x[-1])), collapse='_'), x[1], sep='.'))
> res <- reshape(as.data.frame(long), varying=-1, direction='l') |>
+   reshape(direction='l', varying=-(1:3), new.row.names=1:1e9, sep='_') |>
+   type.convert(as.is=FALSE)
> cbind(res[1:3], model.matrix(~ 0 + env + eth + pri, res, 
+                              contrasts.arg = list(eth=contrasts(res$eth, contrasts=FALSE),
+                                                   pri=contrasts(res$pri, contrasts=FALSE))))
                  id time choice envhigh_env envlow_env envmid_env ethhigh_eth ethlow_eth ethmid_eth pri$125 pri$25 pri$75
1  R_88j7lG37gLfxk22   p1      2           1          0          0           0          1          0       1      0      0
2  R_6DK8lERVf8lSQf4   p1      3           0          0          1           0          0          1       0      1      0
3  R_88j7lG37gLfxk22   p1      1           1          0          0           0          1          0       0      0      1
4  R_6DK8lERVf8lSQf4   p1      3           0          0          1           1          0          0       1      0      0
5  R_88j7lG37gLfxk22   p1      1           1          0          0           1          0          0       1      0      0
6  R_6DK8lERVf8lSQf4   p1      2           0          0          1           0          0          1       1      0      0
7  R_88j7lG37gLfxk22   p1      2           0          1          0           0          1          0       0      0      1
8  R_6DK8lERVf8lSQf4   p1      1           1          0          0           0          1          0       1      0      0
9  R_88j7lG37gLfxk22   p2      2           0          0          1           1          0          0       0      1      0
10 R_6DK8lERVf8lSQf4   p2      3           1          0          0           0          1          0       0      0      1
11 R_88j7lG37gLfxk22   p2      1           0          0          1           0          0          1       1      0      0
12 R_6DK8lERVf8lSQf4   p2      3           0          1          0           0          1          0       0      0      1
13 R_88j7lG37gLfxk22   p2      1           0          0          1           0          1          0       0      1      0
14 R_6DK8lERVf8lSQf4   p2      2           1          0          0           0          1          0       0      1      0
15 R_88j7lG37gLfxk22   p2      2           1          0          0           0          0          1       1      0      0
16 R_6DK8lERVf8lSQf4   p2      1           0          0          1           0          0          1       0      1      0
17 R_88j7lG37gLfxk22   p3      2           0          1          0           0          0          1       0      0      1
18 R_6DK8lERVf8lSQf4   p3      3           0          1          0           0          1          0       0      0      1
19 R_88j7lG37gLfxk22   p3      1           0          0          1           0          0          1       0      0      1
20 R_6DK8lERVf8lSQf4   p3      3           1          0          0           1          0          0       0      0      1
21 R_88j7lG37gLfxk22   p3      1           0          1          0           1          0          0       0      1      0
22 R_6DK8lERVf8lSQf4   p3      2           0          1          0           1          0          0       0      0      1
23 R_88j7lG37gLfxk22   p3      2           0          1          0           1          0          0       0      1      0
24 R_6DK8lERVf8lSQf4   p3      1           0          1          0           0          0          1       1      0      0

Data:

> dput(long)
structure(list(id = c("R_88j7lG37gLfxk22", "R_6DK8lERVf8lSQf4"
), t1_choice = c("2", "3"), t2_choice = c("1", "3"), t3_choice = c("1", 
"2"), t4_choice = c("2", "1"), t1_p1_env = c("high_env", "mid_env"
), t1_p1_eth = c("low_eth", "mid_eth"), t1_p1_pri = c("$125", 
"$25"), t1_p2_env = c("mid_env", "high_env"), t1_p2_eth = c("high_eth", 
"low_eth"), t1_p2_pri = c("$25", "$75"), t1_p3_env = c("low_env", 
"low_env"), t1_p3_eth = c("mid_eth", "low_eth"), t1_p3_pri = c("$75", 
"$75"), t2_p1_env = c("high_env", "mid_env"), t2_p1_eth = c("low_eth", 
"high_eth"), t2_p1_pri = c("$75", "$125"), t2_p2_env = c("mid_env", 
"low_env"), t2_p2_eth = c("mid_eth", "low_eth"), t2_p2_pri = c("$125", 
"$75"), t2_p3_env = c("mid_env", "high_env"), t2_p3_eth = c("mid_eth", 
"high_eth"), t2_p3_pri = c("$75", "$75"), t3_p1_env = c("high_env", 
"mid_env"), t3_p1_eth = c("high_eth", "mid_eth"), t3_p1_pri = c("$125", 
"$125"), t3_p2_env = c("mid_env", "high_env"), t3_p2_eth = c("low_eth", 
"low_eth"), t3_p2_pri = c("$25", "$25"), t3_p3_env = c("low_env", 
"low_env"), t3_p3_eth = c("high_eth", "high_eth"), t3_p3_pri = c("$25", 
"$75"), t4_p1_env = c("low_env", "high_env"), t4_p1_eth = c("low_eth", 
"low_eth"), t4_p1_pri = c("$75", "$125"), t4_p2_env = c("high_env", 
"mid_env"), t4_p2_eth = c("mid_eth", "mid_eth"), t4_p2_pri = c("$125", 
"$25"), t4_p3_env = c("low_env", "low_env"), t4_p3_eth = c("high_eth", 
"mid_eth"), t4_p3_pri = c("$25", "$125")), row.names = c(NA, 
-2L), class = c("tbl_df", "tbl", "data.frame"))

R相关问答推荐

将模拟变量乘以多个观测结果中的模拟变量

使用R中的gt对R中的html rmarkdown文件进行条件格式设置表的单元格

R创建一个数据透视表,计算多个组的百分比

根据R中两个变量的两个条件删除带有dspirr的行

次级y轴R gggplot2

获取一个数据库框架的摘要,该数据库框架将包含一列数据库框架,

为什么观察不会被无功值变化触发?

在数学中正确显示摄氏度、开氏度或华氏度

如何调整曲线图中的y轴标签?

在数组索引上复制矩阵时出错

找出二叉树中每个 node 在R中的深度?

如何在R库GoogleDrive中完全删除预先授权的Google帐户?

在列表中排列R数据框中的列顺序

基于数据集属性将科分配给物种

解析嵌套程度极高的地理数据

以任意顺序提取具有多个可能匹配项的组匹配项

如何构建一个for循环来循环处理动物ID?

随机 Select 的非NA列的行均数

根据列和行的不同组合 Select 各种单元格

向数据添加标签