如何在R中将一个列表中的所有元素添加到另一个列表中的每个元素?

szqfcxe2  于 2023-09-27  发布在  其他
关注(0)|答案(2)|浏览(114)

我想得到一周中每一天的所有小时的列表,按天和小时排序。
我可以通过创建一个工作日列表和一个小时列表来获得这个列表,然后通过索引工作日列表将小时添加到一周中的每一天。我知道有更好的办法。我尝试了一个for循环,但它没有产生相同的结果。请告诉我一条更好的路。

weekdays <- c("Mon","Tue","Wed","Thu","Fri","Sat","Sun")
hours <- c("12am","1am","2am","3am","4am", "5am","6am","7am","8am","9am","10am",
           "11am","12pm","1pm","2pm","3pm","4pm","5pm","6pm","7pm","8pm","9pm",
           "10pm","11pm")

monhours <- paste(weekdays[1], hours)
tuehours <- paste(weekdays[2], hours)
wedhours <- paste(weekdays[3], hours)
thuhours <- paste(weekdays[4], hours)
frihours <- paste(weekdays[5], hours)
sathours <- paste(weekdays[6], hours)
sunhours <- paste(weekdays[7], hours)

weekdaysandhours <- c(monhours, tuehours, wedhours, thuhours, frihours, sathours, sunhours)

这就是我想要的结果:“Mon 12am”“Mon 1am”“Mon 2am”“Mon 3am”“Mon 4am”“Mon 5am”“Mon 6am”“Mon 7am”“Mon 8am”“Mon 9am”“Mon 10am”“Mon 11am”“Mon 12pm”“Mon 1pm”“Mon 2pm”“Mon 3pm”“Mon 4pm”“Mon 5pm”“Mon 6pm”“Mon 7pm”“Mon 8pm”“Mon 9pm”“Mon 10pm”“Mon 11pm”“星期二上午12点”“星期二上午1点”“星期二上午2点”“星期二上午3点”“星期二上午4点”“星期二上午5点”“星期二上午6点”“星期二上午7点”“星期二上午8点”“星期二上午9点”“星期二上午10点”“星期二上午11点”等等。

jfewjypa

jfewjypa1#

使用expand.grid,您可以:

dat <- expand.grid(
  hours = hours,
  weekdays = weekdays
)
weekdayshours <- paste(dat$weekdays, dat$hours)

weekdayshours
#>   [1] "Mon 12am" "Mon 1am"  "Mon 2am"  "Mon 3am"  "Mon 4am"  "Mon 5am" 
#>   [7] "Mon 6am"  "Mon 7am"  "Mon 8am"  "Mon 9am"  "Mon 10am" "Mon 11am"
#>  [13] "Mon 12pm" "Mon 1pm"  "Mon 2pm"  "Mon 3pm"  "Mon 4pm"  "Mon 5pm" 
#>  [19] "Mon 6pm"  "Mon 7pm"  "Mon 8pm"  "Mon 9pm"  "Mon 10pm" "Mon 11pm"
#>  [25] "Tue 12am" "Tue 1am"  "Tue 2am"  "Tue 3am"  "Tue 4am"  "Tue 5am" 
#>  [31] "Tue 6am"  "Tue 7am"  "Tue 8am"  "Tue 9am"  "Tue 10am" "Tue 11am"
#>  [37] "Tue 12pm" "Tue 1pm"  "Tue 2pm"  "Tue 3pm"  "Tue 4pm"  "Tue 5pm" 
#>  [43] "Tue 6pm"  "Tue 7pm"  "Tue 8pm"  "Tue 9pm"  "Tue 10pm" "Tue 11pm"
#>  [49] "Wed 12am" "Wed 1am"  "Wed 2am"  "Wed 3am"  "Wed 4am"  "Wed 5am" 
#>  [55] "Wed 6am"  "Wed 7am"  "Wed 8am"  "Wed 9am"  "Wed 10am" "Wed 11am"
#>  [61] "Wed 12pm" "Wed 1pm"  "Wed 2pm"  "Wed 3pm"  "Wed 4pm"  "Wed 5pm" 
#>  [67] "Wed 6pm"  "Wed 7pm"  "Wed 8pm"  "Wed 9pm"  "Wed 10pm" "Wed 11pm"
#>  [73] "Thu 12am" "Thu 1am"  "Thu 2am"  "Thu 3am"  "Thu 4am"  "Thu 5am" 
#>  [79] "Thu 6am"  "Thu 7am"  "Thu 8am"  "Thu 9am"  "Thu 10am" "Thu 11am"
#>  [85] "Thu 12pm" "Thu 1pm"  "Thu 2pm"  "Thu 3pm"  "Thu 4pm"  "Thu 5pm" 
#>  [91] "Thu 6pm"  "Thu 7pm"  "Thu 8pm"  "Thu 9pm"  "Thu 10pm" "Thu 11pm"
#>  [97] "Fri 12am" "Fri 1am"  "Fri 2am"  "Fri 3am"  "Fri 4am"  "Fri 5am" 
#> [103] "Fri 6am"  "Fri 7am"  "Fri 8am"  "Fri 9am"  "Fri 10am" "Fri 11am"
#> [109] "Fri 12pm" "Fri 1pm"  "Fri 2pm"  "Fri 3pm"  "Fri 4pm"  "Fri 5pm" 
#> [115] "Fri 6pm"  "Fri 7pm"  "Fri 8pm"  "Fri 9pm"  "Fri 10pm" "Fri 11pm"
#> [121] "Sat 12am" "Sat 1am"  "Sat 2am"  "Sat 3am"  "Sat 4am"  "Sat 5am" 
#> [127] "Sat 6am"  "Sat 7am"  "Sat 8am"  "Sat 9am"  "Sat 10am" "Sat 11am"
#> [133] "Sat 12pm" "Sat 1pm"  "Sat 2pm"  "Sat 3pm"  "Sat 4pm"  "Sat 5pm" 
#> [139] "Sat 6pm"  "Sat 7pm"  "Sat 8pm"  "Sat 9pm"  "Sat 10pm" "Sat 11pm"
#> [145] "Sun 12am" "Sun 1am"  "Sun 2am"  "Sun 3am"  "Sun 4am"  "Sun 5am" 
#> [151] "Sun 6am"  "Sun 7am"  "Sun 8am"  "Sun 9am"  "Sun 10am" "Sun 11am"
#> [157] "Sun 12pm" "Sun 1pm"  "Sun 2pm"  "Sun 3pm"  "Sun 4pm"  "Sun 5pm" 
#> [163] "Sun 6pm"  "Sun 7pm"  "Sun 8pm"  "Sun 9pm"  "Sun 10pm" "Sun 11pm"
7gcisfzg

7gcisfzg2#

使用outer方法。

res <- outer(hours, weekdays, Vectorize(\(x, y) paste(y, x))) |> as.vector()

stopifnot(all.equal(weekdaysandhours, res))

相关问题