0

I'd like to get a function that sorts data frame in the way that equal numbers or 0 (/optionally NA) are in the same column.

It should look similar to this:

1 0 3 4 5
0 2 0 0 5
1 2 0 0 0
1 0 0 0 0
Konrad X
  • 19
  • 5
  • Can you rollback to your previous update – akrun Aug 26 '19 at 01:04
  • KonradX: thanks for deleting your other question, (I believe) that's the right thing to do. As suggested in its comments, though, it would be very useful to (1) un-accept @akrun's answer and explain why you are deselecting it; then (2) edit your question to provide sample unambiguous data, code attempted, and expected output. Asking questions in a way that SO deals with *well* is not always intuitive, and most definitely takes extra effort on your part. You will be rewarded by developing this skill, I'm confident. (https://stackoverflow.com/questions/5963269) – r2evans Aug 27 '19 at 21:10

2 Answers2

2

An option is to convert to data.frame and bind with map_df

library(purrr)
library(dplyr)
map_df(z, as.data.frame) %>%
    mutate_all(replace_na, 0)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Could you help me as well with repositioning this data frame from "triangular matrix" form to the shape I wanted? I've tried to do it by myself, but I failed :/ – Konrad X Aug 26 '19 at 00:31
  • @KonradX The above gives a rectangular format as you intendeed. I guess the values you mentioned are the `rnorm` values, right – akrun Aug 26 '19 at 00:34
  • @KonradX I am not fully getting the logic for expected output esp when you showed only 'x1', 'x2', 'x3', 'x4' – akrun Aug 26 '19 at 00:35
  • Can you please edit your post and update as the format is not cleear in the comments – akrun Aug 26 '19 at 00:39
  • I want to have in every column only 0 and the same number from vector "numbers". The attached image shows what I got, not what I wanted – Konrad X Aug 26 '19 at 00:44
  • @KonradX I saw your image update. Isnt it the same format from my output – akrun Aug 26 '19 at 00:49
0

Using just base R you could do this.

do.call(rbind, Map(function(z) {x <- t(apply(z, 1, `length<-`, l));x[is.na(x)] <- 0;x}, z))
#             [,1]       [,2]      [,3]      [,4]
#  [1,]  1.3709584  0.0000000 0.0000000 0.0000000
#  [2,] -0.5646982  0.0000000 0.0000000 0.0000000
#  [3,]  0.3631284  0.0000000 0.0000000 0.0000000
#  [4,]  0.6328626  0.0000000 0.0000000 0.0000000
#  [5,]  1.3709584 -0.5646982 0.0000000 0.0000000
#  [6,]  1.3709584  0.3631284 0.0000000 0.0000000
#  [7,]  1.3709584  0.6328626 0.0000000 0.0000000
#  [8,] -0.5646982  0.3631284 0.0000000 0.0000000
#  [9,] -0.5646982  0.6328626 0.0000000 0.0000000
# [10,]  0.3631284  0.6328626 0.0000000 0.0000000
# [11,]  1.3709584 -0.5646982 0.3631284 0.0000000
# [12,]  1.3709584 -0.5646982 0.6328626 0.0000000
# [13,]  1.3709584  0.3631284 0.6328626 0.0000000
# [14,] -0.5646982  0.3631284 0.6328626 0.0000000
# [15,]  1.3709584 -0.5646982 0.3631284 0.6328626

Explanation: Essentially this is an rbind problem where the matrices in list z have different number of columns. Since the needed number of columns is known by l we can take each single row of a matrix as a vector with apply() and prolong it to l with length<-. Because this yields NAs we need to convert these to the desired zeroes. Map applies that to the whole list. Finally wrapping do.call(rbind..) around it binds the listed matrices into a single one.

Data

z <- list(structure(c(1.37095844714667, -0.564698171396089, 0.363128411337339, 
0.63286260496104), .Dim = c(4L, 1L)), structure(c(1.37095844714667, 
1.37095844714667, 1.37095844714667, -0.564698171396089, -0.564698171396089, 
0.363128411337339, -0.564698171396089, 0.363128411337339, 0.63286260496104, 
0.363128411337339, 0.63286260496104, 0.63286260496104), .Dim = c(6L, 
2L)), structure(c(1.37095844714667, 1.37095844714667, 1.37095844714667, 
-0.564698171396089, -0.564698171396089, -0.564698171396089, 0.363128411337339, 
0.363128411337339, 0.363128411337339, 0.63286260496104, 0.63286260496104, 
0.63286260496104), .Dim = 4:3), structure(c(1.37095844714667, 
-0.564698171396089, 0.363128411337339, 0.63286260496104), .Dim = c(1L, 
4L)))

l <- 4L
Community
  • 1
  • 1
jay.sf
  • 60,139
  • 8
  • 53
  • 110