小编Ujj*_*ari的帖子

变换单位矩阵

我有单位矩阵,可以通过生成diag(5)

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    0    0    0    0
[2,]    0    1    0    0    0
[3,]    0    0    1    0    0
[4,]    0    0    0    1    0
[5,]    0    0    0    0    1
Run Code Online (Sandbox Code Playgroud)

我想将其转换为矩阵,其中系列在 1 之后开始。例如,第一列,值 1 到 5。第二列 - 值 1 到 4。

所需输出

    [,1] [,2] [,3] [,4] [,5]
[1,]    1   0   0   0   0
[2,]    2   1   0   0   0
[3,]    3   2   1   0   0
[4,]    4   3   2   1   0
[5,]    5 …
Run Code Online (Sandbox Code Playgroud)

r matrix

9
推荐指数
2
解决办法
332
查看次数

在 dplyr 中替代 Sapply

我想知道我们如何在 dplyr 中编写类似于 sapply 的内容。在这里我计算没有。不同的价值观。我有类似的多个 sapply 语句,所以我想mutate在 dplyr 中使用编写。

distinctValues <- sapply(iris, function(var) dplyr::n_distinct(var))
Run Code Online (Sandbox Code Playgroud)

r dplyr

5
推荐指数
1
解决办法
106
查看次数

Excel IFERROR的R等效项是什么?

我正在尝试将R的IFERROR条件放在Excel的IFERROR函数之类的位置。我正在建立一个随机森林模型。为了进行微调,我使用了tuneRF功能。它有助于给出最佳的mtry参数。

#Selecting Optimal MTRY parameter
mtry <- tuneRF(dat3[, -36], dat3[,36], ntreeTry=1000, stepFactor=1.5,improve=0.01, trace=TRUE, plot=TRUE)
best.m <- mtry[mtry[, 2] == min(mtry[, 2]), 1]
Run Code Online (Sandbox Code Playgroud)

有时,如果OOB错误在不同的迭代中无法改善,则上述函数将返回错误。

if(改善>改善){时错误:缺少值,需要TRUE / FALSE。

下一步:如果上述功能正常,我在下面的代码中使用best.m的值。

tuneRF功能中没有错误-运行以下代码。

rf <-randomForest(classe~.,data=dat3, mtry=best.m, importance=TRUE,ntree=1000)
Run Code Online (Sandbox Code Playgroud)

tuneRF函数中的错误-运行以下代码。

#Train Random Forest
rf <-randomForest(classe~.,data=dat3, importance=TRUE,ntree=1000)
Run Code Online (Sandbox Code Playgroud)

谢谢您的期待!任何帮助将不胜感激。

error-handling excel r try-catch

4
推荐指数
1
解决办法
1万
查看次数

在 Shiny 中运行 Javascript

我想在服务器中运行 JS(而不是 UI)。我知道我可以使用 runjs 函数通过闪亮js 包来做到这一点,但我想知道如何通过本机闪亮包来做到这一点。我尝试通过session$sendCustomMessage()Shiny.addCustomMessageHandler( )它不起作用。

我的尝试

library(shiny)
library(shinydashboard)

jscode <- "
  window.close();
"

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    actionButton("close", "Close app"),
    tags$script(
      "Shiny.addCustomMessageHandler('closeWindow', function(data) {
       data.message
      });"
    )
  ) 
)

server = function(input, output, session) {
  observeEvent(input$close, {
    
    session$sendCustomMessage(type = "closeWindow", list(message = jscode))

  })
}

runApp(list(ui = ui, server = server), launch.browser =T)
Run Code Online (Sandbox Code Playgroud)

这适用于shinyjs

library(shiny)
library(shinydashboard)
library(shinyjs)

jscode <- "
  window.close();
"

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    shinyjs::useShinyjs(),
    actionButton("close", "Close app") …
Run Code Online (Sandbox Code Playgroud)

r shiny shinyjs

4
推荐指数
1
解决办法
1052
查看次数

CSS 中的反向查找

我想选择img不包含在 下的标签table。下面的代码应该只返回 1 的长度,因为该 HTML 只有 1 个img不在table.

注意:无法更改标记/样式。

console.log(document.querySelectorAll('img').length)
Run Code Online (Sandbox Code Playgroud)
<table align="center">
  <tbody>
    <tr>
      <td style="text-align: center;">
        <a href="https://xxx" imageanchor="1" style="margin-left: auto; margin-right: auto;">
          <img border="0" src="https://xxx" original="https://xxx" style="">
        </a>
      </td>
    </tr>
    <tr>
      <td class="tr-caption" style="text-align: center;">Double Trailing</td>
    </tr>
  </tbody>
</table>

<a href="https://xxx" imageanchor="1" style="margin-left: auto; margin-right: auto;">
  <img border="0" src="https://xxx" original="https://yyy" style="">
</a>
Run Code Online (Sandbox Code Playgroud)

html javascript css

3
推荐指数
1
解决办法
234
查看次数

R 中的自定义排序

我在 R 中有以下数据框。

# Create a data frame with duplicates
df <- data.frame(
  pagePath = c(
    "/page7",
    "/page9",
    "/page3",
    "/page9",
    "/page5",
    "/page7",
    "/page4",
    "/page9",
    "/page9",
    "/page4",
    "/page5",
    "/page7"
  ),
  country = c(
    "USA",
    "Canada",
    "UK",
    "Germany",
    "France",
    "India",
    "Australia",
    "Japan",
    "Brazil",
    "India",
    "South Africa",
    "Canada"
  )
)
Run Code Online (Sandbox Code Playgroud)

我想根据唯一 pagePath 的顺序对数据进行排序。例如,首先应出现“/page7”的所有行,然后是“/page9”的所有行,然后是“/page3”等。

r dplyr

3
推荐指数
1
解决办法
57
查看次数

将数值转换为日期时间

我有以下数值。我需要将其转换为日期时间格式。

1627074000000
Run Code Online (Sandbox Code Playgroud)

要查看更多这样的值(例如 1627077600000),您可以参考以下链接 - https://coindatadesktop.com/coins/getRelatives.php?symbol=VNDC&limit=48&modo=pre

我用谷歌搜索了一下,发现(这里)它是 GMT 格式。

<startTimeGmtMs>1627243200000</startTimeGmtMs>
<endTimeGmtMs>1627248600000</endTimeGmtMs>
Run Code Online (Sandbox Code Playgroud)

我试过这个,但返回无效的日期 -

as.POSIXct(1627243200000, origin = "1970-01-01", tz = "GMT")
Run Code Online (Sandbox Code Playgroud)

r

0
推荐指数
1
解决办法
32
查看次数

标签 统计

r ×6

dplyr ×2

css ×1

error-handling ×1

excel ×1

html ×1

javascript ×1

matrix ×1

shiny ×1

shinyjs ×1

try-catch ×1