R如果条件嵌套在for循环中

时间:2015-12-27 01:21:58

标签: regex r for-loop

这是R编程,提前谢谢!

我有一个数据向量,其中包含给定年份,后跟dd / mm格式的某些日期。我尝试使用for循环遍历整个向量,并使用带有正则表达式的if条件将年份值与日期值粘贴(缺少年份但发生在适当年份之下)。

载体:

doc.year <- c(2014, Alabama, 10/07, Georgia, 11/07, 2013, Virginia, 9/21, GT, 12/12, 2012, Miami, 08/21, Cal, 08/30) #original data
year.vector <- (2014, 2013, 2012) #years are pulled from the original data
doc.date <- doc.year #create a copy of the original data
gameday.vector <- (10/07, 11/07, 9/21, 12/12, 08/21, 08/30) #mm/dd dates are pulled from the original data

我的代码:

for(i in doc.year){ #iterate through the raw data
  if(doc.year[i] %in% year.vector){ #if it is a year value
    season.year <- doc.year[i] #then assign that year to a variable
  }
  else if(doc.year[i] %in% gameday.vector){ #if it's not a year value and it is a mm/dd value
    gameday <- doc.year[i] #then assign that date a variable
    doc.date[i] <- paste(gameday,"/",season.year) #and replace the copied vector with the date in mm/dd/yyyy format
  }
  else{
  }
}

期望的输出:

doc.date <- (2014, Alabama, 10/07/2014, Georgia, 11/07/2014, 2013, Virginia, 9/21/2013, GT, 12/12/2013, 2012, Miami, 08/21/2012, Cal, 08/30/2012)

2 个答案:

答案 0 :(得分:2)

你不想在R中使用for循环。如果你认为for循环,你最好使用C,Perl,C ++,Java或类似的东西。也就是说,假设数据形式为

Build fingerprint: 'Lenovo/LenovoB8080-F/B8080:4.3/JLS36C/B8080_S003313_140611_ROW_WiFi:user/release-keys'
Revision: '0'
pid: 12494, tid: 12546, name: Thread-17177  >>> com.jintin.XXXXXXX <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr deadbaad
Abort message: '@@@ ABORTING: heap corruption detected by dlmalloc'
    r0 00000027  r1 71f59cf0  r2 00000008  r3 deadbaad
    r4 00000000  r5 7680868c  r6 78f070d0  r7 40acabd5
    r8 40a8ee58  r9 78f5985d  sl 00000001  fp 78f5985f
    ip 00000001  sp 76808688  lr 4005122b  pc 4004e784  cpsr 60000030
    d0  524f424120404040  d1  6568203a474e4954
    d2  7572726f63207061  d3  6564206e6f697470
    d4  0000000000000000  d5  000000db00000008
    d6  00000db703000008  d7  0000040000000003
    d8  0000000000000000  d9  0000000000000000
    d10 0000000000000000  d11 0000000000000000
    d12 0000000000000000  d13 0000000000000000
    d14 0000000000000000  d15 0000000000000000
    d16 61b0066a80a7bef8  d17 09c0c303a1d07351
    d18 e1db241d52025705  d19 e37d6398ecf10ed7
    d20 5520ed68728b39fe  d21 147c1f8668ff3afe
    d22 7d03cbb21d60ba6a  d23 b0066a80a7bef8d3
    d24 0081818181818181  d25 0082828282828282
    d26 0000000000000000  d27 00ffffffffffffff
    d28 0100010001000100  d29 0100010001000100
    d30 ffffffffffffffff  d31 00ffffff00ffffff
    scr 60000010

backtrace:
    #00  pc 0001e784  /system/lib/libc.so
    #01  pc 0001c784  /system/lib/libc.so (abort+4)
    #02  pc 000123b9  /system/lib/libc.so
    #03  pc 0000eadd  /system/lib/libc.so
    #04  pc 0000f9ab  /system/lib/libc.so (dlmalloc+606)
    #05  pc 0000db93  /system/lib/libc.so (malloc+10)
    #06  pc 0007707d  /system/lib/libcrypto.so
    #07  pc 00077517  /system/lib/libcrypto.so (CRYPTO_malloc+66)
    #08  pc 0002a57f  /system/lib/libssl.so (ssl_parse_serverhello_tlsext+242)
    #09  pc 0001696d  /system/lib/libssl.so (ssl3_get_server_hello+904)
    #10  pc 00019457  /system/lib/libssl.so (ssl3_connect+642)
    #11  pc 00024ba1  /system/lib/libssl.so (SSL_do_handshake+72)
    #12  pc 00028def  /system/lib/libjavacore.so
    #13  pc 000203cc  /system/lib/libdvm.so (dvmPlatformInvoke+112)
    #14  pc 00050d8f  /system/lib/libdvm.so (dvmCallJNIMethod(unsigned int const*, JValue*, Method const*, Thread*)+398)
    #15  pc 00029860  /system/lib/libdvm.so
    #16  pc 0002e218  /system/lib/libdvm.so (dvmInterpret(Thread*, Method const*, JValue*)+184)
    #17  pc 00062de9  /system/lib/libdvm.so (dvmCallMethodV(Thread*, Method const*, Object*, bool, JValue*, std::__va_list)+292)
    #18  pc 00062e13  /system/lib/libdvm.so (dvmCallMethod(Thread*, Method const*, Object*, JValue*, ...)+20)
    #19  pc 00057b8b  /system/lib/libdvm.so
    #20  pc 0000cba0  /system/lib/libc.so (__thread_entry+72)
    #21  pc 0000cd1c  /system/lib/libc.so (pthread_create+208)

