简体   繁体   中英

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?

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

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 = ", "))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM