0

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

Anoushiravan R
  • 21,622
  • 3
  • 18
  • 41
a123
  • 5
  • 2
  • It's the first time I see `0 %>% as.numeric()`. Care to explain? – Rui Barradas Nov 06 '22 at 15:11
  • 1
    Your `test2` data is problematic as the `var1_p` has 10 entries and the `var2` has six, so will not be valid. Could you please edit your question to include a valid desired output? – jpsmith Nov 06 '22 at 15:13
  • 1
    1) Ithe 2nd data.frame, `var1_p` has 10 elements but `var2` only has 6. Why? 2) I don't understand where `var3` comes from. 3) Why expand the data to 10 rows, where does this number come from? – Rui Barradas Nov 06 '22 at 15:14
  • what i get from your problem is that you want to duplicate the rows that you have (here 5 times). Check: https://stackoverflow.com/questions/8753531/repeat-rows-of-a-data-frame-n-times. You can then assign `var3` with a `mutate` call and `rep()` to get "numbering" you are looking for. – Ray Nov 06 '22 at 15:15
  • The as.numeric is probably more than redundant. But I mixed up two different approaches, so this is a leftover, sorry. – a123 Nov 06 '22 at 15:21
  • Thank you for the heads up, I now corrected the mistake in the testdata! – a123 Nov 06 '22 at 15:22
  • Ray, thank you kindly for the input! This worked like a charm and was super easy to do! – a123 Nov 06 '22 at 15:34

2 Answers2

2

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
akrun
  • 874,273
  • 37
  • 540
  • 662
1

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))
Andre Wildberg
  • 12,344
  • 3
  • 12
  • 29