Is there an efficient way, without using for loops, to duplicate columns in a data frame? For example, if I have the following data frame:
Var1 Var2 1 1 0 2 2 0 3 1 1 4 2 1 5 1 2 6 2 2
And I indicate that the column Var1 should be repeated twice, and the column Var2 three times, then I would like to get the following:
Var1 Var1 Var2 Var2 Var2 1 1 1 0 0 0 2 2 2 0 0 0 3 1 1 1 1 1 4 2 2 1 1 1 5 1 1 2 2 2 6 2 2 2 2 2
Any help would be greatly appreciated!
(rep), . data.frame , make.unique .1, .2 'df2'. , sub.
rep
data.frame
make.unique
.1
.2
sub
df2 <- df1[rep(names(df1), c(2,3))] names(df2) <- sub('\\..*', '', names(df2)) df2 # Var1 Var1 Var2 Var2 Var2 #1 1 1 0 0 0 #2 2 2 0 0 0 #3 1 1 1 1 1 #4 2 2 1 1 1 #5 1 1 2 2 2 #6 2 2 2 2 2
@Frank, ,
`[.noquote`(df1,c(1,1,2,2,2))
Source: https://habr.com/ru/post/1606807/More articles:How to add horizontal lines showing facilities for all groups in ggplot2? - rR: summarize items in multiple lists of different lengths? - rgeom_abline doesn't seem to respect groups in facet_grid [ggplot2] - rhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1606805/how-can-i-slow-my-network-performance-for-ajax-requests-to-debug&usg=ALkJrhhPeCglB_YlG1X-ySs0B9HnrkbPfgMultiple sort table in boostrap table does not work for multiple tables on one page - javascriptLike (@) template in Haskell - haskellAzure AD OAuth 2.0 Invalid Resource - oauthIs there a Youtube 360 โโAPI - youtube-apipython ORM for apache cassandra - pythonRestoring bacpac file to SQL Server 2014 results in SQL72014 error - sql-serverAll Articles