Home > Blockchain >  R, How to generate additional observations denoted by numbered sequence
R, How to generate additional observations denoted by numbered sequence

Time:11-07

I'm currently a bit stuck, since I'm a bit unsure of how to even formulate my problem. What I have is a dataframe of observations with a few variables. Lets say:

test <- data.frame(var1=c("a","b"),var2=c(15,12))

Is my initial dataset. What I want to end up with is something like:

test2 <- data.frame(var1_p=c("a","a","a","a","a","b","b","b","b","b"),
                    var2=c(15,15,15,15,15,12,12,12,12,12),
                    var3=c(1,2,3,4,5,1,2,3,4,5)

However, the initial observation count and the fact, that I need the numbering to run from 0-9 makes it rather tedious to do by hand.

Does anybody have a nice alternative solution?

Thank you.

What I tried so far was:
a)

testdata$C <- 0
testdata <- for (i in testdata$Combined_Number) {add_row(testdata,C=seq(0,9))}

which results in the dataset to be empty.

b)

testdata$C <- with(testdata, ave(Combined_Number,flur, FUN = seq(0,9)))

which gives the following error code:

Error in get(as.character(FUN), mode = "function", envir = envir) :
object 'FUN' of mode 'function' was not found

CodePudding user response:

Perhaps crossing helps

library(tidyr)
crossing(df, var3 = 0:9)

-output

# A tibble: 20 × 3
   var1   var2  var3
   <chr> <dbl> <int>
 1 a        15     0
 2 a        15     1
 3 a        15     2
 4 a        15     3
 5 a        15     4
 6 a        15     5
 7 a        15     6
 8 a        15     7
 9 a        15     8
10 a        15     9
11 b        12     0
12 b        12     1
13 b        12     2
14 b        12     3
15 b        12     4
16 b        12     5
17 b        12     6
18 b        12     7
19 b        12     8
20 b        12     9

CodePudding user response:

With dplyr this is one approach

library(dplyr)

df %>% 
  group_by(var1) %>% 
  summarize(var2, var3 = 0:9, .groups="drop")
# A tibble: 20 × 3
   var1   var2  var3
   <chr> <dbl> <int>
 1 a        15     0
 2 a        15     1
 3 a        15     2
 4 a        15     3
 5 a        15     4
 6 a        15     5
 7 a        15     6
 8 a        15     7
 9 a        15     8
10 a        15     9
11 b        12     0
12 b        12     1
13 b        12     2
14 b        12     3
15 b        12     4
16 b        12     5
17 b        12     6
18 b        12     7
19 b        12     8
20 b        12     9

Data

df <- structure(list(var1 = c("a", "b"), var2 = c(15, 12)), class = "data.frame", row.names = c(NA,
-2L))
  • Related