Data Frame Modification in R

2019-09-20 00:23发布

问题:

the three lines of R code below show a vector a1,b1 and "c1" which is the unique of fields a1. I want to display a data frame of two columns where I should get every element of c1 displayed the number of times equal to the length of elements in "a1", in one column, and the corresponding ID "b1" of that letter in another column. Simply, say a data frame with column "y" in which say letter "a" from "c1" will be represented 6 times back to back(length of string a1), then "b" 6 times, then c and so on. Also corresponding to a in other column, "1" 6 times, then "2" 6 times and so on. Please help and thanks.

a1 = c("a","b","c","d","a","b")
b1 = c(1,2,3,4,1,2)
c1 = unique(a1)

New Change

a1 = c("a","b","b","d","c","e","f","a","b","c","d")
b1 = c(1,1,1,2,3,2,3,1,1,3,2)
c1 = unique(a1)

回答1:

This achieves what you ask I believe:

cbind(rep(c1, each = length(a1)),rep(b1, each = length(a1)))


回答2:

a1 and b1 have to be combined into one data.frame before the unique() function is applied. Otherwise, the vectors have different lengths.

DF <- data.frame(a1, b1)
unique(DF)[rep(1:nrow(unique(DF)), each = nrow(DF)), ]

For the first dataset

a1 = c("a","b","c","d","a","b")
b1 = c(1,2,3,4,1,2)

the result is:

    a1 b1
1    a  1
1.1  a  1
1.2  a  1
1.3  a  1
1.4  a  1
1.5  a  1
2    b  2
2.1  b  2
2.2  b  2
2.3  b  2
2.4  b  2
2.5  b  2
3    c  3
3.1  c  3
3.2  c  3
3.3  c  3
3.4  c  3
3.5  c  3
4    d  4
4.1  d  4
4.2  d  4
4.3  d  4
4.4  d  4
4.5  d  4

consisting 24 rows (4 unique values in a1 times 6 which is the length of a1)

Note that this different to user124123's answer which has 36 rows (length(b1) times length(a1) caused by rep(b1, each = length(a1)).

For the second dataset

a1 = c("a","b","b","d","c","e","f","a","b","c","d")
b1 = c(1,1,1,2,3,2,3,1,1,3,2)

the result consists of 66 rows (6 unique values times 11). (Output omitted for brevity).



标签: r dplyr unique