-1

I want to create a dataframe from a list. The list is from a shiny handsontable input

When I call the input I got:

unlist(input$hot$data)

                                    texta
2                                     80%
3                                    100%
4                                    100%
5                                    100%
6                                   textb
7                                    100%
8                                    100%
9                                    100%
10                                   100%
11                                  textc
12                                    80%
13                                   100%
14                                   100%
15                                   100%
16                                  textd
17                                     5%
18                                    95%
19                                   100%
20                                    35%

What I am trying to is to get a df like:

col1  col2 col3 col4 col5
texta 80   100  100  100
textb 100  100  100  100
textc 80   100  100  100
textd 5    95   100  35

data

# output dput(input$hot$data)
list(list("texta", "80%", "100%", "100%", "100%"), list(
    "textb", "100%", "100%", "100%", "100%"), list(
    "textc", "80%", "100%", "100%", "100%"), list("textd", 
    "5%", "95%", "100%", "35%"))
jogo
  • 12,469
  • 11
  • 37
  • 42
user2963882
  • 625
  • 1
  • 8
  • 19

4 Answers4

3

Try:

out <- as.data.frame(t(matrix(unlist(lapply(input$hot$data,  unlist)),
                     nrow = length(input$hot$data[[1]]))))
Zach
  • 1,103
  • 8
  • 11
  • `unlist` is recursive by default, so `unlist(lapply(input$hot$data, unlist))` could just be `unlist(input$hot$data)` – Rich Scriven Oct 13 '16 at 18:53
3
data <- list(list("texta", "80%", "100%", "100%", "100%"), list(
    "textb", "100%", "100%", "100%", "100%"), list(
    "textc", "80%", "100%", "100%", "100%"), list("textd", 
    "5%", "95%", "100%", "35%"))

data <- data.frame(t(sapply(data, unlist)))

If don't want X1, X2, ... as your column names then add:

colnames(data) <- paste0("col", 1:ncol(data))
data

Then data is the following data.frame:

    col1 col2 col3 col4 col5
 1 texta  80% 100% 100% 100%
 2 textb 100% 100% 100% 100%
 3 textc  80% 100% 100% 100%
 4 textd   5%  95% 100%  35%

If you want to remove % symbol and covert your numeric values to numbers then:

data[,-1] <- sapply(data[,-1], function(x) as.numeric(sub("%", "", x)))
Enrique Pérez Herrero
  • 3,699
  • 2
  • 32
  • 33
2

Here is my attempt. You could do this all in matrix() and wrap in as.data.frame(). No loops are needed (except if you want to convert to numeric - the second part below). Consider x to be your given list.

## create a data frame from the unlisted values with '%' removed
df <- as.data.frame(
    matrix(
        sub("%", "", unlist(x), fixed = TRUE), 
        length(x),
        byrow = TRUE, 
        dimnames = list(NULL, paste0("col", 1:lengths(x)[[1]]))
    ),
    stringsAsFactors = FALSE
)
## convert the character numbers to numeric
df[] <- lapply(df, type.convert, as.is = TRUE)
## result
df
   col1 col2 col3 col4 col5
1 texta   80  100  100  100
2 textb  100  100  100  100
3 textc   80  100  100  100
4 textd    5   95  100   35
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
1

Another attempt:

d <- setNames(data.frame(matrix(sub("%","",unlist(l)), ncol = 5, byrow = T)), 
          paste0("col", 1:5))

#   col1 col2 col3 col4 col5
#1 texta   80  100  100  100
#2 textb  100  100  100  100
#3 textc   80  100  100  100
#4 textd    5   95  100   35
989
  • 12,579
  • 5
  • 31
  • 53