Convert a data.frame into a list of characters based on one of the column of the dataframe with R

孤者浪人 提交于 2020-05-15 08:06:10

问题


I need to convert a data.frame into a list of characters based on one of the column of the dataframe.

Starting from a data.frame of two colums, the first one contains uniques values of compounds names, while the other contains compound type categories, that are not unique. Example:

Compound_name Compound_type
A             Inhibitor_A
B             Inhibitor_B
C             Inhibitor_A
D             Inhibitor_C
E             Inhibitor_B

I would like to end with a list based on the compound types that looks like this:

Inhibitor_A   'A' 'C'
Inhibitor_B   'B' 'E'
Inhibitor_C   'C'

My data.frame contains 2000 compounds, so I need a way to do it iteratively.

I don't know where to start to compute this, so many thanks in advance.


回答1:


You can use split like:

split(x$Compound_name, x$Compound_type)
#$Inhibitor_A
#[1] "A" "C"
#
#$Inhibitor_B
#[1] "B" "E"
#
#$Inhibitor_C
#[1] "D"

Data:

x <- structure(list(Compound_name = c("A", "B", "C", "D", "E"), Compound_type = c("Inhibitor_A", 
"Inhibitor_B", "Inhibitor_A", "Inhibitor_C", "Inhibitor_B")), class = "data.frame", row.names = c(NA, 
-5L))



回答2:


Another base R option is unstack

unstack(x, Compound_name ~ Compound_type)
#$Inhibitor_A
#[1] "A" "C"

#$Inhibitor_B
#[1] "B" "E"

#$Inhibitor_C
#[1] "D"

data

x <- structure(list(Compound_name = c("A", "B", "C", "D", "E"), 
  Compound_type = c("Inhibitor_A", 
"Inhibitor_B", "Inhibitor_A", "Inhibitor_C", "Inhibitor_B")), 
 class = "data.frame", row.names = c(NA, 
-5L))



回答3:


library(data.table)
d <- data.table( name = sample(letters, 100, replace = T),
                 type= paste0("in_" ,sample(letters[1:5], replace = T)))

d <- unique(d)

dd <- d[, .(gg = list(name)), by=type]

dd
   type              gg
1: in_a p,o,b,w,h,x,...
2: in_d t,p,w,q,j,n,...
3: in_c o,b,g,k,m,a,...
4: in_b b,u,e,y,r,i,...
5: in_e w,m,n,g,s,t,...

str(dd)
Classes ‘data.table’ and 'data.frame':  5 obs. of  2 variables:
 $ type: chr  "in_a" "in_d" "in_c" "in_b" ...
 $ gg  :List of 5
  ..$ : chr  "p" "o" "b" "w" ...
  ..$ : chr  "t" "p" "w" "q" ...
  ..$ : chr  "o" "b" "g" "k" ...
  ..$ : chr  "b" "u" "e" "y" ...
  ..$ : chr  "w" "m" "n" "g" ...
 - attr(*, ".internal.selfref")=<externalptr> 

list <- dd$gg
names(list) <- dd$type


来源:https://stackoverflow.com/questions/61346815/convert-a-data-frame-into-a-list-of-characters-based-on-one-of-the-column-of-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!