R:获取样本并将其放入组中

时间:2015-10-22 13:07:58

标签: r

所以基本上我必须从标准正态分布中得到一个随机样本(称之为x),然后我必须对它进行排序,并将有序向量放入10组,其中第一组包含10个最低等等。现在我必须创建一个新的向量,使得在x的顺序中,y中的值是x的值所在的组编号。即,x中的前3个值是2,5和2.5的组。分别为4,则y中的前3个值为2,5,4 ... 任何人都可以对我的代码提供一些帮助,看看我哪里出错了?

x<-rnorm(100, mean=0, sd =1)
y<-(c(x))
datart<-x[order(x)]
datart
v1<-datart[1:10]
v2<-datart[11:20]
v3<-datart[21:30]
v4<-datart[31:40]
v5<-datart[41:50]
v6<-datart[51:60]
v7<-datart[61:70]
v8<-datart[71:80]
v9<-datart[81:90]
v10<-datart[91:100]
for (i in y)
{
    if (i<-v1) print(1)
    else if (i<-v2) print(2)
    else if (i<-ve) print(3)
    else if (i<-v4) print(4)
    else if (i<-v5) print(5)
    else if (i<-v6) print(6)
    else if (i<-v7) print(7)
    else if (i<-v8) print(8)
    else if (i<-v9) print(9)
    else if (i<-v10) print(10)
}

2 个答案:

答案 0 :(得分:2)

res <- cut(x, quantile(x, seq(0, 1, 0.1)), 
         labels=paste0("v", 1:10), 
         include.lowest=TRUE)

table(res)
#res
# v1  v2  v3  v4  v5  v6  v7  v8  v9 v10 
# 10  10  10  10  10  10  10  10  10  10 

?cut会将矢量分成几组。 ?quantile用于创建用于分组的分档,seq(0, 1, .1)将分布分配为十分位,如示例中所示。 labels参数允许命名bin。 include.lowest会将下限设置为&#34;大于或等于&#34;而不只是&#34;大于&#34;。

如果你想访问一个数字为输出的组,有多个选项(命名为vector,data.frame,vector subsetting),它只取决于你下一步要做什么:

我们可以创建一个包含所有组的列表:

split(x, res)
# $v1
#  [1] -1.880931 -2.030636 -1.744457 -1.488322
#  [5] -1.235983 -2.436656 -1.604551 -1.380327
#  [9] -1.418812 -1.737736
# 
# $v2
#  [1] -1.2147030 -0.8343691 -0.8357011 -1.2101051
#  [5] -1.1952688 -0.9705542 -0.8684121 -0.9311926 

数据

set.seed(777)
x <- rnorm(100) 

答案 1 :(得分:0)

这是一个单行,适应不同数量的团体或不同的团体规模。

package project3_16;
import java.util.*;

public class Project3_16 {


public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int money=0,bet=0,ReturnBet,hold=0,hold1=0,hold2=0;
String ans=("yes"),slota,slotb,slotc;
int again=0;
String[] slot1 = {"۞","☯","☠","♕","∎","☢","♨","♣","Ω","★"};

while(ans.equalsIgnoreCase("yes")){  

    System.out.println("Would you like to play the slot machine?");
    ans= scan.nextLine();

    if(ans.equalsIgnoreCase("yes")){
    System.out.println("How much moeny do you have?");
    money =scan.nextInt();
    }

    while(ans.equalsIgnoreCase("yes"))
    {
    slota =(slot1[new Random().nextInt(slot1.length)]);
    slotb =(slot1[new Random().nextInt(slot1.length)]);
    slotc =(slot1[new Random().nextInt(slot1.length)]);

    System.out.println("How much would u like to bet?");
    bet = scan.nextInt();        

    while(bet>money)
    {

        System.out.println("you do not have enough money");
        System.out.println("How much would u like to bet?");
        bet = scan.nextInt();

    }
    hold = money-bet;
    hold1=hold;

    if(slota.equals(slotb)  && slotb.equals(slotc))
    {
        System.out.println("congrats you won");
        hold= money+(bet*2);
        System.out.println("you have $"+hold1);
        System.out.println("" +slota +" " +slotb +" " +slotc);
    }

    if(!slota.equals(slotb) && !slotb.equals(slotc) && !slota.equals(slotc))
    {
        System.out.println(" you lost");
        System.out.println("you have $"+hold1);  
        System.out.println("" +slota +" " +slotb +" " +slotc);            
    }        

    if(slota.equals(slotb) && !slota.equals(slotc))
    {
        System.out.println("keep your money");
        hold1=hold1+bet;
        System.out.println("you have $"+hold1);             
        System.out.println("" +slota +" " +slotb +" " +slotc);            
    }

    if(slotb.equals(slotc) && !slota.equals(slotc))
    {
        System.out.println("keep your money");
        hold1=hold1+bet;
        System.out.println("you have $"+hold1);             
        System.out.println("" +slota +" " +slotb +" " +slotc);
    } 

    if(slotc.equals(slota) && !slotc.equals(slotb))
    {
        System.out.println("keep your money");
        hold1=hold1+bet;
        System.out.println("you have $"+hold1);             
        System.out.println("" +slota +" " +slotb +" " +slotc);            
    }
    if(hold1==0 || money==0 || hold==0 )
    {
        System.out.println("you are broke");
        ans=("no");

    }
    money=hold1;
        }
    System.out.println("play again?");
    ans =scan.nextLine();
    }     
}
}