I have an empty dataframe which is my template
temp <- data.frame(matrix(ncol=3))
colnames(temp) <- c("variable", "group", "bin")
And another dataframe which has the details of these:
info <- data.frame(group_abc = c("1", "2", "2", "3", "3", "3"), bin_abc = c("0-700", "700-750", "750-800", "800-850", "850-900", "900-950"))
I want the variable name to be "group_abc", and group to have values of group_abc and bin to have values of bin_abc.
When I was trying to use the values from the dataframe, it gives me an error saying: Error in
$<-.data.frame(
tmp, group, value = c("0-699", "700-750", :
replacement has 6 rows, data has 1
Just assign (value of) the existing dataframe to the new name but change the column names which can be done in one easy step:
info <- data.frame(group_abc = c("1", "2", "2", "3", "3", "3"), bin_abc = c("0-700", "700-750", "750-800", "800-850", "850-900", "900-950"))
temp <- setNames( info, c("group", "bin"))
> temp
group bin
1 1 0-700
2 2 700-750
3 2 750-800
4 3 800-850
5 3 850-900
6 3 900-950
If you had wanted only a portion of the existing dataframe to get duplicated, you could have selected either rows or columns or both using "[" on the info
argument with the setNames call.