我有类似的问题,这其中对使用多个dataframes用于绘制ggplot.我想创建一个基础图,然后使用数据框列表添加数据(下面描述的基本原理/用例).
library(ggplot2)
# generate some data and put it in a list
df1 <- data.frame(p=c(10,8,7,3,2,6,7,8),v=c(100,300,150,400,450,250,150,400))
df2 <- data.frame(p=c(10,8,6,4), v=c(150,250,350,400))
df3 <- data.frame(p=c(9,7,5,3), v=c(170,200,340,490))
l <- list(df1,df2,df3)
#create a layer-adding function
addlayer <-function(df,plt=p){
plt <- plt + geom_point(data=df, aes(x=p,y=v))
plt
}
#for loop works
p <- ggplot()
for(i in l){
p <- addlayer(i)
}
#Reduce throws and error
p <- ggplot()
gg <- Reduce(addlayer,l)
Error in as.vector(x, mode) :
cannot coerce type 'environment' to vector of type 'any'
Called …
Run Code Online (Sandbox Code Playgroud) 我有一系列基因,我想与属性列表和颜色相关联:
gene_A_color = red
gene_B_color = blue
gene_A_property = ['a','b','c']
gene_B_property = ['d','e','f']
Run Code Online (Sandbox Code Playgroud)
出于绘图的目的,我想创建一个字典,我可以使用属性值作为获取颜色或基因的键,如下所示:
#lookup dictionary
{'a': ['red', 'gene_A']
'b': ['red', 'gene_A']
'c': ['red', 'gene_A']
'd': ['blue' 'gene_B']
'e': ['blue' 'gene_B']
'f': ['blue' 'gene_B']}
lookup[a][0] = red
lookup[a][1] = gene_A
Run Code Online (Sandbox Code Playgroud)
我这样开始,但只有在我丢失基因名称时才能反转列表:
lookup_dict = defaultdict(list)
lookup_dict['red'] = ['a','b','c']
lookup_dict['blue'] = ['d','e','f']
inverted = defaultdict(list)
for k, v in lookup_dict.items():
inverted[v].append( k )
#inverted
{'a': 'red'
'b': 'red'
'c': 'red'
'd': 'blue'
'e': 'blue'
'f': 'blue' }
Run Code Online (Sandbox Code Playgroud)
建议?
我想将一个函数多次应用于数据结构,并想知道是否有更简单的方法.
;; simple map and map-incrementing function
(def a {:a 1})
(defn incmap [x] (update-in x [:a] inc))
;; best I could come up with
(reduce (fn [m _] (incmap m)) a (range 10))
;; was hoping for something like this
(repeatedly-apply incmap a 10)
Run Code Online (Sandbox Code Playgroud) 我有一个包含三个分组变量的数据集:condition,sub和delay.这是我的数据的简化版本(实际数据更长)
Run Code Online (Sandbox Code Playgroud)sub condition delay later_value choiceRT later_choice primeRT cue 10 SIZE 10 27 1832 1 888 CHILD 10 PAST 5 11 298 0 1635 PANTS 10 SIZE 21 13 456 0 949 CANDY 11 SIZE 120 22 526 1 7963 BOY 11 FUTURE 120 27 561 1 4389 CHILDREN 11 PAST 5 13 561 1 2586 SPRING
我有一套复杂的程序来应用这些数据(细节并不重要)我编写了以下函数,它可以在分割三个分组变量时完成我想要的功能.它返回我感兴趣的3个变量(indiff,p_intercept,&p_lv)
getIndiffs <- function(currdelay){
if (mean(currdelay$later_choice) == 1) {
indiff = 10.5
p_intercept = "laters"
p_lv = "laters"
}
else if (mean(currdelay$later_choice) == …
Run Code Online (Sandbox Code Playgroud) 我想找到存储在defaultdict(list)
容器中的列表的交集.这是我的字典,'d'
查找值列表'my_list':
d = { a: ['1', '2', '3'],
b: ['3', '4', '5'],
c: ['3', '6', '7']
}
my_list = ['a', 'b']
Run Code Online (Sandbox Code Playgroud)
我想返回列表的交集.根据以前的帖子我尝试了以下但是得到一个错误:TypeError:unhashable type:'list'
set.intersection(*map(set,d[my_list]))
Run Code Online (Sandbox Code Playgroud)
欢迎大家提出意见.
谢谢,zach cp
我是clojure的新手,并试图比较一个字符列表,我遇到了一些令人困惑的行为.当直接比较连接的字符串版本时,为什么难以(不可能?)比较字符列表的相等性?
(identical? (\A \T \C \G) (\A \T \C \G) )
; ClassCastException java.lang.Character cannot be cast to clojure.lang.IFn user/eval672
;(NO_SOURCE_FILE:1)
(identical? '(\A \T \C \G) '(\A \T \C \G) )
;false
;convert to string
(identical? "ATCG" "ATCG" )
;True
Run Code Online (Sandbox Code Playgroud) 我正在使用Analemma库来制作一些svg数字.为了制作更复杂的数字,我想将svg分解成可以在不同函数中生成并传递给main svg
函数的组件.简单的想法,对吗?
当我尝试传递一个值列表并且我不确定如何解决这个问题时,我一直被绊倒.在python中你可以使用解压缩列表*list
,我想知道clojure中的等价物是什么.
如果没有相应的东西,我会欣赏有关如何实现同一目标的任何指示.
(use 'analemma.svg)
;set a line
(def oneline (->
(line 0 0 100 100)
(style :stroke "#006600" :stroke-width 3)))
;create an SVG by passing one or more line elements works fine
; this works
(svg oneline)
; so does this
(svg oneline oneline)
; but if i have a list of lines created in a different function i have a problem
(def manylines (repeat 5 oneline))
; this will not work
(svg …
Run Code Online (Sandbox Code Playgroud) 什么是交错的两个向量最简单的方法n+1
和n
成员?
(def a [:a :c :e])
(def b [:b :d])
(interleave a b ); truncates to shortest list
[:a :b :c :d]
;what I would like.
(interleave-until-nil a b)
[:a :b :c :d :e]
Run Code Online (Sandbox Code Playgroud)