小编use*_*954的帖子

从矢量中选择每个其他元素

假设我有一个向量:

remove <- c(17, 18, 19, 20, 24, 25, 30, 31, 44, 45).

如何选择/提取向量中的每个第二个值?像这样:17, 19, 24, 30, 44

我正在尝试使用该seq功能:seq(remove, 2)但它不太有效.

任何帮助是极大的赞赏.

r vector seq

42
推荐指数
3
解决办法
3万
查看次数

是否有R函数用于查找包含矩阵中特定元素的行?

假设我有一个4x2矩阵.

x<- matrix(seq(1:8), 4)
Run Code Online (Sandbox Code Playgroud)

这包含以下元素

1 5
2 6
3 7
4 8
Run Code Online (Sandbox Code Playgroud)

对于这个具体的例子,假设我想删除包含'2'或'7'的行(无需手动查看矩阵并删除它们).我该怎么做?

这是我想出来的东西,但它没有做我想要的.我希望它返回矩阵中包含a 2或a 的行索引7.

remove<- which(2 || 7 %in% x)
x<- x[-remove,]
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我解决这个问题吗?谢谢

r rows matrix which

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

如何根据R中的接近度将数字排序到列表中

假设我在向量中有一个数字列表.我试图想出一个脚本,将列表分成或排序成(不一定是偶数)集合,这些集合的数量相对于向量中的其他数字彼此非常接近.你可以假设向量中的数字是按升序排列的.

my_list<- c(795, 798, 1190, 1191, 2587, 2693, 2796, 3483, 3668)
Run Code Online (Sandbox Code Playgroud)

也就是说,我需要帮助提出一个脚本,它将这些数字分成几组并分配到哪里

set_1<- c(795, 798) # these 2 numbers are fairly close to each other
set_2<- c(1190, 1191) # these numbers would be another set
set_3<- c(2587, 2693, 2796) # these numbers would be another set relative to the other numbers
set_4<- c(3483, 3668)  # the last set
Run Code Online (Sandbox Code Playgroud)

非常感谢任何帮助或建议.

sorting r vector

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

如何成对循环数组?

我有一个1000x1向量(1000行和1列).我想成对获取元素(第1行和第2行,第3行和第4行,第5行和第6行等)

这是我到目前为止所拥有的

for (j in 1: ncol(total_loci)){
    for (i in 1: sample_size){
    # a pair
    genotype[i]<- paste(total_loci[i, j], total_loci[i+1,j], sep="")
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,基因型应该是含有基因型的500x1载体(500行和1列).假设我的for循环是正确的.我认为我需要跳过其他所有索引 - 所以我i应该从1开始,然后total_loci是3,5,7,9等.变量是类数据框.

arrays r matrix dataframe

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

如何检查值是否为数字?

有人可以帮我修改下面的功能,检查一个数字是否是数字?

# handy function that checks if something is numeric
check.numeric <- function(N){
  !length(grep("[^[:digit:]]", as.character(N)))
}

check.numeric(3243)
#TRUE
check.numeric("sdds")
#FALSE
check.numeric(3.14)
#FALSE
Run Code Online (Sandbox Code Playgroud)

我希望在它像3.14这样的小数时check.numeric()返回TRUE.

regex grep r numeric decimal

3
推荐指数
2
解决办法
2万
查看次数

标签 统计

r ×5

matrix ×2

vector ×2

arrays ×1

dataframe ×1

decimal ×1

grep ×1

numeric ×1

regex ×1

rows ×1

seq ×1

sorting ×1

which ×1