小编mzu*_*uba的帖子

R:表和多维数组中的维名称

由于我使用的函数需要一个表对象作为参数,我想将一个多维数组转换为一个表,但我有维度名称的问题.

如帮助文件中所指定as.table,dnn参数应包含dimnames名称.

dnn … the names to be given to the dimensions in the result (the dimnames names).
Run Code Online (Sandbox Code Playgroud)

但即使在指定时dnn,我生成的表as.table也没有维度名称.

以下代码说明了我的问题.

>test <- table(c("a","b","c","c","c"),c("1","2","3","2","2"),dnn=c("letters","numbers"))
>test 

          numbers
letters 1 2 3
      a 1 0 0
      b 0 1 0
      c 0 2 1

# this works perfectly
Run Code Online (Sandbox Code Playgroud)

现在从数组构造表时尝试相同:

>my2dimdata <- array(c(1,0,0,0,1,2,0,0,1),dim=c(3,3),
                    dimnames=list(c("a","b","c"),
                                  c("1","2","3")))
>my2dimdata

  1 2 3
a 1 0 0
b 0 1 0
c 0 2 1

# the array as expected …
Run Code Online (Sandbox Code Playgroud)

r type-conversion multidimensional-array dataframe

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

R中的多维数组乘法

我想执行一些复杂的多维数组乘法,其中我乘以数组的特定边距.

考虑这个例子,我在群体的某些边缘流行分组特征(A和B):

# setup data

random=runif(4)

group.prevalence <- aperm (array(c(random,1-random),
                  dim=c(2,2,2), 
                  dimnames=list(age=c("young","old"),
                                gender=c("male","female"),
                                group=c("A","B"))) , c(3,1,2) )

group.prevalence 
# A + B = 1
Run Code Online (Sandbox Code Playgroud)

假设我现在有一群兴趣...

population <- round(array(runif(4, min=100,max=200) %o% c(1,1*(1+random[1]),1*(1+random[1])^2), 
                          dim=c(2,2,3), dimnames=list(age=c("young","old"),
                                                      gender=c("male","female"),
                                                      year=c("year1","year2","year3"))))

population
Run Code Online (Sandbox Code Playgroud)

...我想计算"A"和"B"的流行程度.

糟糕的解决方案是在循环中填充所有内容:

# bad solution
grouped.population <- array(NA, dim=c(2,2,2,3), 
                            dimnames=list(group=c("A","B"),
                                          age=c("young","old"),
                                          gender=c("male","female"),
                                          year=c("year1","year2","year3")))

for (group in c("A","B"))
  for(gender in c("male","female"))
    for (age in c("young","old")) 
      grouped.population[group,age,gender,] <- group.prevalence[group,age,gender] * population[age,gender,]
Run Code Online (Sandbox Code Playgroud)

但我认为某种应用可能派上用场,可能是plyr的aaply,因为结果的尺寸应该保留.我试过了:

library(plyr)
aaply(population, c(1,2), function(x) x * group.prevalence)
# too many …
Run Code Online (Sandbox Code Playgroud)

r multidimensional-array

5
推荐指数
0
解决办法
459
查看次数

在R中修改传单弹出窗口

我想修改R中的传单弹出窗口的外观。

帮助文件规定,...popupOptions()功能以“传递给Javascript对象的构造基本额外的选项。”

此示例中,该style选项设置为可修改标记外观的CSS参数列表:

  addMarkers(
    lng = -118.456554, lat = 34.075,
    label = "Label w/ custom CSS style",
    labelOptions = labelOptions(noHide = T, direction = "bottom",
      style = list(
        "color" = "red",
        "font-family" = "serif",
        "font-style" = "italic",
        "box-shadow" = "3px 3px rgba(0,0,0,0.25)",
        "font-size" = "12px",
        "border-color" = "rgba(0,0,0,0.5)"
      )))
Run Code Online (Sandbox Code Playgroud)

但是,此方法似乎不适用于Popups,如以下最小工作示例所示:

if (!require("pacman")) install.packages("pacman")
pacman::p_load(leaflet, eurostat, dplyr)

map <- get_eurostat_geospatial() %>% subset(., .$NUTS_ID == "AT11")

leaflet() %>%

  addPolygons(data = map , 
              group = …
Run Code Online (Sandbox Code Playgroud)

css r leaflet r-leaflet

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

Groovy Beans标准构造函数如何处理空值?

我正在尝试使用Groovy构建一个微模拟模型,但我遇到了一个问题,我追溯到Groovy在Groovy Bean构造函数中处理null值.

简而言之,Groovy提供构造方法

new Person(nrLegs:calculationResult1, nrArms:calculationResult2)
Run Code Online (Sandbox Code Playgroud)

IllegalArgumentException如果其中一个计算结果为空,则抛出一个,这就是我认为缺失的调查数据最能代表的方式.

这对我来说似乎很奇怪:如果我定义一个double age;没有值的变量,显然它被设置为null.

double testDouble;
assert testDouble == null; // no Problem
Run Code Online (Sandbox Code Playgroud)

如果我对Groovy bean执行相同操作,则其值为0.0,例如:

class Person {
double age;
int nrLegs, nrArms;
}
Run Code Online (Sandbox Code Playgroud)

然后

Person testPerson = new Person(nrArms:calculationResult1) 
assert testPerson.age == null; // Assertion failed. testPerson.age == 0.0
Run Code Online (Sandbox Code Playgroud)

此外,我无法使用Groovy语法将属性设置为null:

Person testPerson = new Person(nrArms:calculationResult1) 
testPerson.age = null; // IllegalArgumentException
Run Code Online (Sandbox Code Playgroud)

这似乎与上面完全相同.

为什么Groovy会禁止我分配空值?

谢谢你的帮助!

编辑:作为参考,这里是整个Person类StackTrace.

null groovy javabeans

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

为什么Eclipse要求我在java代码中设置(任意)括号?

我目前正在试图弄清楚如何使用Eclipse在java中编写Escape模型.我是Escape和Eclipse的新手,自从我用java编程以来已经有一段时间了,所以请原谅这是一个愚蠢的问题.

基本上,我一直被奇怪的Eclipse错误消息所困扰.我跟踪了最后一个问题:

这有效:

public class CoordinationGame extends Scape {

.
    .
    .
    Scape lattice;

boolean test;
int test2;

{
test = true;
test2 = 3;
}

{
lattice = new Scape(new Array2DVonNeumann());
}

}
Run Code Online (Sandbox Code Playgroud)

这给出了奇怪的错误消息:

public class CoordinationGame extends Scape {

    .
.
.

Scape lattice;

boolean test;
int test2;

test = true;
test2 = 3;

lattice = new Scape(new Array2DVonNeumann());
}
Run Code Online (Sandbox Code Playgroud)

{预期的int test2Syntax error on token "lattice", VariableDeclaratorId expected after this token.

正如我所说,Java已经有一段时间了,但是IIRC,这些括号不应该被要求. …

java eclipse

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