我正在尝试对数据帧列进行计算,但它们仍然失败,因为尽管我使用了droplevels命令(从这篇文章中),该列包含级别.我在这做错了什么:
csv <- data.frame(col1 = c("question",1,23,2,5,6), col2 = c("question",5,6,7,3,""))
csv[csv==''] <- NA
csv <- csv[-c(1),] #remove the header question row because this screws up numeric calculations
csv <- droplevels(csv)
csv[,1] <- 7-csv[,1]
Run Code Online (Sandbox Code Playgroud)
我明白了:
Warning message:
In Ops.factor(7, csv[, 1]) : ‘-’ not meaningful for factors
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下代码为 Ehcache 3.3.1 项目实现侦听器。任何人都可以为 ListenerObject 提出解决方案吗?我似乎无法在任何地方找到它,除了在文档页面上我得到了代码
import java.util.logging.Level;
import java.util.logging.Logger;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheEventListenerConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.event.EventType;
public class CacheHandler{
private Logger LOG = Logger.getLogger(this.getClass().getName());
private String cacheName="basicCache";
public Cache cache;
public CacheHandler(){
if(cache==null)
cache=initCache();
}
private Cache initCache(){
CacheEventListenerConfigurationBuilder cacheEventListenerConfiguration = CacheEventListenerConfigurationBuilder
.newEventListenerConfiguration(new ListenerObject(), EventType.CREATED, EventType.UPDATED)
.unordered().asynchronous();
final CacheManager manager = CacheManagerBuilder.newCacheManagerBuilder()
.withCache(cacheName,
CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class, ResourcePoolsBuilder.heap(10))
.add(cacheEventListenerConfiguration)
).build(true);
final Cache<String, String> cache = manager.getCache("foo", String.class, String.class);
return cache;
}
public …
Run Code Online (Sandbox Code Playgroud) 有没有更好的方法来计算红宝石中的滞后(值 - 前值)?这就是我提出的.如果滞后大于0 1
,则返回,如果小于,则返回-1
(我的代码不起作用.使用ruby 2.3.5)
def lagChange(arr,k=1)
prev=nil
ret=[]
arr.each{|x|
if(x.to_i>-9999)
ret<<if(x-prev>0){1}else{-1}end
else
ret<<"NA"
end
prev=x.to_i
}
return ret
end
lagChange([1,2,3,5]) #NA 1 1 -1
Run Code Online (Sandbox Code Playgroud) 为什么dodge参数不为每个组创建多个条形图?我正在寻找一个分组的条形图,而不是我得到的堆积条形图.
df<-data.frame(c(40,23,18,41,15,14,38,21,1),c(rep("Do you agree with \nthe 'Hands Up' protestors ?",3),rep("Have the Police alienated themselves\n from the Public?",3),rep("Are the public ignoring the\n dangers Police face everyday?",3)),c("49%","28%","22%","59%","21%","20%","63%","35%","2%"),c(1,1,1,2,2,2,3,3,3))
colnames(df)<-c("values","names","percentages","group")
ggplot(df,aes(names,values,group=group))+
geom_bar(stat = "identity",position = "dodge",fill=rep(c("green","red","orange"),3))+
geom_text(aes(label=percentages))+
ylab("number of votes")+
xlab("")+
ggtitle("Police Opinion polls")
Run Code Online (Sandbox Code Playgroud)
我的结果:
我想要的是: