r - Replicate values of column along rows -
given following data frame:
df1 <- data.table( v1=c(0,0,0,0),v2=c(0,0,0,0),v3=c(0,2,0,2)) df1 v1 v2 v3 1: 0 0 0 2: 0 0 2 3: 0 0 0 4: 0 0 2
i seek replicate values of v3 along entire row, that:
df2 v1 v2 v3 1: 0 0 0 2: 2 2 2 3: 0 0 0 4: 2 2 2
how can achieve this?
many in advance.
you use base-r syntax:
# overwrite df1[] <- df1$v3 # make new table df2 <- copy(df1) df2[] <- df1$v3
i think data.table-ish way modify many columns set
:
# overwrite (j in setdiff(names(df1),"v3")) set(df1, j = j, value = df1$v3) # make new table -- simple extension
finally, there several other ideas @akrun, @davidarenburg , @veerendragadekar:
# overwrite df1[, (1:ncol(df1)) := v3] # david/veerendra # make new table df2 <- setdt(rep(list(df1$v3), ncol(df1))) # akrun df2 <- df1[, rep("v3",ncol(df1)), = false] # david
Comments
Post a Comment