Advertisement
Guest User

Untitled

a guest
Dec 13th, 2012
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement