I have a dfrm of over 100 columns and 150 rows. I need to merge the contents of every 4 columns to 1 (preferably separated by a "/", although dispensable) which is simple enough, performing apply(dfrm[ ,1:4], 1, paste, collapse="/")
. I have difficulties scaling that solution to my whole df. In other words:
How can I go from this:
loc1 loc1.1 loc1.2 loc1.3 loc2 loc2.1 loc2.2 loc2.3
ind.1 257 262 228 266 204 245 282 132
ind.2 244 115 240 187 196 133 189 251
ind.3 298 139 216 225 219 276 192 254
ind.4 129 176 180 182 215 250 227 186
ind.5 238 217 284 240 131 184 247 168
To something like this:
loc1 loc2
ind.1 257/262/228/266 204/245/282/132
ind.2 244/115/240/187 196/133/189/251
ind.3 298/139/216/225 219/276/192/254
ind.4 129/176/180/182 215/250/227/186
ind.5 238/217/284/240 131/184/247/168
In a dataframe of over 100 rows and columns. I've tried indexing the data frame as presented in the solution of this question, but after creating said index of every 4 columns y do find myself lost while trying to perform do.call
over my data frame. I'm sure there must be a easy solution for this, but please keep in mind that i'm all but proficient in R.
Also; the colnames are not a real problem if the body is in shape, since extracting a list of names is performed by loc <- colnames(dfrm)
and loc <- loc[c(T, F, F, F)
, and then defining colnames(dfrm) <- loc
, although would be nice if incorporated.