Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
142 views
Welcome To Ask or Share your Answers For Others

1 Answer

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...