Rows for Columns Without Using Loop
I will present my doubt in the form of an example: I have a 30x10 dataframe, and I would like each row of each column to become a new column, so that at the end of the process i would have a 1x300 dataframe. I currently do it through for, but I would like a code that would provide me with greater efficiency and speed.
Below I leave the current shape i use:
data <- data.frame(matrix(nrow = 30, ncol = 10))
data[,] <- sample(1:20,300,replace = TRUE)
newdata <- data.frame(matrix(nrow = 1, ncol = 300))
j <- 1
for (i in 1:ncol(data)) {
newdata[1,j:(j+29)] <- data[1:nrow(data),i]
j <- j + 30
}
Solution 1:
You could use this:
data.frame(t(c(data.matrix(data))))
Or if you want to use the matrix directly:
data.frame(t(c(matrix(nrow = 30, ncol = 10))))
Output:
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X20 X21 X22 X23 X24 X25 X26 X27 X28 X29 X30 X31
1 1 9 4 13 10 9 5 8 2 2 11 18 4 11 16 17 9 3 10 18 4 18 15 12 20 17 19 18 14 11 18
X32 X33 X34 X35 X36 X37 X38 X39 X40 X41 X42 X43 X44 X45 X46 X47 X48 X49 X50 X51 X52 X53 X54 X55 X56 X57 X58 X59 X60
1 14 7 6 11 4 14 2 10 19 15 18 6 13 6 7 2 15 1 20 7 18 1 4 11 16 17 8 4 8
X61 X62 X63 X64 X65 X66 X67 X68 X69 X70 X71 X72 X73 X74 X75 X76 X77 X78 X79 X80 X81 X82 X83 X84 X85 X86 X87 X88 X89
1 2 16 4 8 4 16 9 8 15 12 1 14 8 13 12 3 2 7 1 19 13 4 9 5 6 18 14 2 3
X90 X91 X92 X93 X94 X95 X96 X97 X98 X99 X100 X101 X102 X103 X104 X105 X106 X107 X108 X109 X110 X111 X112 X113 X114
1 9 13 7 12 20 16 7 9 18 16 18 12 16 1 18 1 19 18 16 17 7 17 20 13 16
X115 X116 X117 X118 X119 X120 X121 X122 X123 X124 X125 X126 X127 X128 X129 X130 X131 X132 X133 X134 X135 X136 X137
1 2 11 16 17 1 14 13 18 12 19 2 10 14 17 7 15 18 2 11 10 19 14 13
X138 X139 X140 X141 X142 X143 X144 X145 X146 X147 X148 X149 X150 X151 X152 X153 X154 X155 X156 X157 X158 X159 X160
1 9 4 1 20 11 16 18 8 6 2 5 9 9 20 9 12 1 15 16 11 17 1 9
X161 X162 X163 X164 X165 X166 X167 X168 X169 X170 X171 X172 X173 X174 X175 X176 X177 X178 X179 X180 X181 X182 X183
1 8 10 5 5 15 5 19 11 20 13 11 2 8 12 8 18 14 10 8 11 2 2 17
X184 X185 X186 X187 X188 X189 X190 X191 X192 X193 X194 X195 X196 X197 X198 X199 X200 X201 X202 X203 X204 X205 X206
1 16 16 10 16 11 9 18 9 18 11 20 14 11 18 18 16 12 11 5 8 13 17 9
X207 X208 X209 X210 X211 X212 X213 X214 X215 X216 X217 X218 X219 X220 X221 X222 X223 X224 X225 X226 X227 X228 X229
1 9 17 5 19 14 7 18 13 19 10 18 14 14 10 10 19 1 7 17 1 10 15 4
X230 X231 X232 X233 X234 X235 X236 X237 X238 X239 X240 X241 X242 X243 X244 X245 X246 X247 X248 X249 X250 X251 X252
1 9 9 6 18 15 4 11 12 6 17 20 19 7 17 4 10 5 2 13 7 18 2 15
X253 X254 X255 X256 X257 X258 X259 X260 X261 X262 X263 X264 X265 X266 X267 X268 X269 X270 X271 X272 X273 X274 X275
1 8 20 18 8 20 2 5 5 8 7 17 17 5 4 20 15 16 7 12 4 15 17 20
X276 X277 X278 X279 X280 X281 X282 X283 X284 X285 X286 X287 X288 X289 X290 X291 X292 X293 X294 X295 X296 X297 X298
1 20 20 9 6 12 2 14 1 8 3 11 20 18 7 6 8 9 12 15 14 11 20 6
X299 X300
1 13 17
Doublecheck:
if(newdata == data.frame(t(c(data.matrix(data))))){
print(TRUE)
} else {
print(FALSE)
}
[1] TRUE