Use cbind
cbind(rn=rownames(dat), dat[1])
# rn freq
# A A 5, 38, 43, 27, 44, 20, 177
# B B 3, 5, 12, 53, 73
BTW, here is a complete solution to your initial problem.
res <- apply(dat_in[-c(1, length(dat_in))], 1, \(x) list2DF(list(unname(x[x != 0])))) |>
list2DF() |> t() |> as.data.frame() |> cbind(dat_in$rn) |> subset(select=2:1) |>
setNames(c('rn', 'values'))
res
# rn values
# 1 W 5, 38, 43, 27, 44, 20
# 2 M 3, 5, 12, 53
where
str(res)
# 'data.frame': 2 obs. of 2 variables:
# $ rn : chr "W" "M"
# $ values:List of 2
# ..$ 1: int 5 38 43 27 44 20
# ..$ 2: int 3 5 12 53
Data:
dat_in <- structure(list(rn = c("W", "M"), ` 0` = c(0L, 0L), `[ 0, 25)` = c(5L,
0L), `[ 25, 50)` = c(0L, 0L), `[ 25, 100)` = c(38L,
3L), `[ 50, 100)` = c(0L, 0L), `[ 100, 250)` = c(43L,
5L), `[ 100, 500)` = c(0L, 0L), `[ 250, 500)` = c(27L,
12L), `[ 500, 1000)` = c(44L, 0L), `[ 500,1000000]` = c(0L,
53L), `[ 1000, 1500)` = c(0L, 0L), `[ 1000,1000000]` = c(20L,
0L), `[ 1500, 3000)` = c(0L, 0L), `[ 3000,1000000]` = c(0L,
0L), Sum_col = c(177, 73)), row.names = 1:2, class = c("data.table",
"data.frame"))
data.frame(dat, rn = rownames(dat))