3

I am a novice R user and new to the forum. I have a data frame that I need to convert so that each row is a character vector. I want to remove the 0's from the data frame so that each row can have varying lengths. So in essence each row is a separate character vector in a list. Where I am at is the following:

mydf<-matrix(sample(0:1,12*5, replace = T),ncol =4)
colnames(mydf)<-letters[1:ncol(mydf)]
swapcol <-which(mydf == 1, arr.ind = T)
mydf[swapcol]<-colnames(mydf)[swapcol[,2]]
mydf

The code produces a data frame in which the column labels are values. I need the following output:

Desired List Result

the format appears to be what I need in order to read in data to the package clickstream. Thanks

yanga
  • 43
  • 3
  • I am getting close to solving my own problem.list_of_DF <- plyr::dlply(mydf, .(id), function(M) M[, !apply(M==0,2,all)]) – yanga Feb 27 '17 at 18:39

2 Answers2

0

This returns a list with the formart you are asking for:

list(
  apply(mydf, 1, function(a_row) {
    my_paste <- function(...){
      paste(..., sep = ", ")
    }
    a_row <- Reduce(my_paste, a_row)
    a_row <- gsub("0(, )*", "", a_row)
    a_row <- gsub(", $", "", a_row)
  })
)

This returns a list of length 1. Replacing list with as.list, returns a list of length 15.

Juan Bosco
  • 1,420
  • 5
  • 19
  • 23
  • 1
    Juan thanks for the help. I am looking at the str of the solution you gave and it is a list of 1 as opposed to a list of 15 vectors. Thanks though, this paste function you created may help me add commas to samuels solution below. – yanga Feb 27 '17 at 22:29
  • I understand. You can get a list of 15 elements changing `list` of `as.list`. I'll edit my answer to mention this for completion's sake. – Juan Bosco Feb 27 '17 at 22:58
0

Try this solution:

library(tidyverse)

s <- sample(x = 0:1, size = 15 * 4, replace = TRUE)

mx <- matrix(data = s, nrow = 15, ncol = 4, byrow = TRUE, 
dimnames = list(c(paste("User", 1:15, sep = " ")), c("V1", "V2", "V3", "V4")))

df2 <- mx %>% as.data.frame() %>% rownames_to_column() %>% as_tibble() 
%>% mutate(
  V1 = ifelse(test = V1 == 1, yes = "a", no = NA), 
  V2 = ifelse(test = V2 == 1, yes = "b", no = NA), 
  V3 = ifelse(test = V3 == 1, yes = "c", no = NA), 
  V4 = ifelse(test = V4 == 1, yes = "d", no = NA))

mx2 <- t(apply(X = df2, MARGIN = 1, FUN = function(x{return(c(x[!is.na(x)], 
x[is.na(x)]))}))
Samuel
  • 2,895
  • 4
  • 30
  • 45
  • Samuel, thanks for taking the time to help. It's hard to tell if the file is reading in correctly, but I need to mark this problem as solved. Thanks – yanga Feb 27 '17 at 22:22