创建一个100数字向量,其中R中的随机值舍入为2位小数

时间:2013-07-21 12:48:26

标签: r

我需要做一个非常简单的任务,但由于我不熟悉R,我不确切知道如何做。我必须创建一个100个数字的向量,随机值从0到1,带有2个DECIMAL数字。我试过这个:

 x2 <- runif(100, 0.0, 1.0)

并且效果很好,但是这些数字有8个十进制数字,而我只需要2个。

6 个答案:

答案 0 :(得分:8)

也许还是:

(sample.int(101,size=100,replace=TRUE)-1)/100

答案 1 :(得分:5)

所以你想从集合{0,1 / 100,2 / 100,...,1}中随机抽样?然后在代码中准确写出:

hundredths <- seq(from=0, to=1, by=.01)
sample(hundredths, size=100, replace=TRUE)

答案 2 :(得分:4)

或者

x2 <- round( runif(100, -0.005, 1.0049, 2 )

答案 3 :(得分:3)

简单修复: x2&lt; - round(runif(100,0.0,1.0),digits = 2)

将轮到两个DP。

答案 4 :(得分:3)

希望这有帮助

#: import main filmy
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
#: import ListItemButton kivy.uix.listview.ListItemButton


<ScreenOne>:
    BoxLayout:
        Label:
            text: "Welcome to Random Movie.\nYou will see several couples of picture. \nLet yourself be emotional and choose one.\nAfter that application chooses you 3 random movies. "
        Button:
            text: "Start"
            on_press: root.manager.current = 'screen2'


<ScreenTwo>:
    BoxLayout:
        ImageButton:
            #cizojazycne: cizojazycne
            #id:cizojazycne
            on_press:
                root.manager.current = 'screen3'
                root.add_genre('en')
            source: "./zkouska.jpg"
            keep_ratio: False
            allow_stretch: True
        ImageButton:
            on_press:
                root.manager.current = 'screen3'
                root.add_genre('cz')
            source: "./zkouska.jpg"
            keep_ratio: False
            allow_stretch: True


<ScreenThree>:
    BoxLayout:
        ImageButton:
            on_press:
                root.manager.current = 'screen4'
                root.add_genre('35', '66', '44')
            source: "./zkouska.jpg"
            keep_ratio: False
            allow_stretch: True
        ImageButton:
            on_press:
                root.manager.current = 'screen4'
                root.add_genre('35', '66', '44')
                root.add_genre('dwadwad')
            source: "./zkouska.jpg"
            keep_ratio: False
            allow_stretch: True

<ScreenFour>:
    BoxLayout:
        ImageButton:
            on_press:
                root.manager.current = 'screen5'
                root.add_genre('1751')
            source: "./zkouska.jpg"
            keep_ratio: False
            allow_stretch: True
        ImageButton:
            on_press:
                root.manager.current = 'screen5'
                root.add_genre('4')
            source: "./zkouska.jpg"
            keep_ratio: False
            allow_stretch: True

<ScreenFive>
    BoxLayout:
        orientation: "vertical"

        Label:
            id: lbl1

        Button:
            text: "Film 1"
        Button:
            text: "Film 2"
        Button:
            text: "Film 3"
            on_press: root.press_read()
        Button:
            text: "Try again"
            on_press: root.manager.current = 'screen1'



<Filmy>:
    id: screen_manager

    screen_one: screen_one
    screen_two: screen_two
    screen_three: screen_three
    screen_four: screen_four
    screen_five: screen_five

    ScreenOne:
        id: screen_one
        name: 'screen1'
        manager: screen_manager

    ScreenTwo:
        id: screen_two
        name: 'screen2'
        manager: screen_manager

    ScreenThree:
        id: screen_three
        name: 'screen3'
        manager: screen_manager

    ScreenFour:
        id: screen_four
        name: 'screen4'
        manager: screen_manager

    ScreenFive:
        id: screen_five
        name: 'screen5'
        manager: screen_manager
  • x1 = round(runif(100,0,1), 2) :随机数
  • 100:最低值
  • 0:最大值
  • 1:四舍五入到两个十进制宫殿

答案 5 :(得分:0)

您可以使用round和runif方法生成随机数

  

Vector = round(runif(随机数,最小值,最大值),   小数位)

例如:Vector = round(runif(10,0,1,3)) //它会生成10个随机数和3个小数位