The aim is to swap the rows and columns, while making the columns in the order of the numerical value of the identifications. For example, the data I have look like:
structure(list(pair_id = structure(c(1L, 1L), .Label = "12_34 ", class = "factor"), member_id = c(34, 12), trait1 = c(5, 2), trait2 = c(3, 3)), class = "data.frame", row.names = c(NA, -2L), variable.labels = structure(character(0), .Names = character(0)), codepage = 65001L) Visually, the above looks like:
╔═════════╦═══════════╦════════╦════════╗ ║ pair_id ║ member_id ║ trait1 ║ trait2 ║ ╠═════════╬═══════════╬════════╬════════╣ ║ 12_34 ║ 34 ║ 5 ║ 3 ║ ╠═════════╬═══════════╬════════╬════════╣ ║ 12_34 ║ 12 ║ 2 ║ 3 ║ ╚═════════╩═══════════╩════════╩════════╝ In swapping 'member_id' and 'trait1-trait2', I'm looking for a way to make sure that member_id's are ordered numerically (that is, member 12 will become the first column or 'member 1' below; I thought I can just sort the data first for this), and that the traits also are structured in the order. That is, the expected end result would be:
╔═════════╦════════╦═════════╦═════════╗ ║ pair_id ║ trait ║ member1 ║ member2 ║ ╠═════════╬════════╬═════════╬═════════╣ ║ 12_34 ║ trait1 ║ 2 ║ 5 ║ ╠═════════╬════════╬═════════╬═════════╣ ║ 12_34 ║ trait2 ║ 3 ║ 3 ║ ╚═════════╩════════╩═════════╩═════════╝ https://stackoverflow.com/questions/66147386/swapping-rows-and-columns-by-order February 11, 2021 at 09:03AM
没有评论:
发表评论