-1

我想从列表中创建一个数据框。该列表来自一个闪亮的可动手做的输入

当我调用输入时,我得到:

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%

我想要的是得到一个像这样的df:

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

数据

# 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%"))
4

4 回答 4

3

尝试:

out <- as.data.frame(t(matrix(unlist(lapply(input$hot$data,  unlist)),
                     nrow = length(input$hot$data[[1]]))))
于 2016-10-13T18:17:14.317 回答
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)))

如果不希望X1, X2, ... 作为您的列名,请添加:

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

然后data是以下内容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%

如果要删除 % 符号并将数值转换为数字,则:

data[,-1] <- sapply(data[,-1], function(x) as.numeric(sub("%", "", x)))
于 2016-10-13T18:29:22.333 回答
2

这是我的尝试。您可以全部完成matrix()并包含as.data.frame(). 不需要循环(除非您想转换为数字 - 下面的第二部分)。考虑x成为您的给定列表。

## 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
于 2016-10-13T18:36:54.327 回答
1

另一种尝试:

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
于 2016-10-13T19:00:58.240 回答