r tm extract doc ids after sort operation - r

I have mindfreeze around this issue. I am extracting most frequent words from a tm::dtm, like so:
> s1<-sort(rowSums(as.matrix(dtm10[,])), decreasing=TRUE)
I get:
290 429 318 125 128 425 431 153 52 385 144 491 126 423 111 130 492 163 176 391
916 875 860 851 844 823 822 766 759 743 701 700 686 673 670 669 663 658 652 623`
But the doc ids and rowSums are in a tuple.
> s1[2]
429
875
where 429 is doc id and rowsum is 875. I have no further use of the rowSums, how do I get a list of the sorted doc ids? I am looking for a vector output like:
290 429 318 125 128 425 431 153 52 385 144 491 126 423 111 130 492 163 176 391
Many thanks.

s1 is a named vector with names as 290, 429, 318 etc and values like 916,875, 860 and so on.
To extract only the names of s1 , we can use,
names(s1)
which would give :
#290 429 318 125 128 425 431 153 52 385 144 491 126 423 111 130 492 163 176 391

Related

Efficiently Finding Sequences Between Vectors of Start and End Numbers in R [duplicate]

This question already has answers here:
Expand ranges defined by "from" and "to" columns
(10 answers)
Closed 2 years ago.
I have two vectors in R, say, start_values and end_values, which contain numbered elements of increasing value. For example:
start_values <- c(88, 241, 394, 545)
end_values <- c(147, 300, 453, 604)
I'm trying to find an efficient (hopefully without writing a loop) that will allow me to obtain a single vector of numbers with sequences of numbers that range from the first element in start_values to the first element in end_values, then from the second element in start_values to the second element in end_values, etc. So in the end, I'd like a vector called sequence_range that looks like this:
sequence_range <- c(seq(88, 147), seq(241, 300), seq(394, 453), seq(545, 604))
which should have output that looks like:
> sequence_range
[1] 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
[30] 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
[59] 146 147 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
[88] 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
[117] 297 298 299 300 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
[146] 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
[175] 448 449 450 451 452 453 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
[204] 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
[233] 597 598 599 600 601 602 603 604
I'd appreciate any ideas for efficient techniques to accomplish this so that it's generalizeable to any two vectors of start and end values.
Thanks.
You may use : in an apply on cbinded vectors.
as.vector(apply(cbind(start_values, end_values), 1, function(x) x[1]:x[2]))
# [1] 88 89 90 91 92 93 94 95 96 97 98 99 100 101
# [15] 102 103 104 105 106 107 108 109 110 111 112 113 114 115
# [29] 116 117 118 119 120 121 122 123 124 125 126 127 128 129
# [43] 130 131 132 133 134 135 136 137 138 139 140 141 142 143
# [57] 144 145 146 147 241 242 243 244 245 246 247 248 249 250
# [71] 251 252 253 254 255 256 257 258 259 260 261 262 263 264
# [85] 265 266 267 268 269 270 271 272 273 274 275 276 277 278
# [99] 279 280 281 282 283 284 285 286 287 288 289 290 291 292
# [113] 293 294 295 296 297 298 299 300 394 395 396 397 398 399
# [127] 400 401 402 403 404 405 406 407 408 409 410 411 412 413
# [141] 414 415 416 417 418 419 420 421 422 423 424 425 426 427
# [155] 428 429 430 431 432 433 434 435 436 437 438 439 440 441
# [169] 442 443 444 445 446 447 448 449 450 451 452 453 545 546
# [183] 547 548 549 550 551 552 553 554 555 556 557 558 559 560
# [197] 561 562 563 564 565 566 567 568 569 570 571 572 573 574
# [211] 575 576 577 578 579 580 581 582 583 584 585 586 587 588
# [225] 589 590 591 592 593 594 595 596 597 598 599 600 601 602
# [239] 603 604
mapply works nicely:
> as.vector(mapply(seq,start_values,end_values))
[1] 88 89 90 91 92 93 94 95 96 97 98 99 100 101
[15] 102 103 104 105 106 107 108 109 110 111 112 113 114 115
[29] 116 117 118 119 120 121 122 123 124 125 126 127 128 129
[43] 130 131 132 133 134 135 136 137 138 139 140 141 142 143
[57] 144 145 146 147 241 242 243 244 245 246 247 248 249 250
[71] 251 252 253 254 255 256 257 258 259 260 261 262 263 264
[85] 265 266 267 268 269 270 271 272 273 274 275 276 277 278
[99] 279 280 281 282 283 284 285 286 287 288 289 290 291 292
[113] 293 294 295 296 297 298 299 300 394 395 396 397 398 399
[127] 400 401 402 403 404 405 406 407 408 409 410 411 412 413
[141] 414 415 416 417 418 419 420 421 422 423 424 425 426 427
[155] 428 429 430 431 432 433 434 435 436 437 438 439 440 441
[169] 442 443 444 445 446 447 448 449 450 451 452 453 545 546
[183] 547 548 549 550 551 552 553 554 555 556 557 558 559 560
[197] 561 562 563 564 565 566 567 568 569 570 571 572 573 574
[211] 575 576 577 578 579 580 581 582 583 584 585 586 587 588
[225] 589 590 591 592 593 594 595 596 597 598 599 600 601 602
[239] 603 604

looping a sequence of numbers

I need to generate the following sequence of numbers:
from 101 to 124 then
from 201 to 224
and so on. I need to repeat this pattern 7 times, up to 724.
I know I can simply use
c(101:124,201:224, ...)
but I suspect there is an easier way. Maybe a loop?
We can try seq with sapply
c(sapply(seq(101, 700, by = 100), function(i) i:(i+23)))
Or we can use rep
(101:124) +rep(0:6, each = 24)*100
#[1] 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
#[19] 119 120 121 122 123 124 201 202 203 204 205 206 207 208 209 210 211 212
#[37] 213 214 215 216 217 218 219 220 221 222 223 224 301 302 303 304 305 306
#[55] 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
#[73] 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
#[91] 419 420 421 422 423 424 501 502 503 504 505 506 507 508 509 510 511 512
#[109] 513 514 515 516 517 518 519 520 521 522 523 524 601 602 603 604 605 606
#[127] 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
#[145] 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
#[163] 719 720 721 722 723 724
Using common 'for' loop:
> vect=c(); for(i in seq(from=101,to=701, by=100)) vect = c(vect, seq(from=i,to=i+23))
> vect
[1] 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 201 202 203 204 205 206 207
[32] 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 301 302 303 304 305 306 307 308 309 310 311 312 313 314
[63] 315 316 317 318 319 320 321 322 323 324 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
[94] 422 423 424 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 601 602 603 604
[125] 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 701 702 703 704 705 706 707 708 709 710 711
[156] 712 713 714 715 716 717 718 719 720 721 722 723 724
We can use either the outer or CJ(cross join), both of which list all the combinations of elements from the two vector arguments. By specifying the reduce function as "+", where for outer, there is a parameter placeholder while for CJ you will have to explicitly use the Reduce function, they should produce the results as wanted. Just to be noted, the outer function will result in a matrix, so we use as.vector to convert it to a 1d array.
as.vector(outer(1:24, (1:7)*100, "+"))
or
Reduce("+", data.table::CJ(1:24, (1:7)*100))

cleaning the data R

I've got the final data DS such as :
|user_id
40 33
70 50
93 67
106 77
136 91
144 97
160 105
176 113
195 128
207 132
211 134
229 142
280 159
338 187
407 232
425 248
442 259
446 261
469 277
470 278
588 353
590 355
594 358
598 362
609 369
615 375
626 381
633 386
652 399
657 402
735 452
751 464
758 470
760 471
769 478
774 480
806 493
821 501
825 505
856 526
876 536
886 540
890 542
894 543
903 549
919 556
921 558
932 562
The fist column is a what left of line numbers I suppose, after many data manipulations,
and I'd like to drop them, nice, efficient way, and replace it with normal order numbers , 1,2,3,4,5 etc.
I did try to use :
aggr.cid <-aggregate(cbind(DS$user_id), by=list(CustID = DS$user_id),
function(x) x[1])
But instead of getting 1 line I'm getting two, with content of "user_id"
I can remove the second one and all will looks as I need but it is a doggy way....
Those are the row names. You can reset them with
rownames(DS) <- NULL

Waves argument in geeglm of geepack in R causes failure

I am trying to calculate a GEE-model in the R package "geepack". The response variable is proportional, coded as (Successes, Failures). The explanatory variables are Weight(cont), Rank(cont), ColonySize(cont) and Sex(factor). The data set contains temporal non-independence of observations because over a study period of 413 days repeated behavioral measurements of the same individuals where taken. This non-independence is reflected in a column specifying the AnimalID and the day of observation (Ndate). The data set is not very large and contains 1062 observations on 165 different individuals. The complete study period is 413 days (i.e. Ndate range:1-413).
gee1<-geeglm(wl~WeightScaled+Rank+ColonySize+Sex,
data=allsub, family=binomial, id=AnimalID,
corstr="ar1")
The above model is calculated without difficulties and without noticeable delay. However, the observations are not regularly distributed over the study period (see the complete vector for Ndate below) which means the model output is not meaningful. When I include the waves argument in the model to correctly account for temporal auto-correlation R seems to get stuck or takes very long to calculate this model which should really not take so much time. What happens is that R-Gui displays "(not responding)" for more than 1 hour and the small circle (Win7) indicates that R is busy. The CPU-usage according to the task manager is mostly between 25-30%, sometimes up to 50%. So my question is: Did I make a mistake when specifying the "waves" function which cause R to hang itself or is it normal for this process to be computational very intense? (see an extract of the variable Ndate below)
Model including the waves argument:
gee1<-geeglm(wl~Weight+Rank+ColonySize+Sex,
data=allsub, family=binomial, id=AnimalID,
corstr="ar1", waves=Ndate)
The second question is more fundamental with regards to this GEE and its autocorrelation structure: Is the model able to deal with this kind of temporal autocorrelation where repeated observations of one individual are typically 5-15 but time in between varies largely (sometimes only a few days, but sometimes up to 100 days or more). Textbook examples all look very different but as I see it the principle should be the same.
Thanks very much.
> allsub$Ndate
[1] 169 169 169 43 43 5 5 5 267 267 267 267 162 162 162 162 162 256
[19] 256 256 256 256 256 263 263 263 263 263 263 176 176 176 176 176 176 183
[37] 183 183 183 183 183 190 190 190 190 190 190 190 196 196 196 196 196 196
[55] 196 284 284 284 284 291 291 291 291 175 175 175 175 175 175 175 175 199
[73] 199 199 199 199 199 199 186 186 186 186 186 186 189 189 189 189 189 189
[91] 266 266 266 266 266 266 196 196 196 196 196 196 196 242 242 242 242 242
[109] 242 207 207 207 207 207 210 210 210 210 210 245 245 245 245 245 245 302
[127] 302 302 302 302 302 302 302 217 217 217 217 217 217 217 270 270 270 272
[145] 272 272 291 291 291 220 220 220 220 220 220 220 238 238 238 238 238 238
[757] 291 291 291 291 291 291 220 220 238 238 241 241 294 294 294 294 294 294
[775] 303 303 303 263 263 263 263 263 263 263 263 263 263 316 316 309 304 304
[793] 304 323 323 19 50 99 67 67 67 22 22 22 43 60 110 178 178 178
[811] 33 115 115 115 115 96 116 116 116 116 116 116 116 116 116 116 116 26
[829] 26 122 122 122 122 122 122 122 122 122 64 40 40 40 40 40 40 40
[847] 40 40 58 58 58 58 58 58 58 58 58 58 71 71 75 85 127 78
[865] 78 12 12 12 12 12 12 12 12 12 12 15 152 152 152 152 337 337
[883] 337 337 337 337 344 344 344 344 344 344 344 82 82 82 82 82 82 82
[901] 82 82 348 348 348 348 348 348 348 348 348 351 351 351 359 359 355 355
[919] 355 354 354 345 345 345 358 358 358 358 362 362 362 331 331 349 349 361
[937] 361 378 364 364 364 369 369 369 375 375 375 373 373 373 373 342 365 365
[955] 365 365 365 365 365 365 379 379 379 379 379 379 379 379 379 379 379 379
[973] 379 379 352 352 341 382 382 382 385 373 373 373 373 373 373 368 368 368
[991] 389 389 389 389 285 285 285 308 308 309 309 321 322 326 329 329 329 329
[1009] 330 330 330 330 385 385 385 385 385 385 385 380 380 380 380 380 380 380
[1027] 386 386 386 386 390 390 390 390 365 365 393 393 393 393 393 393 393 393
[1045] 393 393 393 393 393 393 399 397 397 397 392 392 392 392 407 407 400 400
[1063] 413 413
I founds out why R crashes when including the waves argument. GEEglm does not accept two observations on the same individual conducted on the same day. This makes sense when thinking through what the model does. Hope this may help someone else.

In R, how can one detect consecutive data points within a column of data which have a value >y only when they appear more than x times consecutively?

I am a beginner R user and am trying to find a way to detect x consecutive values within a column of data which have a value >=y. Example: find all instances where 4 or more consecutive data points have a value >=2
The run length encoding rle() command looks promising for identifying these consecutive values:
rle(dataset>=2)
but I cannot find a way to further set the condition for the lengths to be >=4 and the values to be "TRUE".
Any suggestions?
res <- rle(dataset>=2)
which( res$lengths>=4 & res$values==TRUE] )
That will identify them in the compacted representation of the rle result and you will then need to expand that result and pick out starting points for the sequences.
You can simply transform the vector, and use rle on that:
res = rle(runif(1000, 0, 4) >= 2)
res
Run Length Encoding
lengths: int [1:491] 2 2 2 2 3 1 3 2 7 1 ...
values : logi [1:491] TRUE FALSE TRUE FALSE TRUE FALSE ...
To get the indices where in the vector the runs are located, you can use this trick:
res$values = res$lengths > 4
inv_res = inverse.rle(res)
seq_along(inv_res)[inv_res]
[1] 3 4 5 6 7 8 9 10 11 12 13 37 38 39 40 41 42 74
[19] 75 76 77 78 79 80 81 82 83 84 85 108 109 110 111 112 142 143
[37] 144 145 146 147 148 221 222 223 224 225 226 235 236 237 238 239 240 241
[55] 278 279 280 281 282 305 306 307 308 309 310 311 312 313 314 315 316 317
[73] 318 319 342 343 344 345 346 347 414 415 416 417 418 419 430 431 432 433
[91] 434 435 449 450 451 452 453 472 473 474 475 476 477 478 523 524 525 526
[109] 527 545 546 547 548 549 561 562 563 564 565 566 567 568 569 607 608 609
[127] 610 611 612 613 625 626 627 628 629 630 646 647 648 649 650 651 652 699
[145] 700 701 702 703 765 766 767 768 769 770 771 772 773 789 790 791 792 793
[163] 794 795 800 801 802 803 804 810 811 812 813 814 850 851 852 853 854 855
[181] 869 870 871 872 873 879 880 881 882 883 904 905 906 907 908 909 919 920
[199] 921 922 923 949 950 951 952 953 954 955 956 957 958 959 960 961

Resources