dcast 数据框没有给出正确的列

dcast a dataframe not giving correct columns

我正在尝试使用 dcast 传播我的数据 d:

看起来像:

  row_id variable        value
1     27 feature1  0.006960242
2     35 feature1 -0.002475289
3     27 feature2 -0.016615848
4     35 feature2  0.010806291
5     27 feature3  0.014437451
6     35 feature3 -0.009046077

我运行的代码是:

mutated_d <- d %>%
  group_by(row_id) %>%
  mutate(NewVar = sqrt(abs(value)))

mydcasted <- dcast(mutated_d, row_id ~ variable, value.var = c("value", "NewVar"))

给我这个错误:

Error in .subset2(x, i) : subscript out of bounds In addition: Warning message: In if (!(value.var %in% names(data))) { : the condition has length > 1 and only the first element will be used

好的,所以我尝试以下操作:

mydcasted <- dcast(mutated_d, row_id ~ variable, value.var = "value")

效果很好。但是它不包含我新创建的变量 NewVar。所以我尝试:

mydcasted <- dcast(mutated_d, row_id ~ variable, value.var = "NewVar")

这给了我与以前相同的输出...列为 feature1feature2...featureN。我只想要 NewVar 的数据。 (还有 NewVar2NewVar3 .. NewVarN)。

感谢任何帮助!

d <- structure(list(row_id = c(27L, 35L, 27L, 35L, 27L, 35L, 27L, 
35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 
27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 
35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 
27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 
35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 
27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 
35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 
27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 
35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 
27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 
35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 
27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 
35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 
27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 
35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 
27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 
35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 
27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 
35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 
27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 
35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 
27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 
35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 
27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 
35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 
27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 
35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 
27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 
35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 
27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 
35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 
27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 
35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 
27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 
35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 
27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 
35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 
27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 
35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 27L, 35L, 
27L, 35L, 27L, 35L, 27L, 35L), variable = structure(c(1L, 1L, 
2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 
10L, 10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 
16L, 17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 
23L, 23L, 24L, 24L, 25L, 25L, 26L, 26L, 27L, 27L, 28L, 28L, 29L, 
29L, 30L, 30L, 31L, 31L, 32L, 32L, 33L, 33L, 34L, 34L, 35L, 35L, 
36L, 36L, 37L, 37L, 38L, 38L, 39L, 39L, 40L, 40L, 41L, 41L, 42L, 
42L, 43L, 43L, 44L, 44L, 45L, 45L, 46L, 46L, 47L, 47L, 48L, 48L, 
49L, 49L, 50L, 50L, 51L, 51L, 52L, 52L, 53L, 53L, 54L, 54L, 55L, 
55L, 56L, 56L, 57L, 57L, 58L, 58L, 59L, 59L, 60L, 60L, 61L, 61L, 
62L, 62L, 63L, 63L, 64L, 64L, 65L, 65L, 66L, 66L, 67L, 67L, 68L, 
68L, 69L, 69L, 70L, 70L, 71L, 71L, 72L, 72L, 73L, 73L, 74L, 74L, 
75L, 75L, 76L, 76L, 77L, 77L, 78L, 78L, 79L, 79L, 80L, 80L, 81L, 
81L, 82L, 82L, 83L, 83L, 84L, 84L, 85L, 85L, 86L, 86L, 87L, 87L, 
88L, 88L, 89L, 89L, 90L, 90L, 91L, 91L, 92L, 92L, 93L, 93L, 94L, 
94L, 95L, 95L, 96L, 96L, 97L, 97L, 98L, 98L, 99L, 99L, 100L, 
100L, 101L, 101L, 102L, 102L, 103L, 103L, 104L, 104L, 105L, 105L, 
106L, 106L, 107L, 107L, 108L, 108L, 109L, 109L, 110L, 110L, 111L, 
111L, 112L, 112L, 113L, 113L, 114L, 114L, 115L, 115L, 116L, 116L, 
117L, 117L, 118L, 118L, 119L, 119L, 120L, 120L, 121L, 121L, 122L, 
122L, 123L, 123L, 124L, 124L, 125L, 125L, 126L, 126L, 127L, 127L, 
128L, 128L, 129L, 129L, 130L, 130L, 131L, 131L, 132L, 132L, 133L, 
133L, 134L, 134L, 135L, 135L, 136L, 136L, 137L, 137L, 138L, 138L, 
139L, 139L, 140L, 140L, 141L, 141L, 142L, 142L, 143L, 143L, 144L, 
144L, 145L, 145L, 146L, 146L, 147L, 147L, 148L, 148L, 149L, 149L, 
150L, 150L, 151L, 151L, 152L, 152L, 153L, 153L, 154L, 154L, 155L, 
155L, 156L, 156L, 157L, 157L, 158L, 158L, 159L, 159L, 160L, 160L, 
161L, 161L, 162L, 162L, 163L, 163L, 164L, 164L, 165L, 165L, 166L, 
166L, 167L, 167L, 168L, 168L, 169L, 169L, 170L, 170L, 171L, 171L, 
172L, 172L, 173L, 173L, 174L, 174L, 175L, 175L, 176L, 176L, 177L, 
177L, 178L, 178L, 179L, 179L, 180L, 180L, 181L, 181L, 182L, 182L, 
183L, 183L, 184L, 184L, 185L, 185L, 186L, 186L, 187L, 187L, 188L, 
188L, 189L, 189L, 190L, 190L, 191L, 191L, 192L, 192L, 193L, 193L, 
194L, 194L, 195L, 195L, 196L, 196L, 197L, 197L, 198L, 198L, 199L, 
199L, 200L, 200L, 201L, 201L, 202L, 202L, 203L, 203L, 204L, 204L, 
205L, 205L, 206L, 206L, 207L, 207L, 208L, 208L, 209L, 209L, 210L, 
210L, 211L, 211L, 212L, 212L, 213L, 213L, 214L, 214L, 215L, 215L, 
216L, 216L, 217L, 217L, 218L, 218L, 219L, 219L, 220L, 220L, 221L, 
221L, 222L, 222L, 223L, 223L, 224L, 224L, 225L, 225L, 226L, 226L, 
227L, 227L, 228L, 228L, 229L, 229L, 230L, 230L, 231L, 231L, 232L, 
232L, 233L, 233L, 234L, 234L, 235L, 235L, 236L, 236L, 237L, 237L, 
238L, 238L, 239L, 239L, 240L, 240L, 241L, 241L, 242L, 242L, 243L, 
243L, 244L, 244L, 245L, 245L, 246L, 246L, 247L, 247L, 248L, 248L, 
249L, 249L, 250L, 250L, 251L, 251L, 252L, 252L, 253L, 253L, 254L, 
254L, 255L, 255L, 256L, 256L, 257L, 257L, 258L, 258L, 259L, 259L, 
260L, 260L), .Label = c("feature1", "feature2", "feature3", "feature4", 
"feature5", "feature6", "feature7", "feature8", "feature9", "feature10", 
"feature11", "feature12", "feature13", "feature14", "feature15", 
"feature16", "feature17", "feature18", "feature19", "feature20", 
"feature21", "feature22", "feature23", "feature24", "feature25", 
"feature26", "feature27", "feature28", "feature29", "feature30", 
"feature31", "feature32", "feature33", "feature34", "feature35", 
"feature36", "feature37", "feature38", "feature39", "feature40", 
"feature41", "feature42", "feature43", "feature44", "feature45", 
"feature46", "feature47", "feature48", "feature49", "feature50", 
"feature51", "feature52", "feature53", "feature54", "feature55", 
"feature56", "feature57", "feature58", "feature59", "feature60", 
"feature61", "feature62", "feature63", "feature64", "feature65", 
"feature66", "feature67", "feature68", "feature69", "feature70", 
"feature71", "feature72", "feature73", "feature74", "feature75", 
"feature76", "feature77", "feature78", "feature79", "feature80", 
"feature81", "feature82", "feature83", "feature84", "feature85", 
"feature86", "feature87", "feature88", "feature89", "feature90", 
"feature91", "feature92", "feature93", "feature94", "feature95", 
"feature96", "feature97", "feature98", "feature99", "feature100", 
"feature101", "feature102", "feature103", "feature104", "feature105", 
"feature106", "feature107", "feature108", "feature109", "feature110", 
"feature111", "feature112", "feature113", "feature114", "feature115", 
"feature116", "feature117", "feature118", "feature119", "feature120", 
"feature121", "feature122", "feature123", "feature124", "feature125", 
"feature126", "feature127", "feature128", "feature129", "feature130", 
"feature131", "feature132", "feature133", "feature134", "feature135", 
"feature136", "feature137", "feature138", "feature139", "feature140", 
"feature141", "feature142", "feature143", "feature144", "feature145", 
"feature146", "feature147", "feature148", "feature149", "feature150", 
"feature151", "feature152", "feature153", "feature154", "feature155", 
"feature156", "feature157", "feature158", "feature159", "feature160", 
"feature161", "feature162", "feature163", "feature164", "feature165", 
"feature166", "feature167", "feature168", "feature169", "feature170", 
"feature171", "feature172", "feature173", "feature174", "feature175", 
"feature176", "feature177", "feature178", "feature179", "feature180", 
"feature181", "feature182", "feature183", "feature184", "feature185", 
"feature186", "feature187", "feature188", "feature189", "feature190", 
"feature191", "feature192", "feature193", "feature194", "feature195", 
"feature196", "feature197", "feature198", "feature199", "feature200", 
"feature201", "feature202", "feature203", "feature204", "feature205", 
"feature206", "feature207", "feature208", "feature209", "feature210", 
"feature211", "feature212", "feature213", "feature214", "feature215", 
"feature216", "feature217", "feature218", "feature219", "feature220", 
"feature221", "feature222", "feature223", "feature224", "feature225", 
"feature226", "feature227", "feature228", "feature229", "feature230", 
"feature231", "feature232", "feature233", "feature234", "feature235", 
"feature236", "feature237", "feature238", "feature239", "feature240", 
"feature241", "feature242", "feature243", "feature244", "feature245", 
"feature246", "feature247", "feature248", "feature249", "feature250", 
"feature251", "feature252", "feature253", "feature254", "feature255", 
"feature256", "feature257", "feature258", "feature259", "feature260"
), class = "factor"), value = c(0.00696024229830469, -0.0024752893900013, 
-0.0166158478838581, 0.0108062906243271, 0.0144374512212513, 
-0.00904607697992132, 0.00191739489546857, 0.00248965185866812, 
0.0144407434574832, 0.00248346888574069, -0.0341285519262852, 
-0.0189925910833653, -0.00728406304272133, -0.0101010090562482, 
-0.00623794301652144, 0.013605440755281, -0.0344755502809147, 
0.00251680403513332, -0.0103691360994962, -0.0184100908874977, 
-0.00424590700348645, -0.0375106534894419, -0.00503859216337831, 
-0.0194862150273969, -0.0320669552602426, 0.00632342343982351, 
-0.00664256465180771, -0.0278276710862506, -0.0345224089830492, 
0.0036934436128373, 0.00903500061489245, -0.0275989486653087, 
-0.0804581751866519, 0.0397351539984685, 0.0259914889513236, 
-0.00818923386991277, -0.0824925834208491, 0, 0.0491220315663671, 
-0.0183486207505298, 0.0303354045057385, 0.0149532684522347, 
0.0393009222633931, -0.00552491751376216, -0.0612479300927777, 
0.00925931509307154, 0.0402918173664227, -0.0137614937455851, 
-0.0800392733203219, -0.0148837188036512, -0.0000220967103218539, 
-0.00472140562386925, 0.00721588274605285, 0.0132826718344752, 
0.0365540666123501, -0.0018726014341115, -0.00468323734906866, 
0.0262664119687723, 0.00754996351504775, 0.0228518876315573, 
0.0124471976116125, -0.0035746197059463, 0.0215938641934065, 
-0.00627805359179645, 0.0040023467359131, 0.0108303237086709, 
0.0104556477197056, -0.0205357394605128, -0.000596291019393899, 
0.0145852312268577, -0.023891484959868, -0.0170708951207419, 
0.069008417116315, 0.0173673717015825, -0.0400161424849012, -0.00808622509942458, 
-0.0255484126182771, -0.0217391280151913, -0.0246392693232307, 
-0.00185179475387776, -0.0101438065444181, 0.0287568669223364, 
0.0238756781523455, 0.00541033448121508, 0.0219966853152304, 
0.0161434404324372, 0.0259265090293524, -0.00353044984522766, 
-0.00844362460472495, 0.00974316193200231, -0.0080623449662374, 
-0.0175438577581331, 0.00134581156839493, 0.025892826874325, 
-0.0466931712398315, -0.0147954464032681, -0.0499480159187248, 
0.0194346811397557, 0.00145864152584483, 0.0147313401782159, 
0.0201110892034762, -0.0136635336476006, 0.0379350690995802, 
0.0225107663239599, -0.0293317997468439, -0.0347163133628278, 
0.027480551115873, 0.00614037716195304, -0.0203187061870591, 
-0.00348735785907317, -0.0332805150819082, -0.00437442572963442, 
0.0110069137608891, -0.0246045654349861, 0.00769442286064929, 
0.00630622223447319, 0.0245931265438962, -0.0017904551447866, 
0.0267950369809876, -0.00627805359179645, 0.0227883139451571, 
-0.00722021580578058, 0.0011490051986798, -0.00181812576211371, 
0.026369924554871, -0.00819680386758881, 0.00446734641105093, 
0, 0.030234322044934, 0.00184675863788719, -0.0192715595118441, 
0.0101382803002439, 0.0511717022955589, 0.0273722454796492, -0.0298696571429239, 
0.0106571870517564, -0.0284464122905248, 0.00966611075550117, 
0.0656783218752391, 0.00783289317401392, 0.008079699489354, 0.00345419407098069, 
0.012416913406533, 0.00516350811054633, 0.0428475987603121, -0.00428085425415958, 
-0.022453824082696, 0.00515906847607406, 0.0102281465919991, 
-0.00171079334356505, -0.0204076189489488, 0.0239930948372955, 
-0.023876747298392, 0.00334734656776314, 0.0261047496083704, 
-0.013344513043091, 0.0201287798565949, 0.00169065051773654, 
0.000689096176236488, -0.0185654581048493, -0.0363579936928865, 
0.0292347914128723, 0.0206675636554778, 0.0158729721209507, -0.00719640910476122, 
0, 0.0212703333015957, 0.0254934735304881, 0.0180690711178583, 
-0.00962308945661206, 0.00293390713453955, 0.0121457413765484, 
0.00263249671752459, -0.0112000256521225, -0.0203127523783442, 
-0.00323621107930461, -0.0183254570171334, 0.0243506340538111, 
0.00225540843286932, 0.00237717762300238, -0.0042995528471391, 
0.0142292403063664, -0.0453581363380724, 0.021044414429525, -0.0316214728113201, 
0.00152674774931327, 0.0493956905728298, -0.00838417218209964, 
0.0133000955849888, -0.0192159449615616, -0.00406567622666737, 
0.000783666636659408, 0.0493243723010676, -0.00783081691957122, 
0.00661952483120701, 0.0299920237570515, 0.00875693531657244, 
-0.0268198458769039, -0.00217700894718734, 0.00472440641826499, 
-0.0084267909671163, 0.00313476231428256, -0.00832700276261352, 
0.00546871487062162, -0.0137792004307814, 0.0116550050152129, 
0.00855552243406948, 0.00230414616790009, -0.0109578348591384, 
-0.00613020232870154, -0.0132216183833317, 0.0285273215317055, 
-0.0140535485239094, -0.010494685465851, 0.0132087760051347, 
-0.0113637215750738, 0.0163758255810065, -0.0283524432788201, 
-0.0132103414410496, 0.0134069640040504, 0.042034289998256, -0.011673144350436, 
-0.00865481392562152, 0.00236220320913239, 0.00821447750795275, 
0.000785513453945841, 0.00210177011263904, -0.0102040434612083, 
0.0067449111070277, -0.0174465243880577, -0.00687584966018104, 
-0.0250201298806332, 0.0166337926397053, 0.0165562133445716, 
-0.00478115208350862, 0.0081433842242602, 0.0109334145047826, 
0.0347333864180668, -0.00485622049339733, 0.0304450428394705, 
-0.00823904906107771, -0.0106060845915023, -0.00017734291286895, 
0, 0.0121082379861545, 0.00689126675798879, 0.0164820145526983, 
-0.000760517754242351, -0.00335900886118942, -0.0136986224933056, 
0.0251413158052075, 0.00308648086681695, -0.00493098911795795, 
-0.00615387363066044, 0.00677157842129127, 0.00928792013296675, 
0.0213930716632289, 0.00613499692111574, -0.0125535626906284, 
0.0167682512243237, -0.0239270207294236, 0.00149928003962407, 
0.00732556269027844, 0.0067365228383216, 0.00820516860454116, 
-0.00669144576113034, 0.00498246896886224, 0.0187125329508806, 
-0.00786697017163955, -0.00220426031892362, 0.0143509883736784, 
-0.00589095285303043, -0.000928934391799982, 0.00814808263982081, 
-0.00586625854613305, -0.00442800432376211, 0.015895937649003, 
0, -0.00276055827332625, -0.0185322134166701, 0.0322200804603656, 
0.00453168082933719, -0.00803774864534239, 0.0157894679068873, 
-0.000749616980167015, 0.00518134528388803, -0.00486724230185588, 
0.00147274072399117, -0.00365007679507084, 0.0198529854442691, 
-0.0141000987687483, 0.0136986373464438, 0.0145123782280468, 
-0.0213371923284129, -0.00630395332153559, -0.0290697705261493, 
-0.0116273480102284, -0.0217065277305134, 0.0107613017523962, 
0.00306041598870488, 0.00390048548290862, -0.00839051205369945, 
-0.00538928928016675, 0.0138461217392218, -0.00349829576384715, 
0.0265553774950964, -0.00196560886579872, 0.00147818324184934, 
0.00995833216278598, -0.0258302496883732, 0.00779904071661459, 
-0.0121211948049365, -0.00175440223680763, -0.00306745690208121, 
-0.00521286226639449, -0.000769270578947268, 0.00849446859417421, 
0.00615862152223601, -0.00116786956897593, -0.00535577452223701, 
-0.00368061995119942, 0.00538461329694484, -0.0137396560849759, 
-0.00153020799435244, -0.0270135520956603, -0.00459775258482575, 
0.0200243802373202, -0.0069283906767178, 0.0256903085341671, 
0.0209302788361796, -0.0117055436165737, -0.0045558595792401, 
0.0239725018284413, 0.0152555776976355, 0.00408865451983712, 
0.0127722811554469, 0.00554858944189857, -0.00296733226919244, 
-0.011822575161756, 0.00148808176764459, 0.00232881032985631, 
0.00965829493922699, -0.0123363910091844, -0.00883003136324012, 
0.00425653579399348, -0.00222719643576308, 0.00281253704471981, 
-0.00446424530293354, -0.000377692820144857, 0.0186845703832705, 
0.0272490358178752, 0.00146733813668853, -0.00313861685492541, 
-0.0117215948099948, -0.0111150485132915, 0.0214974361046323, 
-0.0158279163258207, -0.00653127768379047, 0.0179619822795058, 
0.00803512291429254, 0.00670585953290761, -0.00289861256440416, 
-0.000907587271594797, -0.00436042579159535, -0.0121078501941389, 
0.0080291687539189, -0.0303161811581844, -0.0195510055648584, 
-0.0160293808895104, -0.0162481858703832, -0.000966277603922321, 
0.0277777549435432, 0.00692807580792184, -0.0138787517228245, 
0.00304655764712927, 0.00518527363542765, 0.00901022555528743, 
0.0574796599199765, -0.0153051222495685, 0.013240425739899, 0.024309094805229, 
-0.00825310400300017, -0.00509513314212792, -0.00485436729379629, 
-0.00644637903964608, 0, 0.0230285193639057, -0.00487804712622342, 
-0.0148189198824454, -0.00700273787924433, 0.00980687978115675, 
0.00564169763311417, 0.0104300794153732, 0.0126226920600458, 
0.00737309658049164, -0.0096952877080041, 0.00155234872947761, 
0, -0.00458096000752376, 0.000702314643596491, 0.00881886599057804, 
-0.00210529519617253, -0.0286532982176578, 0.00140643441187516, 
0.00655746049772372, 0.00561800668385759, -0.0174988171881668, 
-0.013268161446359, 0.0120214878429701, 0.0148620510932038, 0.00265658830147528, 
-0.0139470726819185, -0.0165831470242292, 0.00141447732839328, 
0.00297098681278353, 0.00918073114225781, -0.0014772192971475, 
-0.0111966143693968, -0.0106777272527109, 0.0120311801354602, 
-0.0150098018633419, 0.00909084978655095, 0.00944901642109611, 
0.0152460523082329, -0.00349472728589403, -0.00341296617863063, 
-0.009153461286255, 0.015753443470051, 0.00628956500898368, 0.0026972491604027, 
0.00423444006837273, 0.00537990568198321, 0.0101526293243281, 
-0.0013377590348167, -0.00927822329286958, 0.000669775515655724, 
-0.00826803350585307, -0.00468537160797311, 0.00625403498692322, 
0.00672494320721828, -0.0341354028360397, 0.00868397802655108, 
-0.00155905316963393, 0.00662251071157538, -0.022576500969729, 
-0.00526310545778297, -0.00715479771904205, 0, -0.00459442379513276, 
0.00396823425474357, 0.0147739030761579, 0.00131748993426695, 
0.00965538071052766, -0.00197363465760503, 0.0221960852043632, 
0.00197753759433783, -0.00629104380697697, 0, 0.0108887919249895, 
0.00131583614257313, -0.0165811599720308, 0.00328514808359581, 
-0.0107878671880834, 0.00916831129367868, 0.015754699816884, 
0.00259573038241201, 0.00614197688444165, -0.0129450500523377, 
0.015215679790898, -0.00327868565943834, -0.0105239904375599, 
-0.0144736396391155, -0.0308897118933981, -0.00200270073045239, 
-0.00630007376824934, 0.00668895724811258, -0.00217405408569986, 
0.019933537166428, -0.00981735622769504, 0.00325740508758798, 
0.0285993399403384, -0.00324682885077099, -0.02264463935953, 
0.00651473124987367, 0.0370050068890428, 0.0116504274254237, 
-0.00643519603643503, 0.00319897350086329, 0.00816507971675868, 
-0.000637739087448908, 0.00370673252901239, -0.00255266154608702, 
-0.0259069298513092, 0.0115163356076733, 0.00656519948770545, 
0.00695760014136515, -0.0299354930202179, -0.0144471777678782, 
0.0116453906254437, -0.00191207497390899, 0.0152941927260588, 
0.0121328268743934, 0.0228396136256276, 0.028391141151298, 0.0136969676068411, 
0.0184048915600432, 0.0160373599049133, 0.0337349686001678, 0.0145624082658708, 
0.00233101434459493, 0.00256695831186454, -0.0151163215772194, 
-0.00207595783372961, -0.00531280709983895, -0.00861127373560793, 
0.00745157222213444, -0.0180410738791992, -0.00473373532746524, 
0.00227452329969657, 0.00475625015685899)), row.names = c(NA, 
-520L), class = "data.frame")

此问题与从 reshape2 而不是 data.table 调用的 dcast 有关。 reshape2::dcast 不会占用多个 value.var,而 data.table::dcast 会占用

library(data.table)
dcast(setDT(mutated_d), row_id ~ variable, value.var = c('value', 'NewVar'))

此外,这主要可以在 data.table

中完成
dcast(setDT(d)[, NewVar := sqrt(abs(value))], 
        row_id ~ variable, value.var = c('value', 'NewVar'))

此外,从 tidyr (‘0.8.3.9000’) 的 dev 版本开始,我们可以将 pivot_wider 用于多个值列

library(tidyr)
libary(dplyr)
mutated_d %>%
   ungroup %>% 
   pivot_wider(names_from = variable, values_from = c('value', 'NewVar'))
# A tibble: 2 x 521
#  row_id value_feature1 value_feature2 value_feature3 value_feature4 value_feature5 value_feature6 value_feature7 value_feature8 value_feature9
#   <int>          <dbl>          <dbl>          <dbl>          <dbl>          #<dbl>          <dbl>          <dbl>          <dbl>          <dbl>
#1     27        0.00696        -0.0166        0.0144         0.00192        0.0144         -0.0341       -0.00728       -0.00624       -0.0345 
#2     35       -0.00248         0.0108       -0.00905        0.00249        0.00248        -0.0190       -0.0101         0.0136         0.00252
# … with 511 more variables: ...