我有以下数据和代码来舍入此data.table的选定列:
> dput(mydf)
structure(list(vnum1 = c(0.590165705411504, -1.39939534199836,
0.720226053660755, -0.253198380120377, -0.783366825121657), vnum2 = c(0.706508400384337,
0.526770398486406, 0.863136084517464, 0.838245498016477, 0.556775856064633
), vch1 = structure(c(2L, 4L, 1L, 3L, 3L), .Label = c("A", "B",
"C", "E"), class = "factor")), .Names = c("vnum1", "vnum2", "vch1"
), row.names = c(NA, -5L), class = c("data.table", "data.frame"
))
> mydf[,round(.SD,1),]
Error in Math.data.frame(list(vnum1 = c(0.590165705411504, -1.39939534199836, :
non-numeric variable in data frame: vch1
> cbind(mydf[,3,with=F], mydf[,1:2,with=F][,round(.SD,1),])
vch1 vnum1 vnum2
1: B 0.6 0.7
2: E -1.4 0.5
3: A …Run Code Online (Sandbox Code Playgroud) 我使用以下命令生成带抖动的散点图:
ddf = data.frame(NUMS = rnorm(500), GRP = sample(LETTERS[1:5],500,replace=T))
library(lattice)
stripplot(NUMS~GRP,data=ddf, jitter.data=T)
Run Code Online (Sandbox Code Playgroud)
我想在这些点上添加箱图(每组一个).我尝试搜索,但我无法找到代码绘制所有点(而不仅仅是异常值)和抖动.我该怎么解决这个问题.谢谢你的帮助.
我有一个数据框,我希望在保留原始结构(列)的同时删除所有行.
ddf
vint1 vint2 vfac1 vfac2
1 9 10 1 3
2 9 6 3 4
3 6 2 2 2
4 10 6 2 4
5 7 12 3 2
>
>
>
> dput(ddf)
structure(list(vint1 = c(9L, 9L, 6L, 10L, 7L), vint2 = c(10L,
6L, 2L, 6L, 12L), vfac1 = structure(c(1L, 3L, 2L, 2L, 3L), .Label = c("1",
"2", "3"), class = "factor"), vfac2 = structure(c(2L, 3L, 1L,
3L, 1L), .Label = c("2", "3", "4"), class = "factor")), …Run Code Online (Sandbox Code Playgroud) 我有以下数据帧和向量:
ddf = data.frame(a=rep(1,10), b=rep(2,10))
xx = c("c", "d", "e", "f")
Run Code Online (Sandbox Code Playgroud)
如何使用xx中的项目命名的新空列?
我试过以下但它不起作用:
ddf = cbind(ddf, data.frame(xx))
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 10, 4
Run Code Online (Sandbox Code Playgroud)
以下也不起作用:
for(i in 1:length(xx)){
ddf$(xx[i]) = ""
}
Error: unexpected '(' in:
"for(i in 1:length(xx)){
ddf$("
}
Error: unexpected '}' in "}"
Run Code Online (Sandbox Code Playgroud) 我使用以下代码与glmnet:
> library(glmnet)
> fit = glmnet(as.matrix(mtcars[-1]), mtcars[,1])
> plot(fit, xvar='lambda')
Run Code Online (Sandbox Code Playgroud)

但是,我想打印最好的Lambda系数,就像在岭回归中一样.我看到以下适合的结构:
> str(fit)
List of 12
$ a0 : Named num [1:79] 20.1 21.6 23.2 24.7 26 ...
..- attr(*, "names")= chr [1:79] "s0" "s1" "s2" "s3" ...
$ beta :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
.. ..@ i : int [1:561] 0 4 0 4 0 4 0 4 0 4 ...
.. ..@ p : int [1:80] 0 0 2 4 6 8 10 12 …Run Code Online (Sandbox Code Playgroud) 我有一个16行12列的矩阵M,我想把它分成16个矩阵的数组,每个矩阵有4行3列.我可以手动完成:
M = matrix(sample(0:127,16*12,replace=TRUE), c(16,12))
ma1 = M[1:4,1:3]
ma2 = M[1:4,4:6]
ma3 = M[1:4,7:9]
ma4 = M[1:4,10:12]
ma5 = M[5:8,1:3]
ma6 = M[5:8,4:6]
.....
Run Code Online (Sandbox Code Playgroud)
但是如何创建一个通用函数matsplitter(M,r,c),它将M分成矩阵数组,每个矩阵都有r行和c列?
谢谢你的帮助.
我有以下简单的示例数据,我想在地图上绘制渐变颜色,对应于给定国家/地区的值.
ddf = read.table(text="
country value
USA 10
UK 30
Sweden 50
Japan 70
China 90
Germany 100
France 80
Italy 60
Nepal 40
Nigeria 20
", header=T)
Run Code Online (Sandbox Code Playgroud)
在谷歌搜索,我发现了几个网站.但是,我正在寻找小而清晰的代码,并且最好是快速的(我发现ggplot方法相对较慢).世界地图的分辨率不必高.
我试过以下代码:
library(maptools)
data(wrld_simpl)
plot(wrld_simpl)
Run Code Online (Sandbox Code Playgroud)
具体国家可以如下所示着色:使用[R]地图包 - 在世界地图上的特定国家着色 使用命令:
plot(wrld_simpl, col = c(gray(.80), "red")[grepl("^U", wrld_simpl@data$NAME) + 1])
Run Code Online (Sandbox Code Playgroud)
但是如何以渐变的颜色获得具有上述数据的地图.谢谢你的帮助.
我有以下代码比较na.omit和complete.cases:
> mydf
AA BB
1 2 2
2 NA 5
3 6 8
4 5 NA
5 9 6
6 NA 1
>
>
> na.omit(mydf)
AA BB
1 2 2
3 6 8
5 9 6
>
> mydf[complete.cases(mydf),]
AA BB
1 2 2
3 6 8
5 9 6
>
> str(na.omit(mydf))
'data.frame': 3 obs. of 2 variables:
$ AA: int 2 6 9
$ BB: int 2 8 6
- attr(*, "na.action")=Class 'omit' Named int [1:3] …Run Code Online (Sandbox Code Playgroud) 我有以下data.table,我不能使用dput命令的输出来重新创建它:
> ddt
Unit Anything index new
1: A 3.4 1 1
2: A 6.9 2 1
3: A1 1.1 1 2
4: A1 2.2 2 2
5: B 2.0 1 3
6: B 3.0 2 3
>
>
> str(ddt)
Classes ‘data.table’ and 'data.frame': 6 obs. of 4 variables:
$ Unit : Factor w/ 3 levels "A","A1","B": 1 1 2 2 3 3
$ Anything: num 3.4 6.9 1.1 2.2 2 3
$ index : num 1 2 1 …Run Code Online (Sandbox Code Playgroud) pip在python中安装软件包时,它会下载并安装一个whl文件,例如pyqt5的文件:
https://files.pythonhosted.org/packages/d4/bf/d884da8e2f7096d201c891d515eb6813a8e85df5eb6f5e12e867bf1d831c/PyQt5-5.11.3-5.11.2-cp35.cp36.cp37.cp38-abi3-manylinux1_x86_64.whl
Run Code Online (Sandbox Code Playgroud)
安装后,此文件是否仍保留在光盘上并占用空间?
如果是这样,是否可以将其删除以释放磁盘空间?
此外,在文档中的任何地方都有关于此的链接吗?
r ×9
data.table ×2
choropleth ×1
coefficients ×1
dictionary ×1
glmnet ×1
lambda ×1
lattice ×1
matrix ×1
pip ×1
plot ×1
python ×1
regression ×1