R 类型为'的数据子集范围;双倍';
问题已在评论中解决强> 我有一个9个元素(R 类型为'的数据子集范围;双倍';,r,double,subset,R,Double,Subset,问题已在评论中解决 我有一个9个元素(S3光谱)的列表(称为“Spec\u opt”)。其中两个元素(“data”和“freq”)的数据类型为“double”(请参阅随附的屏幕转储) 调用Spec\u opt$freq[1:5]如下所示: > Spec_opt$freq[1:5] [1] 19.65228 19.65205 19.65183 19.65160 19.65137 Spec_opt$freq[1:10] [[1]] [1] 19.65228 [[2]] [1] 19.652
S3光谱
)的列表(称为“Spec\u opt
”)。其中两个元素(“data
”和“freq
”)的数据类型为“double”
(请参阅随附的屏幕转储)
调用Spec\u opt$freq[1:5]
如下所示:
> Spec_opt$freq[1:5]
[1] 19.65228 19.65205 19.65183 19.65160 19.65137
Spec_opt$freq[1:10]
[[1]]
[1] 19.65228
[[2]]
[1] 19.65205
[[3]]
[1] 19.65183
[[4]]
[1] 19.6516
[[5]]
[1] 19.65137
我需要对这些值的范围进行子集,我已经尝试:
Spec_opt$freq <- Spec_opt$freq %>% filter(Spec_opt$freq %in% (-0.5:9.5))
这:
有人能帮我,将'double'
转换成“普通”值,或者在数据采用这种格式时帮我对其进行子集划分吗
很抱歉,我不知道如何提供数据,如果我调用dput(Spec_opt$freq[1:50])
来提供一个子集(总计n=130000个值),它只给出一个数字列表,其中问题没有“传递”,也就是说
> dput(Spec_opt$freq[1:5])
c(19.6522802209025, 19.6520531555891, 19.6518260902757, 19.6515990249622,
19.6513719596488)
整个Spec\u opt
对象非常大,无法在我的“窗口”中打印dput(Spec\u opt)
。
让我知道我是否可以把这个例子做得更好
处理某些错误的一种方法是将列表转换为数据帧,然后应用过滤器:
library(dplyr)
Spec_opt <- list(freq =c(19.6522802209025, 19.6520531555891, 19.6518260902757, 19.6515990249622, 19.6513719596488))
Spec_opt <- as.data.frame(Spec_opt) %>%
dplyr::filter(freq > -0.5, freq < 9.5)
库(dplyr)
规格选项-0.5,频率<9.5)
此外:
规格选项$freq=-.5|
Spec_opt$freq编辑:看到@GKis评论太晚了
有关列表()
中的子集/拆分向量,请参见以下示例:
Spec\u opt我有时也会看到这个错误。通常会加载另一个包,它会屏蔽dplyr
中的filter
函数。你能试试dplyr::filter
?像这样:Spec\u opt$freq%dplyr::filter(Spec\u opt$freq%in%(.5:9.5))-这会给出相同的错误消息。试试:Spec\u opt$freq[Spec_opt$freq>=-.5&Spec_opt$freq@GKi完全奏效了!-啊,这样一个愚蠢的小叮当声取代了“|”。非常感谢你看到了这个错误!@Mathilde请看,嗨,对不起,我应该说清楚,Spec_opt$freq是130000个从ca.-10到19的值。好的。把它转换成数据框解决问题了吗?数据框框架始终可以转换回列表
> dput(Spec_opt$freq[1:5])
c(19.6522802209025, 19.6520531555891, 19.6518260902757, 19.6515990249622,
19.6513719596488)
library(dplyr)
Spec_opt <- list(freq =c(19.6522802209025, 19.6520531555891, 19.6518260902757, 19.6515990249622, 19.6513719596488))
Spec_opt <- as.data.frame(Spec_opt) %>%
dplyr::filter(freq > -0.5, freq < 9.5)
Spec_opt <- list()
Spec_opt$freq <- as.double(seq(-10, 19, 0.01))
# subset data
subset1 <- Spec_opt$freq[Spec_opt$freq >= 18 & Spec_opt$freq < 18.5]
subset2 <- Spec_opt$freq[Spec_opt$freq >= 18.5 & Spec_opt$freq < 19]
# split data
split(Spec_opt$freq, cut(Spec_opt$freq, breaks = c(17, 17.5, 18, 18.5, 19)))