R语言中,生成10000个标准正态分布的随机数,分别计算大于1.96,小于-1.96和-1.96到1.96之间的数各有多少个.

Python019

R语言中,生成10000个标准正态分布的随机数,分别计算大于1.96,小于-1.96和-1.96到1.96之间的数各有多少个.,第1张

n<-1.96 这是R的bug,你想数n小于-1.96的个数, 但此时R会认为你把1.96赋值给n

这块你要用length(n[n<(-1.96)])

length(n[abs(n)>1.96]) 这个是没问题的,但是你前一步已经让n=1.96了,所以这里只能是0

#这里我假设你的数据是test,是一个框架数据

dimoftest <- dim(test) # 获取数据维数

factoftest <- c() #定义factor数组

k <- 2 #定义你的那个变量的列k,你自己设定

#循环找出k列大于k-1列10%的序号,并将factor数组设为1,否则为0

for(i in 1:dimoftest){

if((test[i,k]- test[i,k-1])/test[i,k-1] >0.1) factoftest[i] = 1

else factoftest[i] = 2

}

factoftest #显示数组

#创建一个factor因子,标记为1,2,文本也设为1,2

newfactor <- factor(factoftest,levels = c(1,2), labels = c("1","2"))

#合并到数据中

test <- data.frame(test, newfactor)

#查看

test

不明白的话继续问

which语句

Description

Give the TRUE indices of a logical object, allowing for array indices.

Usage

which(x, arr.ind = FALSE, useNames = TRUE)

arrayInd(ind, .dim, .dimnames = NULL, useNames = FALSE)