1

I have a data frame like this,

user_name1     user_name2        user_name3
0                Alex              0
0                  0               0
0                  0             Jacob
0
                  Lee             Mark
John               0              Kevin

I want to rearrange this in this way ignoring 0 or any NA values,

user_name1     user_name2        user_name3
    John          Alex              Jacob
    0             Lee                Mark
    0              0                 Kevin
    0              0                   0
    0              0                   0
    0              0                   0

noticeable that, row numbers will be unchanged.

user_name1 <- c(0,0,0,0, "", "John")
user_name2 <- c("Alex", 0,0, "", "Lee",0)
user_name3 <- c(0,0, "Jacob", "",  "Mark", "Kevin")
df<- data.frame(user_name1, user_name2, user_name3)
bim
  • 612
  • 7
  • 18

2 Answers2

3

A decreasing sort works well:

df[] <- lapply(df, sort, decreasing=TRUE)
df
#  user_name1 user_name2 user_name3
#1       John        Lee       Mark
#2          0       Alex      Kevin
#3          0          0      Jacob
#4          0          0          0
#5          0          0          0

update

If there are blank spaces of NA values in the data you can fix them first then run the above code:

#Example with NA and blank ""
  user_name1 user_name2 user_name3
1          0       Alex          0
2          0          0          0
3          0          0      Jacob
4          0                  <NA>
5                   Lee       Mark
6       John          0      Kevin

First coerce values to zero, then sort:

df[df=="" | is.na(df)] <- 0
df[] <- lapply(df, sort, decreasing=TRUE)
#  user_name1 user_name2 user_name3
#1       John        Lee       Mark
#2          0       Alex      Kevin
#3          0          0      Jacob
#4          0          0          0
#5          0          0          0
#6          0          0          0

data

user_name1 <- c(0,0,0,0,"", "John")
user_name2 <- c("Alex", 0,0,"", "Lee",0)
user_name3 <- c(0,0, "Jacob", NA, "Mark", "Kevin")
df<- data.frame(user_name1, user_name2, user_name3,
                      stringsAsFactors=FALSE)
akrun
  • 874,273
  • 37
  • 540
  • 662
Pierre L
  • 28,203
  • 6
  • 47
  • 69
  • 1
    sorry, it still creating error for me. because original data frame has some missing values and NA values as well. `user_name1 <- c("John", , NA, 0, 0)` thanks for your help. Trying to figure out solution based on your suggestion – bim Feb 23 '16 at 16:21
  • 1
    Can you be more specific about "no values at all"? Please add actual data from your original – Pierre L Feb 23 '16 at 16:23
  • Sorry for the misunderstanding. Can you see my edited question now? the main data frame I am working has some thing like this. – bim Feb 23 '16 at 16:27
  • 1
    Instead of editing the table in the post. Add the correct code. The space you create can mean different things in actual code. Please add "code" that summarises the variability, not just an added space in the question. – Pierre L Feb 23 '16 at 16:29
  • 1
    You probably need to add a `na.last = TRUE` since by default, `sort` will remove `NA`s (check `sort(c(0, "a", NA))`) – talat Feb 23 '16 at 16:36
  • @docendodiscimus I just cleaned it along with the blanks simultaneously – Pierre L Feb 23 '16 at 16:38
  • Thanks, I am new in R, so little slow in understanding. I have also edited the code. going to follow you. – bim Feb 23 '16 at 16:39
  • I added an example with blanks and NA's – Pierre L Feb 23 '16 at 16:40
  • Thank you @PierreLafortune – bim Feb 23 '16 at 16:48
  • Thank you @docendodiscimus :) – bim Feb 23 '16 at 16:49
2

With dplyr

library(dplyr)
df[df=="" | is.na(df)] <- 0
df <- df %>% mutate_each(funs(sort(.,decreasing = TRUE))) 

#      user_name1 user_name2 user_name3
#1       John        Lee       Mark
#2          0       Alex      Kevin
#3          0          0      Jacob
#4          0          0          0
#5          0          0          0
bim
  • 612
  • 7
  • 18
akrun
  • 874,273
  • 37
  • 540
  • 662
  • I get an error on this `Error: wrong result size (5), expected 6 or 1`. Try it on the data I provided. – Pierre L Feb 23 '16 at 16:42
  • 1
    No, it is working fine. `library(dplyr); df %>% mutate_each(funs(sort(.,decreasing = TRUE)))` – bim Feb 23 '16 at 16:43
  • with `NA` values we will have to add `na.last=TRUE` – Pierre L Feb 23 '16 at 16:44
  • What mistake? I don't understand, sorry. – Pierre L Feb 23 '16 at 16:48
  • 1
    nope @akrun, you did not make mistake, it is working fine and excellent. you guys are genius – bim Feb 23 '16 at 16:51
  • @akrun , as I have changed the data frame with null values, instead of your previous code, this edited code will work now. hope you wont mind. – bim Feb 23 '16 at 17:05
  • 1
    @bipu I approved your edits. Thanks for doing the extra step and plus one to you as well. – akrun Feb 23 '16 at 17:06
  • 1
    Why not just treat every answer on its own merits. Who cares about the petty stuff? That kind of attitude is ruining SO – Pierre L Feb 23 '16 at 17:13