Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 13th, 2012  |  syntax: None  |  size: 1.22 KB  |  views: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. mat<-read.csv("mat.csv")
  2. sub<-c(123,132)
  3.        
  4. set<- subset(mat, mat$V2 %in% sub)
  5. set[,7]<-set[,7]+1
  6.        
  7. # download and read in your file from dropbox
  8. tf <- tempfile()
  9. library(httr)
  10. csvpage <- GET( "https://dl.dropbox.com/u/22681355/mat.csv" )
  11. writeBin(content(csvpage, "raw"), tf )
  12.  
  13. # import the data into R
  14. mat <- read.csv( tf )
  15.  
  16. # note read.csv creates a data frame not a matrix
  17. class( mat )
  18.  
  19. # create your sub object
  20. sub <- c(123,132)
  21.  
  22. # subset the matrix..
  23.  
  24. # show all rows where V2 is equal to any of the contents of sub
  25. mat[ mat$V2 %in% sub , ]
  26.  
  27. # access only the V7 column for rows where V2 is equal to the contents of sub
  28. mat[ mat$V2 %in% sub , 'V7' ]
  29.  
  30. # overwrite the V7 column with MISSING
  31. # for rows where V2 is equal to the contents of sub
  32. mat[ mat$V2 %in% sub , 'V7' ] <- NA
  33.  
  34. # OR
  35.  
  36. # overwrite the V7 column with the value of V2 (which matched something in sub)
  37. # for rows where V2 is equal to the contents of sub
  38. mat[ mat$V2 %in% sub , 'V7' ] <- mat[ mat$V2 %in% sub , 'V2' ]
  39.  
  40. # OR
  41.  
  42. # overwrite the V7 column with itself plus one
  43. # for rows where V2 is equal to the contents of sub
  44. mat[ mat$V2 %in% sub , 'V7' ] <- mat[ mat$V2 %in% sub , 'V7' ] + 1
  45.        
  46. mat[7] <- mat[7] + mat$V2 %in% sub
  47.        
  48. mat["V7"] <- mat["V7"] + mat$V2 %in% sub
clone this paste RAW Paste Data