我似乎无法找到正确的方式来索引一个矢量的平均值

问题描述 投票:0回答:1

我目前的工作是需要我来索引一个矢量的平均到一个新的载体家庭作业的问题。在选择前面的向量的教练要我们仅选择+元素的均值 - 2上述意思。请参阅下面的问题。

从rnorm.vector,仅选择与值的平均值的±2标准偏差中的元素。分配这个新的载体中以可变rnorm.select.vector并显示向量。

我一直在使用[]和其他索引规定 - 试图

// Normal distribution of 30 numbers with a mean of 25 and Standard Deviation of 2.5

rnorm.vector <- rnorm(30, mean = 25, sd = 2.5)

// logical vector 

rnorm.logical.vector <- (rnorm.vector >= 25)
rnorm.logical.vector

// +- 2.5 of standard Deviation

rnorm.select.vector <- 

我似乎无法得到正确的结果没有错误

r indexing
1个回答
0
投票

因为我喜欢猫了很多我试图就如何使用逻辑矢量选择从现有的矢量所需的元素一个小例子(这可能是猫的名字,随机NUMER等)

# i got a list of my cats name
my_cats_names <- c("Boby", "Tara", "Petzi", "Felix", "Mauzi", "Schnurrli")
# and a coresponding list with my cats weight
my_cats_weight <- c(8, 4, 7, 5, 4, 5)
# so Boby has 8kg, Tara 7 and so on

# During last night someone has stolen my ham, and im suspecting one of my cats to be the thief. 
# Since it was a big ham im pretty sure it needs a cat of at least 6kg to steal and eat it,
# so i want to select all my cats that are 6kg or more from my list. 

which_one_is_over_6_kg_logical_vector <- my_cats_weight >= 6
which_one_is_over_6_kg_logical_vector
# > which_one_is_over_6_kg_logical_vector
# [1]  TRUE FALSE  TRUE FALSE FALSE FALSE
# This result tells me, that the first one in the list is over 6kg, the second one not the 3rd one is over and so on

# Now i can use the logical vector to select only those elements from my list, that fullfill my constraint (beeing over 6kg)
my_cats_over_6kg <- my_cats_names[which_one_is_over_6_kg_logical_vector]
my_cats_over_6kg
# > my_cats_over_6kg
# [1] "Boby"  "Petzi"
# im pretty sure it was Boby or Petzi

# So by creating a logical vector we can select items from an existing vector if they fullfill our constraint

相反catnames的载体,你得到了,你可以计算出平均值和标准偏差数的载体。而不是选择在猫下面6千克意味着你whant号(your_vector) - 2 * SD(your_vector)和数字高于平均(your_vector)+ 2×SD(your_vector)。

我希望所有的猫会有点帮助,而不是造成额外的混乱:)

© www.soinside.com 2019 - 2024. All rights reserved.