我正在尝试使用 来学习 Fortran2018 gfortran。在使用指针时,我注意到似乎没有测试空指针的工具。所以我有两个问题:
更实际地说,在下面的代码片段中:我们如何在执行过程中的任何时刻找出分配给p是否“安全”?allocate(可能会想象一个更复杂的、nullify和语句序列deallocate。)
program test
implicit none
real, pointer :: p
! p = 333.333 ! Segfault, p is neither defined, nor allocated
! Since p is not defined, checking whether it's
! associated to a target gives arbitrary results:
print *, "Start: Pointer p associated? ", associated(p) ! Result: True or False
nullify(p) ! Now p is defined, but `null`
print *, "Nullyfied: …Run Code Online (Sandbox Code Playgroud) 我经常需要对宽格式的 data.table 或 data.frame 中的一对列应用一些函数或操作。例如,计算患者治疗前后的体重差异。
通常,有多对列需要应用相同的操作。例如,计算患者在治疗前后的体重、bmi、血压、白细胞计数等之间的差异。
在 R 中执行此操作的最简单的方法是什么,尤其是在使用 data.table 包时?我发现以下解决方案可行,但是当变量名称不遵循完美模式时,它们会在现实世界中产生开销。
考虑以下最小的工作示例。目标是计算 a.1 和 a.2、b.1 和 b.2、c.1 和 c.2 的差异,并将它们命名为 a.3、b.3、c.3。我特别不喜欢的是最后必须“手动”重命名列。
library(data.table)
prefixes <- c("a", "b", "c")
one.cols <- paste0(prefixes, ".1")
two.cols <- paste0(prefixes, ".2")
result.cols <- paste0(prefixes, ".3")
# Data usually read from file
DT <- data.table(id = LETTERS[1:5],
a.1 = 1:5,
b.1 = 11:15,
c.1 = 21:25,
a.2 = 6:10,
b.2 = 16:20,
c.2 = 26:30)
DT.res <- cbind(DT[,.(id)],
result = DT[,..one.cols] - DT[,..two.cols]
)
old <- grep(pattern …Run Code Online (Sandbox Code Playgroud)