如何计算R中相同数字的连续字符串的出现次数

时间:2016-01-28 17:52:46

标签: r

我在R中有一个0和1的字符串:

 28-Jan-2016 19:14:51.947 SEVERE [http-nio-8080-exec-6]  org.apache.catalina.core.ApplicationContext.log StandardWrapper.Throwable
java.lang.RuntimeException: Failed to create device table.
at main.java.com.amazonaws.cognito.devauthsample.identity.DeviceAuthentication.<init>(DeviceAuthentication.java:85)
at main.java.com.amazonaws.cognito.devauthsample.identity.AWSCognitoDeveloperAuthenticationSample.<init>(AWSCognitoDeveloperAuthenticationSample.java:46)
at main.java.com.amazonaws.cognito.devauthsample.servlet.RootServlet.init(RootServlet.java:50)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1231)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:817)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:676)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)Causedby:main.java.com.amazonaws.cognito.devauthsample.exception.DataAcces  sException: Failed to get status of table:       AWSCognitoDeveloperAuthenticationSample_awscognitodeveloperauthenticationsample_DEVICES
at main.java.com.amazonaws.cognito.devauthsample.identity.DeviceAuthentication.doesTableExist(DeviceAuthentication.java:266)
at main.java.com.amazonaws.cognito.devauthsample.identity.DeviceAuthentication.<init>(DeviceAuthentication.java:81)
... 23 more
Caused by: com.amazonaws.AmazonServiceException: User: arn:aws:sts::728830372817:assumed-role/aws-elasticbeanstalk-ec2-role/i-746f13fd is not authorized to perform: dynamodb:DescribeTable on resource: arn:aws:dynamodb:us-east-1:728830372817:table/AWSCognitoDeveloperAuthenticationSample_awscognitodeveloperauthenticationsample_DEVICES (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: AccessDeniedException; Request ID: EOA1PRP4O61T364TFRCF0GP4FRVV4KQNSO5AEMVJF66Q9ASUAAJG)
at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1077)
at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:725)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:460)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:295)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:3106)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.describeTable(AmazonDynamoDBClient.java:833)
at main.java.com.amazonaws.cognito.devauthsample.identity.DeviceAuthentication.doesTableExist(DeviceAuthentication.java:261)
... 24 more

我想计算有多少个1的字符串(即两者之间没有0)。对于我在这里的例子,我想获得输出4,因为有4个1连续的字符串。

让我知道我是否应该澄清我的意思。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

我们可以尝试rle

sum(with(rle(x), lengths[!!values])>1)
$[1] 4

或者

sum(with(rle(x!=0), lengths*values)>1)

rleid

library(data.table)
sum(table(rleid(x)[x!=0])>1)
#[1] 4