首页 > 解决方案 > 在检索文件期间将整个数据集转换为数字形式

问题描述

我正在开发 Shiny 应用程序,并希望将整个数据集转换为数字形式。我已使用此代码从本地 PC 检索文件。可以进行哪些更改以在检索时将整个数据集转换为数字形式

datami <- reactive({
file1 <- input$file
if(is.null(file1)){return()} 
read.csv(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)})

output$table <- renderPrint({
if(is.null(datami())){return ()}
str(datami())})
tabsetPanel(tabPanel("Data",div(h5("Data",style="color:red")),verbatimTextOutput("table"))```

标签: rshiny

解决方案


根据您希望如何处理小写/大写字母(如果您的数据中有它们),我们可以执行以下操作之一:

MRE:

letter_variable <- c(letters, LETTERS)

大小写字母相同的数值:

letter_variable_as_numeric1 <- as.numeric(factor(toupper(letter_variable), levels = LETTERS))

 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21
[22] 22 23 24 25 26  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16
[43] 17 18 19 20 21 22 23 24 25 26

大小写字母的不同数值:

letter_variable_as_numeric2 <- as.numeric(factor(letter_variable), levels = c(letters, LETTERS))


 [1]  1  3  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41
[22] 43 45 47 49 51  2  4  6  8 10 12 14 16 18 20 22 24 26 28 30 32
[43] 34 36 38 40 42 44 46 48 50 52

推荐阅读