如何在R中重复观察n次?

时间:2019-07-01 15:36:59

标签: r dplyr repeat

我正在努力将一个变量记录到一个年度数据集中,该变量每单位记录一次。虽然重复观察n次非常简单,但是我很难为观察分配数年。

我的数据结构如下:

java.lang.RuntimeException: javax.xml.ws.WebServiceException: Unsupported endpoint address: at com.oracle.webservices.testclient.core.ws.cdf.SOAPBindingEngine.call(SOAPBindingEngine.java:73) at com.oracle.webservices.testclient.wstest.InvocationDelegate.invoke(InvocationDelegate.java:46) at com.oracle.webservices.testclient.wstest.WsTestCaller.invokeOpertion(WsTestCaller.java:92) at com.oracle.webservices.testclient.ws.action.WsInvokeAction.execute(WsInvokeAction.java:67) at com.oracle.webservices.testclient.ws.action.AbstractAction.process(AbstractAction.java:36) at com.oracle.webservices.testclient.ws.res.WebserviceResource.invokeWebService(WebserviceResource.java:206) at com.oracle.webservices.testclient.ws.res.WebserviceResource.invokeWebServiceByFormURLEncoded(WebserviceResource.java:179) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60) at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185) at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75) at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302) at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108) at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84) at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1511) at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1442) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381) at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716) at javax.servlet.http.HttpServlet.service(HttpServlet.java:844) at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280) at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254) at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136) at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:341) at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:25) at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79) at com.oracle.webservices.testclient.web.LocaleFilter.doFilter(LocaleFilter.java:25) at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79) at com.oracle.webservices.testclient.wlsweb.TestPageAccessFilter.doFilter(TestPageAccessFilter.java:43) at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79) at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3367) at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3333) at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321) at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120) at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57) at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2220) at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2146) at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2124) at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1564) at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:254) at weblogic.work.ExecuteThread.execute(ExecuteThread.java:295) at weblogic.work.ExecuteThread.run(ExecuteThread.java:254) Caused by: javax.xml.ws.WebServiceException: Unsupported endpoint address: at com.sun.xml.ws.api.pipe.TransportTubeFactory.createDefault(TransportTubeFactory.java:167) at weblogic.wsee.jaxws.WLSTransportTubeFactory.createDefault(WLSTransportTubeFactory.java:37) at weblogic.wsee.jaxws.WLSTransportTubeFactory.doCreate(WLSTransportTubeFactory.java:18) at com.sun.xml.ws.api.pipe.TransportTubeFactory.create(TransportTubeFactory.java:133) at com.sun.xml.ws.transport.DeferredTransportPipe.processRequest(DeferredTransportPipe.java:135) at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:1136) at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:1050) at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:1019) at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:877) at com.sun.xml.ws.client.Stub.process(Stub.java:464) at com.sun.xml.ws.client.dispatch.DispatchImpl.doInvoke(DispatchImpl.java:265) at com.sun.xml.ws.client.dispatch.DispatchImpl.invoke(DispatchImpl.java:304) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at weblogic.wsee.jaxws.spi.ClientInstanceInvocationHandler.invoke(ClientInstanceInvocationHandler.java:84) at com.sun.proxy.$Proxy149.invoke(Unknown Source) at com.oracle.webservices.testclient.core.ws.cdf.Engine.invoke(Engine.java:201) at com.oracle.webservices.testclient.core.ws.cdf.SOAPBindingEngine.call(SOAPBindingEngine.java:57) ... 48 more

我想做的是创建一个名为id startyear endyear dummy 1 1946 2005 1 2 1957 2005 1 3 1982 2005 1 4 1973 2005 1 的新行,该行重复单元1 years次;单位2 n = 2005 - 1946 = 59次,依此类推,并指定年份,生成以下输出:

2005-1957

我曾尝试在dplyr中将id startyear endyear dummy year 1 1946 2005 1 1946 1 1946 2005 1 1947 1 1946 2005 1 1948 1 1946 2005 1 1949 […] slicemutaterep结合使用,但都没有得到我想要的结果。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:6)

我们可以使用map2为每个元素创建一个从'startyear'到'endyear'的序列,依次进入listunnest

library(tidyverse)
df1 %>% 
    mutate(year = map2(startyear, endyear, `:`)) %>%
    unnest
# id startyear endyear dummy year
#1    1      1946    2005     1 1946
#2    1      1946    2005     1 1947
#3    1      1946    2005     1 1948
#4    1      1946    2005     1 1949
#5    1      1946    2005     1 1950
#6    1      1946    2005     1 1951
#7    1      1946    2005     1 1952
#...

或者通过'id'将mutate分组为listunnest

df1 %>% 
  group_by(id) %>% 
  mutate(year = list(startyear:endyear)) %>% 
  unnest

答案 1 :(得分:4)

几乎没有简单的替代品:

library(tidyverse)
df1 %>% 
    uncount(endyear - startyear + 1, .id = "row") %>%
    mutate(year = startyear + row - 1)