小编joh*_*nny的帖子

检查唯一值并在 R data.table 中只有一个唯一值时返回它的最快方法

假设我有一个大的data.table,看起来像dt下面。

dt <- data.table(
  player_1 = c("a", "b", "b", "c"),
  player_1_age = c(10, 20, 20, 30),
  player_2 = c("b", "a", "c", "a"),
  player_2_age = c(20, 10, 30, 10)
)
# dt
#    player_1 player_1_age player_2 player_2_age
# 1:        a           10        b           20
# 2:        b           20        a           10
# 3:        b           20        c           30
# 4:        c           30        a           10
Run Code Online (Sandbox Code Playgroud)

根据dt以上内容,我想创建一个data.table具有独特玩家及其年龄的人,如下所示player_dt

# player_dt
# player  age
#      a   10
# …
Run Code Online (Sandbox Code Playgroud)

r data.table

9
推荐指数
1
解决办法
202
查看次数

在 R Shiny 中检测 selectInput 值更改为 NULL

在下面的代码中,我无法检测selectInput到 的值更改为NULL

library(shiny)
ui <- fluidPage(
  selectInput(
    inputId = "var",
    label = "Select a variable:",
    choices = c("A", "B", "C"),
    selected = NULL,
    multiple = T),
  textOutput("selected_var")
)
server <- function(input, output) {
  observeEvent(input$var, {
    showNotification("var changed")
    output$selected_var <- renderPrint(paste0("selected var: ", input$var))
    if(is.null(input$var)) {                          # I want to be able to
      showNotification("var changed to null")         # detect this action
    }
  })
}
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

如果用户选择 A 然后按退格键将其删除,我希望能够检测到该操作。

input$var您将如何检测更改为 的值NULL

r shiny shinydashboard shiny-reactivity

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

在 Ubuntu 系统上的 R 中安装钠包时出现问题

我正在尝试sodium在 Ubuntu 系统上安装一个在 R 中调用的软件包,但收到如下错误消息:

install.packages("sodium", dependencies = T)

...

* installing *source* package ‘sodium’ ...
** package ‘sodium’ successfully unpacked and MD5 sums checked
Package libsodium was not found in the pkg-config search path.
Perhaps you should add the directory containing `libsodium.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libsodium' found
Using PKG_CFLAGS=
Using PKG_LIBS=-lsodium
------------------------- ANTICONF ERROR ---------------------------
Configuration failed because libsodium was not found. Try installing:
 * deb: libsodium-dev (Debian, Ubuntu, etc)
 * rpm: libsodium-devel …
Run Code Online (Sandbox Code Playgroud)

ubuntu r libsodium sodium ubuntu-18.04

2
推荐指数
1
解决办法
3473
查看次数

如何检查 data.table 的各个行中的值是否相同

假设我有以下 data.table:

dt <- data.table(a = 1:2, b = 1:2, c = c(1, 1))

# dt
#    a b c
# 1: 1 1 1
# 2: 2 2 1
Run Code Online (Sandbox Code Playgroud)

创建第四列以d指示每行中预先存在的值都相同的最快方法是什么,以便生成的 data.table 将如下所示?

# dt
#    a b c              d
# 1: 1 1 1      identical
# 2: 2 2 1  not_identical
Run Code Online (Sandbox Code Playgroud)

我想避免使用duplicated函数并希望坚持使用identical或类似的函数,即使这意味着迭代每行中的项目。

r data.table rowwise

2
推荐指数
1
解决办法
62
查看次数

使用相同的列名对 data.table 的多个列进行子集化

假设我有以下 data.table:

dt <- data.table(a = 1:3, b = 4:6, a = 7:9)

# dt
   a b a
1: 1 4 7
2: 2 5 8
3: 3 6 9
Run Code Online (Sandbox Code Playgroud)

如何对 data.table 进行子集化以便a选择具有该名称的所有列?

下面的代码只给我第一个匹配项。

dt[, "a", with = F]

   a
1: 1
2: 2
3: 3
Run Code Online (Sandbox Code Playgroud)

r data.table

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