循环中的尺寸错误-R

时间:2018-08-11 10:05:21

标签: r loops

这可能是该网站上最愚蠢的问题之一(而且我知道还有另一个问题可以解决相同的问题,但是对我没有帮助)。 因此,我有274行矩阵,并创建了一个循环,在每个数字旁边分配一个分数。

  scorec2.2=function(x)
{
  x$score=NA
  for(i in 1:274)
  {
    if (x[i,"time to sleep"]<= 15){
      x[i,"score"]=0
    }
    if (between(x[i,"time to sleep"], 16, 30 )){ #PS : I tried with the simple x<15 & x=>30 and it didn't work either 
      x[i,"score"]=1
    }
    if (between(x[i,"time to sleep"], 31, 60 )){
      x[i,"score"]=2
    }
    if (x[i,"time to sleep"]> 60){
      x[i,"score"]=3
    }
  }
  print(x[order(x$score),])
}

scorec2.2(c2.2)

,我收到此错误消息:

Error in x[i, "time to sleep"] : incorrect number of dimensions
In addition: Warning message:
In x$score = NA : Coercing LHS to a list

并且如果我尝试将矩阵转换为data.frame,则会收到其他错误消息:

Error in if (x[i, "time to sleep"] <= 15) { : 
  missing value where TRUE/FALSE needed

我已经重新阅读了30分钟以上的简单代码,但是我没有纠正应该纠正的内容。 最糟糕的是,我对具有不同评分系统的另一个矩阵使用了相同的代码(因此我只是更改了数字),并且一切正常。 有任何想法吗 ? 谢谢!

这是我的矩阵:

structure(c(30, 35, 10, 60, 10, 10, 30, 10, 30, 20, 5, 10, 30, 
20, 10, 20, 10, 2, 15, 10, 30, 10, 10, 0, 5, 10, 10, 10, 60, 
NA, 0, 1, 15, 15, 2, NA, 45, 60, 15, 5, 1, 45, NA, 30, 10, 2, 
15, 15, 3, 20, 15, 2, 15, 15, 15, 10, 30, 15, 15, 30, 5, 20, 
10, 20, 1, 25, 20, 30, 15, 20, 40, 120, 5, 30, 5, 5, 20, 10, 
30, 10, 40, 45, 10, 0, 30, 60, 5, 30, 60, 20, 60, 30, 15, NA, 
5, 15, 5, 10, 15, 60, 5, 25, 10, 5, 30, 15, 20, 20, 20, 5, 5, 
10, 10, 5, 30, 30, 20, 20, 20, 10, 45, 60, 10, 30, 10, 15, 30, 
10, 30, 5, 30, 2, 60, 10, 60, NA, 5, 10, 40, 45, 5, 5, 10, 5, 
10, 30, 5, 30, 10, 10, 15, 40, 10, 30, 20, 5, 60, 5, 60, 10, 
NA, 10, 10, 30, 2, 20, 10, 10, 30, 60, 20, 15, 5, 10, 10, 35, 
10, 10, 10, 30, 1, 30, 10, NA, 5, 5, 10, 10, 8, 10, 3, 5, 45, 
10, 30, 60, 15, 75, 40, 15, 20, 30, 90, 5, 10, 5, 7, 0.5, 40, 
60, 15, 30, 15, 60, 5, 15, 5, 30, 20, 2, 20, 15, 30, 10, 60, 
5, 0, 10, 10, 10, 30, 20, 30, 5, 30, 10, 60, 7, 5, 1, 60, 30, 
35, NA, 10, 5, 20, 10, 30, 30, 30, 15, 25, 15, 30, 10, 25, 5, 
10, 10, 30, 1, 10, 60, 23, 35, 60, 3, NA, 20, 30, 2, 30, 45), .Dim = c(274L, 
1L), .Dimnames = list(NULL, "time to sleep"))

应该看起来像这样

       time to sleep
  [1,]          30.0
  [2,]          35.0
  [3,]          10.0
  [4,]          60.0
  [5,]          10.0
  [6,]          10.0
  [7,]          30.0
  [8,]          10.0
  [9,]          30.0
 [10,]          20.0
 [11,]           5.0
 [12,]          10.0
 [13,]          30.0
 [14,]          20.0
 [15,]          10.0
 [16,]          20.0
 [17,]          10.0
 [18,]           2.0
 [19,]          15.0
 [20,]          10.0
 [21,]          30.0
 [22,]          10.0
 [23,]          10.0
 [24,]           0.0
 [25,]           5.0
 [26,]          10.0
 [27,]          10.0
 [28,]          10.0
 [29,]          60.0
 [30,]            NA
 [31,]           0.0
 [32,]           1.0
 [33,]          15.0
 [34,]          15.0
 [35,]           2.0
 [36,]            NA
 [37,]          45.0
 [38,]          60.0
 [39,]          15.0
 [40,]           5.0
 [41,]           1.0
 [42,]          45.0
 [43,]            NA
 [44,]          30.0
 [45,]          10.0
 [46,]           2.0
 [47,]          15.0
 [48,]          15.0
 [49,]           3.0
 [50,]          20.0
 [51,]          15.0
 [52,]           2.0
 [53,]          15.0
 [54,]          15.0
 [55,]          15.0
 [56,]          10.0
 [57,]          30.0
 [58,]          15.0
 [59,]          15.0
 [60,]          30.0
 [61,]           5.0
 [62,]          20.0
 [63,]          10.0
 [64,]          20.0
 [65,]           1.0
 [66,]          25.0
 [67,]          20.0
 [68,]          30.0
 [69,]          15.0
 [70,]          20.0
 [71,]          40.0
 [72,]         120.0
 [73,]           5.0
 [74,]          30.0
 [75,]           5.0
 [76,]           5.0
 [77,]          20.0
 [78,]          10.0
 [79,]          30.0
 [80,]          10.0
 [81,]          40.0
 [82,]          45.0
 [83,]          10.0
 [84,]           0.0
 [85,]          30.0
 [86,]          60.0
 [87,]           5.0
 [88,]          30.0
 [89,]          60.0
 [90,]          20.0
 [91,]          60.0
 [92,]          30.0
 [93,]          15.0
 [94,]            NA
 [95,]           5.0
 [96,]          15.0
 [97,]           5.0
 [98,]          10.0
 [99,]          15.0
[100,]          60.0
[101,]           5.0
[102,]          25.0
[103,]          10.0
[104,]           5.0
[105,]          30.0
[106,]          15.0
[107,]          20.0
[108,]          20.0
[109,]          20.0
[110,]           5.0
[111,]           5.0
[112,]          10.0
[113,]          10.0
[114,]           5.0
[115,]          30.0
[116,]          30.0
[117,]          20.0
[118,]          20.0
[119,]          20.0
[120,]          10.0
[121,]          45.0
[122,]          60.0
[123,]          10.0
[124,]          30.0
[125,]          10.0
[126,]          15.0
[127,]          30.0
[128,]          10.0
[129,]          30.0
[130,]           5.0
[131,]          30.0
[132,]           2.0
[133,]          60.0
[134,]          10.0
[135,]          60.0
[136,]            NA
[137,]           5.0
[138,]          10.0
[139,]          40.0
[140,]          45.0
[141,]           5.0
[142,]           5.0
[143,]          10.0
[144,]           5.0
[145,]          10.0
[146,]          30.0
[147,]           5.0
[148,]          30.0
[149,]          10.0
[150,]          10.0
[151,]          15.0
[152,]          40.0
[153,]          10.0
[154,]          30.0
[155,]          20.0
[156,]           5.0
[157,]          60.0
[158,]           5.0
[159,]          60.0
[160,]          10.0
[161,]            NA
[162,]          10.0
[163,]          10.0
[164,]          30.0
[165,]           2.0
[166,]          20.0
[167,]          10.0
[168,]          10.0
[169,]          30.0
[170,]          60.0
[171,]          20.0
[172,]          15.0
[173,]           5.0
[174,]          10.0
[175,]          10.0
[176,]          35.0
[177,]          10.0
[178,]          10.0
[179,]          10.0
[180,]          30.0
[181,]           1.0
[182,]          30.0
[183,]          10.0
[184,]            NA
[185,]           5.0
[186,]           5.0
[187,]          10.0
[188,]          10.0
[189,]           8.0
[190,]          10.0
[191,]           3.0
[192,]           5.0
[193,]          45.0
[194,]          10.0
[195,]          30.0
[196,]          60.0
[197,]          15.0
[198,]          75.0
[199,]          40.0
[200,]          15.0
[201,]          20.0
[202,]          30.0
[203,]          90.0
[204,]           5.0
[205,]          10.0
[206,]           5.0
[207,]           7.0
[208,]           0.5
[209,]          40.0
[210,]          60.0
[211,]          15.0
[212,]          30.0
[213,]          15.0
[214,]          60.0
[215,]           5.0
[216,]          15.0
[217,]           5.0
[218,]          30.0
[219,]          20.0
[220,]           2.0
[221,]          20.0
[222,]          15.0
[223,]          30.0
[224,]          10.0
[225,]          60.0
[226,]           5.0
[227,]           0.0
[228,]          10.0
[229,]          10.0
[230,]          10.0
[231,]          30.0
[232,]          20.0
[233,]          30.0
[234,]           5.0
[235,]          30.0
[236,]          10.0
[237,]          60.0
[238,]           7.0
[239,]           5.0
[240,]           1.0
[241,]          60.0
[242,]          30.0
[243,]          35.0
[244,]            NA
[245,]          10.0
[246,]           5.0
[247,]          20.0
[248,]          10.0
[249,]          30.0
[250,]          30.0
[251,]          30.0
[252,]          15.0
[253,]          25.0
[254,]          15.0
[255,]          30.0
[256,]          10.0
[257,]          25.0
[258,]           5.0
[259,]          10.0
[260,]          10.0
[261,]          30.0
[262,]           1.0
[263,]          10.0
[264,]          60.0
[265,]          23.0
[266,]          35.0
[267,]          60.0
[268,]           3.0
[269,]            NA
[270,]          20.0
[271,]          30.0
[272,]           2.0
[273,]          30.0
[274,]          45.0

2 个答案:

答案 0 :(得分:0)

您的代码中存在一些问题,但这应该可以解决

scorec2.2 = function(x) {

  # use cbind and `dimnames<-`
  x <- cbind(x, NA)
  dimnames(x) <- list(NULL, c("time to sleep", "score"))

  for (i in 1:274)

    if (is.na(x[i, "time to sleep"])) { # this handles NAs
      x[i, "score"] <- NA
    } else {
      if (x[i, "time to sleep"] <= 15) {
        x[i, "score"] <- 0
      }
      if (between(x[i, "time to sleep"], 16, 30)) {
        x[i, "score"] <- 1
      }
      if (between(x[i, "time to sleep"], 31, 60)) {
        x[i, "score"] <- 2
      }
      if (x[i, "time to sleep"] > 60) {
        x[i, "score"] <- 3
      }
    }
  print(x[order(x[, "score"]), ]) # `$` doesn't work with matrices
}

scorec2.2(c2.2)

#       time to sleep score
#  [1,]          10.0     0
#  [2,]          10.0     0
#  [3,]          10.0     0
#  [4,]          10.0     0
#  [5,]           5.0     0
#  [6,]          10.0     0
#  [7,]          10.0     0
#  [8,]          10.0     0
#  [9,]           2.0     0
# [10,]          15.0     0
# ...

答案 1 :(得分:0)

整洁的版本,因为您已经使用dplyr::between

df %>% 
    as_tibble() %>% 
    select(time.to.sleep = `time to sleep`) %>%
    mutate(
        score = case_when(
            time.to.sleep <= 15 ~ NA_real_,
            between(time.to.sleep , 16, 30) ~ 1,
            between(time.to.sleep, 31, 60) ~ 2,
            TRUE ~ 3
        )
    )