我有一个数字向量:
v <- seq(1, 50, .5)
如果我想将一个值附近的所有三个相邻的三个值子集化:25。
24 24.5 25
24.5 25 25.5
25 25.5 26
有什么方便的方法吗?
答案 0 :(得分:3)
您可以使用绝对差异:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/my.Base"
/>
答案 1 :(得分:2)
这里是一个选项,可以将值25周围的子集进行子集处理,然后将其转换为每个包含3个元素的limits = c(1,2,5,6,7,6)
mysum = 10
set.seed(42)
ans = setNames(limits, paste0("x", 1:length(limits)))
while(sum(ans) > mysum){
ind = sample(which(ans > 1), 1)
ans[ind] = ans[ind] - 1
}
ans
#x1 x2 x3 x4 x5 x6
# 1 1 4 2 1 1
sum(ans)
#[1] 10
matrix