我对大量的.txt文件进行了情感分析,结果显示在数据帧列表(listtxt)中。该列表中的每个数据框代表对一个.txt文件的分析。
因此,第7和第8个txt文件的listtxt如下所示
[[7]]
element_id word_count sd ave_sentiment
1: 1 1 NA 0.00000000
2: 2 8 NA 0.00000000
3: 3 15 NA 0.25819889
4: 4 28 NA 0.28347335
5: 5 45 0.1689353 0.05695529
---
115: 115 104 0.1864339 -0.33659356
116: 116 60 0.5830784 -0.11404312
117: 117 83 0.1694059 0.20015495
118: 118 54 0.3094642 0.18327970
119: 119 30 0.4849154 0.36961306
[[8]]
element_id word_count sd ave_sentiment
1: 1 1 NA 0.000000000
2: 2 7 NA 0.188982237
3: 3 15 NA 0.258198890
4: 4 98 0.2573836 0.018342501
5: 5 85 0.3151697 0.009575169
---
167: 167 60 0.1950696 0.182300225
168: 168 65 0.2226071 0.338903560
169: 169 12 NA 0.230940108
170: 170 8 NA 0.353553391
171: 171 3 0.0000000 0.000000000我创建了一个单独的dataframe dfsummary,其中的变量(name,date,...)提供所分析的每个txt文件的概述。
它看起来像这样
dfsummary.
V1 Year Name V2
1 "Poem" 1000 Person1 a
2 "Poem" 1001 Person1 b
3 "Poem" 1002 Person1 a
4 "Poem" 1003 Person2 b
5 "Poem" 1004 Person2 b
6 "Poem" 1004 Person2 b
7 "Poem" 1005 Person2 a
8 "Poem" 1006 Person3 a我正在尝试创建一个循环,这样listtxt中的每个数据帧都可以根据dfsummary中它所链接的行来绘制。然后用它们所链接的每个名称对图进行facet_wrap。
这是我的代码。
for (i in dfsummary) {
title <- paste("poem", dfsummary$Name[i], dfsummary$Year[i])
}然后对于情节
for (i in (listtxt)){
sentiment.plots <- pblapply(listtxt, function(x)
p <- ggplot(x, aes(x, y)) +
geom_jitter(...) +
geom_smooth(...) +
...
ggtitle(paste0(title)))
}该代码返回列表中的绘图,但是dfsummary$$Yeari的标题中都有"NA“值
有人对我做错了什么有什么建议吗?
谢谢
发布于 2020-04-07 02:26:28
没有任何数据,我不能产生你的确切情景。但是这里有一个reprex,它执行您所描述的操作:使用一个循环创建标题名称列表,使用另一个循环使用标题列表中的名称创建列表的绘图。上面的注释描述了一些问题,这只是一个小示例,展示了使用数据集虹膜修复的一种方法。
library(tidyverse)
df <- data.frame(Name = c("one", "two", "three"),
Year = c(2018, 2019, 2020))
#if explicitly want a loop
title <- vector() #allocate space for storage
for (i in 1:nrow(df)) {
#use [[i]] to avoid overwriting title object each time
title[[i]] <- paste("aaa", df$Name[i], df$Year[i], "bbbb")
}
#but as Gregor points out much easier to do this:
title <- paste("aaa", df$Name, df$Year, "bbbb")
plots <- vector("list")
for(i in 1:nrow(df)){
plots[[i]] <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length))+
geom_point()+
ggtitle(title[[i]]) ##specify with [[i]] what title to use
}
plots[[1]]"plots[ 1 ]“将显示:

https://stackoverflow.com/questions/61066120
复制相似问题