Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. genre_vector <- c("Comedy","Comedy","Animation","Animation","Crime")
  2.  
  3. fgenre_vector <- factor(genre_vector)
  4. fgenre_vector
  5.  
  6. summary(genre_vector)
  7. summary(fgenre_vector)
  8.  
  9. movielength_vector <- c("Very short","Short","Medium","Short","Long","Very short","Very Long")
  10. movielength_vector
  11.  
  12. mvlength_factor <- factor(movielength_vector,
  13. ordered = TRUE,
  14. levels =c("Very short","Short","Medium","Long","Very Long"))
  15. mvlength_factor
  16.  
  17. year <- c(1985,1999,2010,2002)
  18. names(year) <- c("The Breakfast Club","American Beauty","Black Swan", "Chicago")
  19. names(year)
  20.  
  21. year["Black Swan"]
  22.  
  23. length(year)
  24.  
  25. year_sorted <- sort(year)
  26. year_sorted
  27.  
  28. min(year)
  29. max(year)
  30.  
  31. mean(year)
  32.  
  33. cost_2014 <- c(8.6,8.5,8.1)
  34. sum(cost_2014)/3
  35. mean(cost_2014)
  36. summary(cost_2014)
  37.  
  38. cost_2014[2]
  39. cost_2014[c(2,3)]
  40. cost_2014[1:3]
  41.  
  42. titles <- c("Black Swan","Casino","City of God","Jumanji","Toy Story")
  43. titles[-1]
  44.  
  45. titles[8]
  46.  
  47. cost_2014 > 8.3
  48. cost_2014[cost_2014 > 8.3]
  49.  
  50. age_restric <- c(14,12,10,NA,18,NA)
  51. age_restric[is.na(age_restric)==FALSE]
  52. age_restric
  53.  
  54. age_restriction <- c(14,16,12,10,18,18)
  55. sequences <- c(2,3,0,2,6,3)
  56.  
  57. multiply <- age_restriction * sequences
  58. multiply
  59.  
  60. cost_2014 * 10
  61. cost_2014
  62.  
  63. movie_vector <- c("Akira","Toy Story","Room","The Wave","Whiplash","Star Wars","The Ring","The Artist","Jumanji")
  64. movie_vector
  65.  
  66. movie_array <- array(movie_vector,dim = c(4,3))
  67. movie_array
  68.  
  69. movie_array[1,2]
  70. movie_array[1,]
  71. movie_array[,2]
  72.  
  73. movie_matrix <- matrix(movie_vector,nrow = 3,ncol = 3, byrow = TRUE)
  74. movie_matrix
  75.  
  76. hola <- matrix(movie_matrix[2:3,1:2],nrow = 2,ncol = 2)
  77. hola
  78.  
  79. movie <- list("Toy Story",1995,c("Animation","Adventure","Comedy"))
  80. movie
  81.  
  82. movie[2]
  83. movie[2:3]
  84.  
  85. movie <- list(name="Toy Story",year=1995,genre = c("Animation","Adventure","Comedy"))
  86. movie$name
  87. movie$year
  88. movie$genre[1]
  89.  
  90. movie["age"] <- 5
  91. movie$name
  92. movie$year
  93. movie$genre
  94. movie$age <- 7
  95. movie["age"] <- NULL
  96. movie
  97.  
  98.  
  99. movieDF <- data.frame(name = c("Toy Story","Akira","The Breakfast Club","The Artist","Modern Times","Fight Club","City of God","The Untouchables")
  100. ,year = c(1995,1998,1985,2011,1936,1999,2002,1987))
  101.  
  102. movieDF$name
  103.  
  104. movieDF[1]
  105. movieDF[1,2]
  106.  
  107. str(movieDF)
  108. movieDF
  109.  
  110. head(movieDF)
  111. tail(movieDF)
  112.  
  113. movieDF["length"] = c(81,125,97,100,87,139,130,119)
  114.  
  115. movieDFx <- data.frame(name="Dr Strange",year=1964,length=94)
  116. movieDF <- rbind(movieDF,movieDFx)
  117.  
  118. movieDF["length"] <- NULL
  119.  
  120. movieDF <- movieDF[-c(9),]
  121. movieDF <- movieDF[-9,] #Elimina filas
  122.  
  123.  
  124. movie_year <- 2002
  125. if( movie_year >2000){print('Película mayor del año 2000')}
  126.  
  127. movie_year <- 1997
  128. if(movie_year >2000){
  129. print('Película mayor del año 2000')
  130. }else{
  131. print('Película menor del año 2000')
  132. }
  133.  
  134. 1995<1987
  135. "Toy Story"=="Toy Story"
  136.  
  137. 2016 > 2015
  138. "Toy Story" != "Interstellar"
  139.  
  140. "Toy Story"=="Toy Story" & 1995<1987
  141.  
  142. "Toy Story"=="Toy Story" | 1995<1987
  143.  
  144. !("Toy Story"=="Toy Story" & 1995<1987)
  145.  
  146. for (yr in year) {print(yr)}
  147.  
  148. for (yr in year){
  149. if(yr < 2000) {
  150. print("Es una película vieja")
  151. }else{
  152. print("No es tan vieja")
  153. }
  154. }
  155.  
  156. count <- 1
  157.  
  158. while(count<=5){
  159. print(c("Número de iteración: ",count))
  160. count <- count+1
  161. }
  162.  
  163. ratings <- c(8.7,6.9,8.5)
  164. mean(ratings)
  165.  
  166. sort(ratings)
  167. sort(ratings,decreasing = TRUE)
  168.  
  169. printHelloWorld <- function(a="Hello World!") {print(a)}
  170. printHelloWorld("Hola Mundo")
  171.  
  172. add <- function(x=3,y=4) {x+y}
  173. add(6,4)
  174.  
  175. isGoodRating <- function(rating){
  176. if(rating<7){
  177. return("No")
  178. }else{
  179. return("Si")
  180. }
  181. }
  182. isGoodRating(ratings[2])
  183.  
  184. isGoodRating <- function(rating,threshold=7){
  185. if(rating<threshold){
  186. return("No")
  187. }else{
  188. return("Si")
  189. }
  190. }
  191. isGoodRating(10,9)
  192.  
  193.  
  194. my_data = data.frame(name=c("Toy Story","Akira","The Breakfast Club","The Artist","Modern Times","Fight Club"),
  195. year=c(1995,1998,1985,2011,1936,1999),
  196. length_min=c(81,125,97,100,87,139),
  197. genre=c("Animation","Animation","Drama","Romance","Comedy","Drama"),
  198. average_rating = c(8.3,8.1,7.9,8,8.6,8.9),
  199. foreign=c(0,1,0,1,0,0),
  200. age_restriction=c(0,14,14,12,10,18))
  201.  
  202. watchMovie <- function(moviename,my_threshold = 7){
  203. rating <- my_data[my_data$name == moviename, "average_rating"]
  204. isGoodRating(rating,threshold = my_threshold)
  205. }
  206.  
  207. watchMovie("Akira")
  208. watchMovie("The Breakfast Club",8)
  209.  
  210. myFunction <- function() {
  211. y <<- 3.14
  212. temp <- "Hello World!"
  213. return(temp)
  214. }
  215.  
  216. myFunction()
  217. y
  218. temp
  219.  
  220. x <- 5
  221. average_rating <- 8.3
  222. class(average_rating)
  223.  
  224. movies <- c("Toy Story","Akira")
  225. class(movies)
  226.  
  227. logical_vector <- c(TRUE,FALSE,FALSE,TRUE,TRUE)
  228. class(logical_vector)
  229.  
  230. age_restriction <- c(12,10,18,18)
  231. class(age_restriction)
  232.  
  233. integer_vector <- as.integer(age_restriction)
  234. class(integer_vector)
  235.  
  236. yar <- as.character(1995)
  237. yar
  238.  
  239. combined <- c("Toy Story",1995,"Akira",1998)
  240. class(combined)
  241.  
  242. for(i in 1:3){print(i+"a")}
  243.  
  244. tryCatch(10+10)
  245.  
  246. tryCatch("a"+10)
  247.  
  248. tryCatch(10+"a",error = function(e)
  249. print("Ups, algo va mal"))
  250.  
  251. tryCatch(
  252. for (i in 1:3) {
  253. print(i+"a")
  254. },
  255. error=function(e) print(c("Error encontrado",e))
  256. )
  257.  
  258. as.integer("A")
  259. tryCatch(as.integer("A"),
  260. warning=function(e) print(c("Aviso",e)))
  261.  
  262.  
  263. peliculas = read.csv("/home/vagrant/peliculas.csv")
  264. class(peliculas)
  265.  
  266. install.packages("readxl")
  267. library(readxl)
  268.  
  269. peliculas = read_excel("/home/vagrant/movies.xlsx")
  270.  
  271. data()
  272. titanic = data.frame(Titanic)
  273.  
  274. quijote <- readLines("/home/vagrant/el_quijote.txt")
  275. length(quijote)
  276. nchar(quijote)
  277. file.size("/home/vagrant/el_quijote.txt")
  278.  
  279. quijote2 <- scan("/home/vagrant/el_quijote.txt", "")
  280. quijote2
  281.  
  282. m <- matrix(c(1,2,3,4,5,6),nrow = 2,ncol = 3)
  283. write(m,file="matriz_como_texto",ncolumns = 3,sep=" ")
  284.  
  285. install.packages("sparklyr")
  286. install.packages("dplyr")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement