Problems with indexing in cbind

Asked

Viewed 50 times

2

I try to perform the function cbind as follows:

reg<-lm(cbind(variable1,variable2,variable3,variable4,variable5,variable6)~kmeans,data=dataset)

and it works.

But consider this structure:

vars<-names(dataset)[1:6]
vars
#[1] "variable1" "variable2" "variable3" "variable4" "variable5" "variable6"

reg<-lm(cbind(vars)~kmeans,data=dataset)

returning:

Error in model.frame.default(formula = cbind(vars) ~ kmeans, data = dataset,  : 
variable lengths differ (found for 'kmeans')

I tried that too:

reg<-lm(cbind(dataset[,c(1:6)])~kmeans,data=dataset)

but makes a mistake.

dput for assistance in response:

dataset=structure(list(variable1 = c(43.715060595423, 35.7412599027157, 
94.2923714034259, 96.7777351103723, 76.7126309499145, 66.269299890846, 
33.242684584111, 65.9016692452133, 90.3558066487312, 97.9997940175235, 
75.0466591119766, 71.56977660954, 30.1207457669079, 56.4753429591656, 
61.4543196000159, 31.4821826294065, 99.7049777023494, 62.3780259676278, 
35.4981143586338, 55.1403805613518, 70.1368744112551, 76.3776612095535, 
45.8356131054461, 21.9446876645088, 21.1162203922868, 96.905946675688, 
38.7824204005301, 84.6504587866366, 20.1018105261028, 33.6058943346143
), variable2 = c(34.3370299972594, 79.2898517847061, 34.8683862946928, 
24.9095291644335, 67.5286993570626, 94.1996664181352, 99.0980565734208, 
94.9659042432904, 96.1392820253968, 97.6973397284746, 39.4121815264225, 
26.8778464011848, 96.3053766638041, 22.547991219908, 72.9508824273944, 
31.3082140125334, 61.387897990644, 79.5275260135531, 61.1386750824749, 
74.4083386473358, 90.9201584197581, 34.5035562105477, 99.5661793276668, 
97.1482198499143, 79.9117597751319, 99.6591885946691, 77.1700587309897, 
52.1403554826975, 71.6546232998371, 64.6964522451162), variable3 = c(30.5059113353491, 
76.2086549960077, 39.7139771841466, 37.7746463939548, 93.9186572097242, 
79.2327047139406, 98.0978714674711, 76.6704231314361, 71.6014115139842, 
29.317216090858, 67.4633122421801, 74.219619166106, 21.7488197237253, 
31.7424241267145, 28.2694784365594, 50.287730358541, 21.8935756199062, 
73.9493303000927, 72.9788122139871, 96.3505859673023, 97.3560711927712, 
53.8786559738219, 76.4053326845169, 42.7507965452969, 68.9038430526853, 
22.056569326669, 77.8469097800553, 46.1005675978959, 44.8027404770255, 
49.1913375817239), variable4 = c(93.28215200454, 32.3373851180077, 
85.9412641637027, 77.4457137659192, 70.0383077189326, 82.9604189656675, 
61.42000220716, 77.1301612630486, 74.2241130396724, 96.7386015690863, 
77.1354926005006, 68.7854127399623, 35.4604564234614, 29.9432530626655, 
24.7897118143737, 32.1905200555921, 97.7257819660008, 57.8458539769053, 
27.1137307956815, 29.4007654115558, 23.2586256042123, 55.3700133599341, 
99.1933928988874, 85.2835193276405, 68.2672765851021, 60.9877277165651, 
66.6570838540792, 68.3115924336016, 23.6951937153935, 65.4859019443393
), variable5 = c(35.3831980377436, 98.0542505159974, 44.3244373612106, 
47.6947486959398, 76.9843175448477, 40.8360417932272, 87.1477203816175, 
45.0157712027431, 37.741565797478, 55.1681268587708, 61.99483089149, 
66.6674076579511, 84.4966142997146, 52.9899350181222, 94.6549653448164, 
45.5086259543896, 93.6255992949009, 40.0677049346268, 42.5229249149561, 
40.4125453345478, 70.9578002244234, 75.3392943926156, 36.9032653234899, 
44.2945418506861, 23.4652379341424, 58.1121163070202, 57.0715335570276, 
86.7404271662235, 93.608070332557, 68.2579671032727), variable6 = c(79.9004037119448, 
65.9739377349615, 30.3757613711059, 84.3126894161105, 41.0510690510273, 
25.1393372192979, 97.5567859970033, 89.5824545063078, 36.5133770182729, 
83.3407552540302, 22.6602656394243, 31.8925970233977, 79.0916395373642, 
80.9901917167008, 71.082565523684, 71.9431832432747, 62.866341304034, 
77.6945791020989, 98.4018620289862, 92.9694261029363, 98.6945399828255, 
29.2068534903228, 49.2816692404449, 59.7111198864877, 83.6879147775471, 
74.2909290455282, 36.3198612257838, 31.4652634598315, 43.965809289366, 
60.9524113871157), kmeans = structure(c(3L, 1L, 2L, 3L, 2L, 2L, 
1L, 1L, 2L, 3L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 
1L, 1L, 1L, 3L, 1L, 2L, 3L, 3L), .Label = c("1", "2", "3"), class = "factor")), .Names = c("variable1", 
"variable2", "variable3", "variable4", "variable5", "variable6", 
"kmeans"), row.names = c(NA, -30L), class = "data.frame")

1 answer

2


In this case the function parameter needs to be a Matrix, try so:

reg<-lm(as.matrix(cbind(dataset[,c(1:6)]))~kmeans,data=dataset)

or so

reg<-lm(as.matrix(dataset[,vars])~kmeans,data=dataset)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.