Beti Endaryati
Beti Endaryati

Reputation: 35

get value from matrix that have same value in other column in R

I have matrix that contains 2 columns. First column name is fuzzified and the second is relasifuzzi.

fuzzified  relasifuzzi
        1            2
        2            3
        3            5
        5            9
        9            8
        9           10
        9            7 

I want to make a group like this:

fuzzified  relasifuzzi
        1            2
        2            3
        3            5
        5            9
        9            8, 10, 7

How can I get like this in R?

Upvotes: 0

Views: 59

Answers (2)

Josh W.
Josh W.

Reputation: 1123

Assuming your data is in a data.frame called "df", try:

library("dplyr")

df %>% group_by(fuzzified) %>% 
   summarize(relasifuzzi = paste(relasifuzzi, collapse = ", "))

or:

library("plyr")

ddply(df, .(fuzzified), summarize, relasifuzzi = paste(relasifuzzi, collapse = ", "))

Upvotes: 1

user227710
user227710

Reputation: 3194

    library(data.table)
    setDT(k1)[,.(relasifuzzi=paste(relasifuzzi,collapse=",")),by=fuzzified]
   fuzzified relasifuzzi
1:         1           2
2:         2           3
3:         3           5
4:         5           9
5:         9      8,10,7

data

k1<-structure(list(fuzzified = c(1L, 2L, 3L, 5L, 9L, 9L, 9L), relasifuzzi = c(2L, 
3L, 5L, 9L, 8L, 10L, 7L)), .Names = c("fuzzified", "relasifuzzi"
), class = "data.frame", row.names = c(NA, -7L))

Upvotes: 2

Related Questions