简体   繁体   中英

Nested For Loop in R to iterate between columns

I'm trying to generate a dataset in which I keep the changing the first column every three rows later and as I change the first column, the second and third column change with it as well. Example given below. I'm a little confused as to how I can achieve this with the nested for loop.

df = NULL 
   for (CDRID in 1:3) 
{ 
for (STARTDATE in 20200517:20200519) 
{ 
for (PRIIDENTITY in 4:6) 
{
    df1 = rbind(df, data.frame  ( "CDR_ID"=CDRID, "START_DATE"=STARTDATE, "PRI_IDENTITY"=PRIIDENTITY)) }}}

预期数据集

df <- data.frame(ID=1:3, START_DATE=20200517:20200519, PRI_IDENTITY=4:6)
df[rep(seq_len(nrow(df)), each=3),]
#     ID START_DATE PRI_IDENTITY
# 1    1   20200517            4
# 1.1  1   20200517            4
# 1.2  1   20200517            4
# 2    2   20200518            5
# 2.1  2   20200518            5
# 2.2  2   20200518            5
# 3    3   20200519            6
# 3.1  3   20200519            6
# 3.2  3   20200519            6

A thought: 20200517:20200519 , where I'm making an assumption that these are intended to be dates. This is fragile in that it doesn't know about wrapping days between months. In R, it might be better to use proper Date objects. With that, try:

> df <- data.frame(ID=1:3, START_DATE=seq.Date(as.Date("2020-05-17"), as.Date("2020-05-19"), by="days"), PRI_IDENTITY=4:6)
Browse[2]> df[rep(seq_len(nrow(df)), each=3),]
    ID START_DATE PRI_IDENTITY
1    1 2020-05-17            4
1.1  1 2020-05-17            4
1.2  1 2020-05-17            4
2    2 2020-05-18            5
2.1  2 2020-05-18            5
2.2  2 2020-05-18            5
3    3 2020-05-19            6
3.1  3 2020-05-19            6
3.2  3 2020-05-19            6

As akrun suggested, if you are using (or amenable to using) packages from the tidyverse, then instead of the rep(seq_len(... part, you can do

# df <- data.frame(...)
tidyr::uncount(df, 3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM