列出特定的数据帧

时间:2016-10-13 17:31:29

标签: r shiny rhandsontable

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

当我打电话给我输入时:

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 个答案:

答案 0 :(得分:3)

尝试:

out <- as.data.frame(t(matrix(unlist(lapply(input$hot$data,  unlist)),
                     nrow = length(input$hot$data[[1]]))))

答案 1 :(得分: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)))

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

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)))

答案 2 :(得分: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

答案 3 :(得分: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