r - 在检索文件期间将整个数据集转换为数字形式
问题描述
我正在开发 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"))```
解决方案
根据您希望如何处理小写/大写字母(如果您的数据中有它们),我们可以执行以下操作之一:
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
推荐阅读
- c# - 以 xamarin 形式或 xamarin android 恢复 sqlite 数据库
- anime.js - 如何同时运行多个动画
- python - 如何在熊猫中将日期列拆分为单独的日、月、年列
- c++ - 从另一个向量连接两个向量
- google-apps-script - Google Sheet getValue() 为包含 Googlefinance() 数据的单元格返回 #N/A
- android - 获得重复循环无限
- assembly - 以 16 位数字为输入并在屏幕上显示
- angular - 为什么我不断收到关于我的结束标签的错误?
- node.js - Lambda 随机丢失文件,在 S3 中创建欺骗
- javascript - 在不重新加载或分页更改的情况下刷新数据表内容