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)
BiMo
  • 476
  • 5
  • 17

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
  • 789,025
  • 32
  • 460
  • 575
Pierre L
  • 27,528
  • 5
  • 43
  • 64
  • 1
    sorry, it still creating error for me. because original data frame has some missing values and NA values as well. `user_name1 , NA, 0, 0)` thanks for your help. Trying to figure out solution based on your suggestion – BiMo 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. – BiMo 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. – BiMo 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 – BiMo Feb 23 '16 at 16:48
  • Thank you @docendodiscimus :) – BiMo 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
BiMo
  • 476
  • 5
  • 17
akrun
  • 789,025
  • 32
  • 460
  • 575