code around pc:
    4004e764 461ce00b 2b006823 e026d1fb b12468e4  
    4004e774 21014a18 6011447a 4b1247a0 24002027  
    4004e784 f7fc7018 2106ee0e e884f7fe 5180f04f  
    4004e794 a9029104 94022006 9403460a ec30f7fd  
    4004e7a4 46224629 f7fd2002 f7fcec3c 2106edfa  
    4004e7b4 e870f7fe f7fc2001 6960eaa6 d1dc2800  
    4004e7c4 bf00e7d4 deadbaad 00028894 ffffff9c  
    4004e7d4 0002bb38 0002bb1c 2400b510 aa04b088  
    4004e7e4 46699002 94014620 94039400 eb7ef7fd  
    4004e7f4 db0642a0 b1139b07 1c419806 98069106  
    4004e804 4620e000 bd10b008 4b1eb40e b082b5f0  
    4004e814 447b491d f107af00 585e021c f8522101  
    4004e824 68354b04 f852607d 31015b04 d1fa2d00  
    4004e834 320e008a 0507f022 0220f107 0d05ebad  
    4004e844 f107603a 46690224 9400466d 4c04f852  
    4004e854 f8453204 2c004f04 f8dfd1f8 f853c030  

code around lr:
    40051208 4a0e4b0d e92d447b 260041f0 4680589c  
    40051218 686768a5 f9b5e007 2b00300c 4628dd02  
    40051228 430647c0 3f013554 6824d5f5 d1ef2c00  
    40051238 e8bd4630 bf0081f0 00025dc8 ffffffc4  
    40051248 43f0e92d fb01461e 4b16f502 4916460f  
    40051258 447bb087 aa014614 20019001 95029203  
    40051268 90049505 681a585b b1124698 f0094630  
    40051278 4630fd26 f7ffa903 4681fe9c 0000f8d8  
    40051288 4630b110 fd28f009 0f00f1b9 9c05d005  
    40051298 1b284639 eab2f017 46204604 e8bdb007  
    400512a8 bf0083f0 00025d7a ffffff60 46036842  
    400512b8 60411e51 da012900 bb9ef000 f8126802  
    400512c8 601a0b01 00004770 4a104b0f b570447b  
    400512d8 589b4604 461e6819 f009b109 6860fcf0  
    400512e8 60651e45 da042d00 f0004620 4605fb85  
    400512f8 6822e003 5b01f812 68336022 4620b113  
```

您可以一次性获得所需的部分,而无需任何其他变量:

doc.year <- c("2014", "Alabama", "10/07", "Georgia", "11/07",
              "2013", "Virginia", "9/21")

对于任何理智的R处理,您只需创建一个包含结果的数据框:

## figure out which entries are years
is.year <- grepl("^\\d{4}$", doc.year)

## create a vector with years for all non-year entries
years <- rep(doc.year[is.year], matrix(rle(is.year)$len,2)[2,] / 2)

## paste together the dates
dates <- paste(matrix(doc.year[!is.year],2)[2,], years, sep='/')

如果你真的想回去修补> data.frame(place=matrix(doc.year[!is.year],2)[1,], date=dates) place date 1 Alabama 10/07/2014 2 Georgia 11/07/2014 3 Virginia 9/21/2013 你可以,例如:

doc.year

答案 1 :(得分:2)

@ Simon的解决方案是一个很好的R解决方案,但是如果你坚持使用循环,那么你的代码几乎就在那里:

doc.year <- c(2014, "Alabama", "10/07", "Georgia", "11/07", 2013, "Virginia", "9/21")
year.vector <- c(2014, 2013)
doc.date <- doc.year
gameday.vector <- c("10/07", "11/07", "9/21")

for(i in 1:length(doc.year)) {
  if(doc.year[i] %in% year.vector){
    season.year <- doc.year[i]
  }
  else if(doc.year[i] %in% gameday.vector){
    gameday <- doc.year[i]
    doc.date[i] <- paste(gameday,"/",season.year, sep="")
  }
}