【发布时间】:2020-12-26 15:36:04
【问题描述】:
我正在尝试制作一个包含 25 个不同密码的列表,以检查另一个包含 50 个密码的列表,然后返回匹配项。这是一个关于密码的大学项目。这个想法是 25 个最常用密码的列表,我希望 R 告诉我 50 个密码中的哪一个与最常见的 25 个匹配。但是我一直收到以下错误:
Error in $<-.data.frame(*tmp*, "Percent", value = character(0)) :
replacement has 0 rows, data has 25
我正在使用以下代码
makeCounts <- function(x) {
return(x=list("count"=sum(grepl(x, Final_DF$pswd, ignore.case=TRUE))))
}
#creates a local variable named tmp which is removed afterwards
printCounts <- function(ct) {
tmp <- data.frame(Term=names(ct), Count=as.numeric(unlist(ct)))
tmp$Percent <- sprintf("%3.2f%%", ((tmp$Count / nrow(Final_DF$Pswd) * 100)))
print(tmp[order(-tmp$Count),], row.names=FALSE)
}
# create top 25 mostly commonly used pswds
worst.pass <- c("password", "123456", "12345678", "qwerty", "abc123",
"monkey", "1234567", "Qwertyuiop", "123", "dragon",
"000000", "1111111", "iloveyou", "1234", "12345",
"1234567890", "1q2w3e4r5t", "ashely", "shadow", "123123",
"654321", "superman", "sunshine", "tinkle", "football")
worst.ct <- sapply(worst.pass, makeCounts, simplify=FALSE)
printCounts(worst.ct)
包含我的 50 个密码的数据包含在我的数据框 Final_DF$Pswd 中,如下所示
> Final_DF$Pswd
[1] "monkey" "iloveyou" "dragon" "jbI2pnK$xi" "password" "computer" "!qessw"
[8] "tUNh&SSm6!" "sunshine" "wYrUeWV" "superman" "samsung" "utoXGe6$" "master"
[15] "wjZC&OvXX" "0R1cNTm9sGir" "Fbuu2bs89?" "pokemon" "secret" "x&W1TjO59" "buster"
[22] "purple" "shine" "flower" "marina" "Tg%OQT$0" "SbDUV&nOX" "peanut"
[29] "angel" "?1LOEc4Zfk" "computer" "spiderman" "nothing" "$M6LgmQgv$" "orange"
[36] "knight" "american" "outback" "TfuRpt3PiZ" "air" "surf" "lEi2a$$eyz"
[43] "date" "V$683rx$p" "newcastle" "estate" "foxy" "ginger" "coffee"
[50] "legs"
当我运行printCounts(worst.ct)读取时显示错误的回溯
Error in `$<-.data.frame`(`*tmp*`, "Percent", value = character(0)) :
replacement has 0 rows, data has 25
4.
stop(sprintf(ngettext(N, "replacement has %d row, data has %d",
"replacement has %d rows, data has %d"), N, nrows), domain = NA)
3.
`$<-.data.frame`(`*tmp*`, "Percent", value = character(0))
2.
`$<-`(`*tmp*`, "Percent", value = character(0))
1.
printCounts(worst.ct)
我已经阅读了一些论坛帖子,但我不确定这是否与 NA 值有关?我是 R 新手,一直在摸索这个问题。
谁能告诉我哪里出错了?
> dput(Final_DF)
structure(list(gender = c("female", "male", "male", "female",
"female", "male", "male", "male", "male", "female", "male", "male",
"female", "female", "female", "female", "male", "female", "male",
"male", "female", "female", "female", "female", "female", "female",
"male", "female", "female", "female", "female", "female", "female",
"female", "male", "male", "female", "female", "male", "female",
"female", "male", "female", "female", "male", "male", "male",
"male", "male", "male"), age = structure(c(47L, 43L, 65L, 24L,
44L, 60L, 26L, 25L, 62L, 23L, 44L, 61L, 27L, 47L, 18L, 23L, 34L,
77L, 71L, 19L, 64L, 61L, 22L, 55L, 45L, 29L, 21L, 64L, 43L, 20L,
32L, 55L, 68L, 21L, 81L, 43L, 63L, 72L, 38L, 20L, 66L, 39L, 64L,
20L, 73L, 21L, 53L, 75L, 69L, 82L), class = c("variable", "integer"
), varname = "Age"), web_browser = structure(c(1L, 1L, 4L, 1L,
3L, 3L, 2L, 1L, 4L, 1L, 1L, 1L, 3L, 4L, 1L, 2L, 1L, 3L, 3L, 2L,
1L, 1L, 1L, 3L, 4L, 3L, 4L, 4L, 1L, 2L, 1L, 1L, 3L, 1L, 1L, 2L,
1L, 2L, 3L, 4L, 2L, 3L, 1L, 1L, 1L, 1L, 3L, 3L, 4L, 1L), .Label = c("Chrome",
"Internet Explorer", "Firefox", "Netscape"), class = c("variable",
"factor"), varname = "Browser"), Pswd = c("monkey", "iloveyou",
"dragon", "jbI2pnK$xi", "password", "computer", "!qessw", "tUNh&SSm6!",
"sunshine", "wYrUeWV", "superman", "samsung", "utoXGe6$", "master",
"wjZC&OvXX", "0R1cNTm9sGir", "Fbuu2bs89?", "pokemon", "secret",
"x&W1TjO59", "buster", "purple", "shine", "flower", "marina",
"Tg%OQT$0", "SbDUV&nOX", "peanut", "angel", "?1LOEc4Zfk", "computer",
"spiderman", "nothing", "$M6LgmQgv$", "orange", "knight", "american",
"outback", "TfuRpt3PiZ", "air", "surf", "lEi2a$$eyz", "date",
"V$683rx$p", "newcastle", "estate", "foxy", "ginger", "coffee",
"legs"), pswd_length = c(6L, 8L, 6L, 10L, 8L, 8L, 6L, 10L, 8L,
7L, 8L, 7L, 8L, 6L, 9L, 12L, 10L, 7L, 6L, 9L, 6L, 6L, 5L, 6L,
6L, 8L, 9L, 6L, 5L, 10L, 8L, 9L, 7L, 10L, 6L, 6L, 8L, 7L, 10L,
3L, 4L, 10L, 4L, 9L, 9L, 6L, 4L, 6L, 6L, 4L), last.num = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 9, NA, NA, NA, NA, NA, 0, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA)), row.names = c(NA, -50L), class = "data.frame")
【问题讨论】:
-
你应该使用
dput提供Final_DF,阅读:stackoverflow.com/a/5963610/6574038 -
可能的重复对象,
[r] replacement has 0 rows。 -
应该
nrow(Final_DF$Pswd)而不是nrow(Final_DF)?如果它是一个简单的列,那么它保证返回NULL。 -
顺便说一句,让你的函数依赖一个既没有在函数内部定义也没有传递给函数的外部变量是一个坏主意:它破坏了可重复性,并且会使故障排除变得困难。一个快速的选择是向
printCounts(ct, finaldf)添加一个参数并使用printCounts(worst.ct, Final_DF)调用它。 -
@r2evans 的不同之处在于它打印了术语、计数和百分比,但它无法识别匹配项,所有计数均显示为 0
标签: r