R中的计数向量

R中的计数向量,r,R,这是我的桌子 读书0 1 2 3 4 5 6 7 8 9 频率2 4 8 4 5 2 4 2 1 例如,频率是读过x本书的学生人数。上周有8名学生读了3本书 用 > nTimes <- c(0:9) > frequency <-c(2,4,4,8,4,5,2,4,2,1) > xTable <- rbind(nTimes,frequency) >nTimes frequency xTable您可以通过使用for循环迭代实现: booksRead <-

这是我的桌子

读书0 1 2 3 4 5 6 7 8 9

频率2 4 8 4 5 2 4 2 1

例如,频率是读过x本书的学生人数。上周有8名学生读了3本书

> nTimes <- c(0:9)
> frequency <-c(2,4,4,8,4,5,2,4,2,1)
> xTable <- rbind(nTimes,frequency)

>nTimes frequency xTable您可以通过使用for循环迭代实现:

booksRead <- 0:9
frequency <- c(2, 4, 4, 8, 4, 5, 2, 4, 2, 1)

length(booksRead) == length(frequency) #check: TRUE required

Counts <- c() #initialize by empty vector
for(i in 1:length(booksRead)) {
  Counts <- c(Counts, rep(booksRead[i], frequency[i]))
} #update iteratively

Counts

## [1] 0 0 1 1 1 1 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 5 5 5 5 5 6 6 7 7 7
## [33] 7 8 8 9
booksRead使用
rep

数据

booksRead = c( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
frequency= c(2, 4, 4, 8, 4, 5, 2, 4, 2, 1)
命令

vectorOfCounts = rep(x = booksRead,times = frequency)
vectorOfCounts
# [1] 0 0 1 1 1 1 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 5 5 5 5 5 6 6 7 7 7 7 8 8 9
rep(时间、频率)
vectorOfCounts = rep(x = booksRead,times = frequency)
vectorOfCounts
# [1] 0 0 1 1 1 1 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 5 5 5 5 5 6 6 7 7 7 7 8 8 9