问题
I have this data frame where the column names are from v1 to v292. There are 17 observations. I need to iterate over the columns and replicate each column fetched 6 times.
For example:
v1 v2 v3 v4
1 3 4 6
3 4 3 1
What the output should be
x
1
3
1
3
1
3
1
3
1
3
1
3
3
4
3
4
3
4
3
4
3
4
3
4 .. and so on.
Please help. Thank you in advance.
回答1:
You could use rep
data.frame(x = unlist(rep(df, each = 6)))
Checking output with each = 2
data.frame(x = unlist(rep(df, each = 2)))
# x
#1 1
#2 3
#3 1
#4 3
#5 3
#6 4
#7 3
#8 4
#9 4
#10 3
#11 4
#12 3
#13 6
#14 1
#15 6
#16 1
来源:https://stackoverflow.com/questions/53645583/looping-through-columns-replicating-each-column-fetched-six-times