Data frame

id=c(100,110,120,130,140,150,160,170,180,190) 
edad=c(18,19,NA,18,24,27,22,25,22,15) 
gen=c(2,1,2,2,1,2,1,1,2,1) 
peso=c(65,58,56,61,84,99,50,64,87,87) 
altura=c(161,170,174,165,150,171,181,170,184,190) 
niv_col=c(1,2,3,1,3,2,3,1,2,3) 
data.frame(id, edad, gen, peso, altura, niv_col)
##     id edad gen peso altura niv_col
## 1  100   18   2   65    161       1
## 2  110   19   1   58    170       2
## 3  120   NA   2   56    174       3
## 4  130   18   2   61    165       1
## 5  140   24   1   84    150       3
## 6  150   27   2   99    171       2
## 7  160   22   1   50    181       3
## 8  170   25   1   64    170       1
## 9  180   22   2   87    184       2
## 10 190   15   1   87    190       3

ETIQUETAR y recodificar

gen2<-factor(gen, levels = c(1,2), labels = c("hombre", "mujer"))

Recodificar; uso de corchetes

Ident <- 1:5
Genero <- c("Masculino", "Femenino", "Masculino", "Masculino", "Femenino")  
Salud <- c(0, 1, 1, 0, 1) #donde 0 es sano y 1 enfermo 
Opinion <- c("Mucho", "Poco", "Mucho", "Bastante", "Mucho") 
Fuma <- c("1", "2", "1", "2", "2") 
OP <- data.frame(Ident, Genero, Salud, Opinion, Fuma)

Cambiar valores de Salud de 0 y 1 a 1 y 2

Con corchetes

OP$Salud[OP$Salud==1]<-2
OP
##   Ident    Genero Salud  Opinion Fuma
## 1     1 Masculino     0    Mucho    1
## 2     2  Femenino     2     Poco    2
## 3     3 Masculino     2    Mucho    1
## 4     4 Masculino     0 Bastante    2
## 5     5  Femenino     2    Mucho    2
OP$Salud[OP$Salud==0]<-1
OP
##   Ident    Genero Salud  Opinion Fuma
## 1     1 Masculino     1    Mucho    1
## 2     2  Femenino     2     Poco    2
## 3     3 Masculino     2    Mucho    1
## 4     4 Masculino     1 Bastante    2
## 5     5  Femenino     2    Mucho    2

Función ifelse

OP$Salud <- ifelse(OP$Salud == 0, 1, 2)

Recodificar Salud: sano y enfermo

Metiendo el factor creado en la variable directamente, me evito pasos innecesarios

OP$Salud<-factor(OP$Salud, levels = c(1,2), labels = c("Enfermo", "Sano"))
OP
##   Ident    Genero Salud  Opinion Fuma
## 1     1 Masculino  Sano    Mucho    1
## 2     2  Femenino  Sano     Poco    2
## 3     3 Masculino  Sano    Mucho    1
## 4     4 Masculino  Sano Bastante    2
## 5     5  Femenino  Sano    Mucho    2

Cambiar, en género, masc y fem por 0 y 1

OP$Genero<-ifelse(OP$Genero=="Masculino", 0,1)
OP
##   Ident Genero Salud  Opinion Fuma
## 1     1      0  Sano    Mucho    1
## 2     2      1  Sano     Poco    2
## 3     3      0  Sano    Mucho    1
## 4     4      0  Sano Bastante    2
## 5     5      1  Sano    Mucho    2

Tablas de frecuencia

Nuevas variables

id=c(100,110,120,130,140,150,160,170,180,190) 
edad=c(18,19,NA,18,24,27,22,25,22,15) 
gen=c(2,1,2,2,1,2,1,1,2,1) 
peso=c(65,58,56,61,84,99,50,64,87,87) 
altura=c(161,170,174,165,150,171,181,170,184,190) 
niv_col=c(1,2,3,1,3,2,3,1,2,3) 

#Frecuencia absoluta mostrando valores NA
table(edad, useNA = "ifany")
## edad
##   15   18   19   22   24   25   27 <NA> 
##    1    2    1    2    1    1    1    1
#Frecuencia relativa
prop.table(edad)
##  [1] NA NA NA NA NA NA NA NA NA NA
prop.table(altura)
##  [1] 0.09382284 0.09906760 0.10139860 0.09615385 0.08741259 0.09965035
##  [7] 0.10547786 0.09906760 0.10722611 0.11072261
table(niv_col, gen)
##        gen
## niv_col 1 2
##       1 1 2
##       2 1 2
##       3 3 1
#tabla contingencia salud - opinión
cont<-table(Salud, Opinion)
print(cont)
##      Opinion
## Salud Bastante Mucho Poco
##     0        1     1    0
##     1        0     2    1
set.seed(999)
rnorm(1000, mean = 0,sd=1)->norm1
rnorm(1000, mean = 10, sd=1)->norm2
norm1
##    [1] -0.2817401589 -1.3125596303  0.7951839825  0.2700704943 -0.2773064221
##    [6] -0.5660237388 -1.8786582578 -1.2667911439 -0.9677496844 -1.1210093616
##   [11]  1.3254637121  0.1339773908  0.9387494457  0.1725381013  0.9576504468
##   [16] -1.3626862453  0.0683351329  0.1006576476  0.9013447540 -2.0743571058
##   [21] -1.2285633035  0.6430443187 -0.3597629141  0.2940355856 -1.1252684881
##   [26]  0.6422656503 -1.1067375801 -0.8848403917 -1.5540951437 -0.1266789886
##   [31]  2.3826641558  0.6012761009  0.1793612670  1.0805314842 -0.2468121056
##   [36] -2.1137369902 -0.3705274711  0.5228677930  0.5178055361 -1.4025108734
##   [41] -0.4856367260  0.0084981387 -1.2821132867 -1.1115788406  0.3006654106
##   [46]  0.2764788448 -2.0508776585  0.0141902114  0.5822664777 -0.0347263866
##   [51] -0.1166641473 -0.6449820878  1.7444115998  0.3660944745 -0.0668099260
##   [56]  0.2826124660  0.5676951748 -1.2792158969  0.4353688083 -0.5655009822
##   [61] -0.9233113555  1.1649540033  1.0420686823  1.1038283634 -0.0185749628
##   [66] -1.1469576692 -1.4081795204 -0.2823287292 -0.4177700162  0.9966635153
##   [71] -0.1062857787 -0.0694021535  0.9497011006 -0.4165328074  0.9740004058
##   [76]  0.0622914303  0.5384220527 -2.0648232536  0.4364316523 -0.1602266887
##   [81] -0.6429227299  0.9852985536 -1.2285733283  0.0852246689 -1.2040938331
##   [86] -0.3768477586  1.3636485788 -0.2528827526  1.0064899780  0.4371491442
##   [91]  1.6585384242  0.0276852113 -0.9787195761  1.2837291425 -1.1297416100
##   [96]  1.0466577288 -0.5779321999 -0.2937943010 -0.2417764306  0.2199478245
##  [101] -1.6509552095  0.4782006874 -0.8052823971 -0.6793295435  1.6073388307
##  [106] -2.5954909186  0.2901482333  1.3836599328 -1.5100542579 -0.6772985616
##  [111] -0.2979716410 -1.5191193563 -0.9118353267 -0.8358806635 -0.2171494542
##  [116] -1.0710323145  0.9450479987  1.1279967971 -1.2786428701  0.4576312745
##  [121] -0.4689673130  0.1139658523 -2.1249926887 -1.7664359181  1.2245907572
##  [126]  0.1778560165  0.2643239983  0.1928339324  1.0700506754  0.2254140200
##  [131] -0.4445507442 -0.1652123960  0.3009234906 -0.4525010581 -0.7701264812
##  [136]  0.6021471158 -1.3148362247 -0.8822634238 -0.9044135376  1.0786102526
##  [141] -0.0486617256  1.4204964731 -0.0819131247  0.8937469624  0.5169801420
##  [146] -0.5856277886  1.0906934519  0.0976721188 -0.8230414751  1.0088029504
##  [151]  0.9951514144  1.4098562212  0.1042460378  0.7605152536  0.8958375632
##  [156] -0.0978086639 -0.1685562985  1.4799712274  1.4142991163 -0.5794274464
##  [161]  0.0635272473  0.0321130383 -0.9624668317 -0.4945015630 -1.1760742855
##  [166]  1.0327759282  1.4309158079  0.8722173721 -0.6617710346 -0.1257547895
##  [171] -0.6145742639 -0.3010607103  0.2198734320  0.1996756382  0.0186433715
##  [176] -0.1643792794 -1.3337025118  0.4226263112 -1.1967343875  0.1916741752
##  [181] -0.6928301454  0.4662417450  0.8171058606  0.6833098821 -0.4789921632
##  [186]  0.4331272165 -1.8381045816 -0.0709436513  0.4480290497 -0.2220408424
##  [191] -0.4318281263  0.8594424324 -0.1844406573 -0.1567897629 -0.9268766695
##  [196]  0.4877335252  0.3876320032 -0.9917445501 -0.8213965541  0.0645758722
##  [201] -1.4821658860  1.5212796848  0.4021953173  0.4689013743  0.9198365552
##  [206]  0.7643808630  0.1294212900 -0.6864295365  0.3907704121 -0.7241929538
##  [211]  1.5696100083 -0.2203031264  0.2874927697 -1.8798803445 -1.9346864185
##  [216]  0.6172457513 -1.1823517668  0.2773604760 -0.7069460975  0.4202898314
##  [221]  0.7370080968 -1.1303335197 -0.8575507879 -0.4357351238  1.0592132876
##  [226] -2.2036090094  1.4958052410  1.4576681225 -1.0047978851  1.1809973196
##  [231]  0.8312496536  0.0508749710  0.6781790355  0.4520264136 -1.5091969877
##  [236]  0.7572681141 -1.1522868140  0.1407814287 -1.5417522340 -1.9511909754
##  [241] -0.5931304750  0.8988958457  1.7240639103  2.2319001286  0.8631213269
##  [246] -1.0247467078  0.2933962980  0.9249434729 -0.8064497687 -2.5884463730
##  [251]  0.2374628602  0.4111172435 -1.7894214213 -1.7654280331  0.0099307382
##  [256] -1.9918057459  0.8044112383 -0.9239138974 -0.6722796784 -0.8916962563
##  [261] -1.0200260355 -0.2577720884  1.2259490791 -0.0692410466 -0.0659810210
##  [266] -1.2346822040 -0.1423594181 -1.3011345991  0.4937206138 -0.5145543444
##  [271]  0.8405483788 -0.7562788356 -0.6646167615 -0.9799399367  0.8262437569
##  [276]  1.2730790248  0.1233055430  1.2754496659  1.5185400061  0.6272251689
##  [281] -0.7795571818  0.7509583080 -0.6914827691 -0.7341786030 -0.4585326846
##  [286]  1.1663938105 -0.3817417875 -2.2167235804 -0.8846723340 -0.3825421432
##  [291] -1.7509973434  0.3300039111  0.0929884710 -0.2599134085  1.3888925666
##  [296]  0.5072339166 -0.7616651505  0.0266299462  0.1033246837 -0.1490354886
##  [301]  1.2241015342 -0.2025701718  1.0908870321  1.1114935731  0.0698455521
##  [306] -0.7533738703  0.6220389124  1.4673809224  0.5635484257  0.4723442868
##  [311] -0.8634140755 -1.7865175490 -0.7893504106  0.8046858016  0.4524406107
##  [316]  0.7464984248 -0.7863203934 -2.0226908781  0.1276969714  1.0004373367
##  [321] -0.7483069963  0.4008536005 -1.5116957453 -0.2249996001  1.2060799584
##  [326] -0.4985889372  0.3810224997 -0.0428469773  0.3090283842 -0.8172867988
##  [331] -0.4608227086 -1.4949539138 -0.6594352556  1.7543202184 -1.6793996708
##  [336]  1.0974855087  1.7014381449 -0.3547379706 -1.7742512064  0.0941318033
##  [341]  1.1244239753  0.4432141709  0.4670896737  1.2267040217  0.7071602461
##  [346] -1.1959200021 -0.1614101935 -1.1885428974 -0.5063214390 -0.8850071934
##  [351]  0.3998326512 -0.0019430731  0.5536498400 -0.1920494389  0.5770923852
##  [356] -0.0520345216  0.9389232304 -1.9578796673  0.0096737842  1.4160626531
##  [361]  1.0438814082  0.1588307749  0.7031326060 -2.0812927669  0.9595926497
##  [366] -0.8444254547  0.4737724918  1.1355251191 -0.3649238743 -0.1091150894
##  [371] -0.3088327512  0.0886467873 -0.1774425794 -0.1784837597  1.7444381777
##  [376]  0.7685957298 -1.6034959251 -0.0816169705  0.5861656345  1.7707997542
##  [381]  0.7906693465 -0.0334779753  1.4760690083 -0.0433515009  1.7443051447
##  [386] -0.0022007149  1.6172260990  0.6301808056 -0.2991095490 -0.9711533888
##  [391] -0.6633785101 -0.4665803427 -0.8066892099 -1.0084734480 -1.3456688602
##  [396] -2.0161907561 -1.0806540074 -0.4173162607 -0.3185850796 -0.7061600400
##  [401]  0.4652504609  0.5712848237  0.1681265615  0.5216191296 -0.4537339051
##  [406] -0.1216055887 -0.7921473797 -0.6368839488  1.6649787782  0.6422241176
##  [411] -0.2974221477  0.9925992197  1.0283798351  1.4996216499 -0.8144706775
##  [416]  1.4365394767  0.9577499618 -0.5251349883  0.5431696525  0.8961808768
##  [421]  0.4568925956 -0.3682627501  0.4986145703 -1.5635054571 -1.4563425063
##  [426] -0.1750078941 -0.9758567874 -0.2828992203  0.4850523593 -1.2099302738
##  [431]  0.0172460899  0.7008571499 -0.4017022609 -0.4872757606 -0.7643624754
##  [436]  2.9722337957 -0.4357854464 -1.3519795359 -0.5286888614 -0.0602794845
##  [441] -0.0322244141  0.5529010071 -0.2596606369 -1.0140034328 -0.8645276131
##  [446]  0.1104008650 -0.0245257380 -2.5154849502  0.4437548826  1.5621844011
##  [451]  0.9718089383  1.5874742819 -1.8453086345 -1.2710747667  2.0389922869
##  [456] -0.7291493284  0.9344735664 -0.7532493228 -0.6844806407 -0.5947319693
##  [461] -0.1241223119 -1.3901402652 -1.2355653431 -1.2850079229 -1.0522533931
##  [466]  1.3649660202 -1.0772118019  0.6753815099  1.0755013649 -0.2767407318
##  [471] -0.6467514453  0.1025856330 -0.0725196097  1.6197123055  0.6877519983
##  [476]  0.2349295959 -0.4305282019  0.6380391455 -0.2172264688  0.0204943089
##  [481] -1.9133578396 -0.5261911440  0.4162826182  0.4576278624 -0.2756396249
##  [486] -0.3538823460 -0.9017625606 -1.0521452433  0.8326266409  0.0564111595
##  [491]  0.8387086521  0.3446117784 -1.0886058315 -0.5278206285  3.0611370136
##  [496] -0.5202138354  0.2482442006  0.0469695189  0.2494495962  1.0418430009
##  [501] -0.3383764950  0.2012877269  0.6027117309 -0.2250181483 -0.0101135895
##  [506] -0.1935972440  0.0091881369  1.4418992692  0.1746938235  0.4720826844
##  [511]  0.9005781938  0.3949221350  0.9021692718 -0.9260771266  0.0662943077
##  [516] -0.6370678492  1.4870186483 -0.0090184877  0.0845431672 -0.2098562556
##  [521] -2.5036769194  1.4237702309  1.4918134003  0.7346850668 -1.3333893734
##  [526]  2.6028246005 -0.1052401737  0.7622441275  0.3788467113  0.6014733479
##  [531] -0.2687536643  0.0835429455  0.5813876499 -0.9176215921 -0.7470990221
##  [536]  0.3049816889 -1.7680120105 -0.5072763227 -1.2132255790 -0.4944860155
##  [541] -0.5873063392 -1.3968130376 -1.5906045714 -0.5749474109  1.4373956232
##  [546]  0.3348121930  0.5997799292  0.8724496228  0.6492566066 -1.5692289781
##  [551] -0.2272341268  0.3050755297  1.5908550341  0.4553800876  2.1060241303
##  [556] -0.3114366799 -1.6365935260 -0.9962428977  0.6997492784 -0.0465505198
##  [561]  0.7268223277  0.1777760800  0.1606291843  0.8079510691 -0.5397537724
##  [566]  0.1166549203  0.6853801966  0.7670202707 -1.6353784060 -0.1257245877
##  [571] -0.2067687995 -0.6994230875 -0.3114883311  0.3858640623  1.0627126466
##  [576]  0.2002994460  0.7429980390  0.2395888921 -1.1453340192 -1.3786902598
##  [581]  0.5807347497  0.1285419404  0.6937782162  0.0174380511  0.0122894550
##  [586]  0.3818023726  1.0882034685 -0.2498993702 -0.2870610381  0.1523210727
##  [591] -1.4472605632  0.6478980619 -0.2644330355 -0.8604851908 -1.4531934290
##  [596] -0.2919261012  1.5717719491  0.6209706838 -0.3314936358 -1.0209971530
##  [601]  0.7336471191 -1.6209314027  0.2723195903 -1.0890948341 -0.5554394595
##  [606] -0.0056642758 -0.4981918347 -0.1493320618  0.6033283189  0.3430171108
##  [611]  0.2615996162  1.2568795039 -1.3194110498 -1.1459946892  0.8277236743
##  [616]  1.7614429611  0.2262328843 -0.9193135930  0.9737505996 -1.6811863185
##  [621]  2.4698769443 -0.2730456758  0.0992974118  0.4581428697 -0.2727818448
##  [626] -2.5140605371 -0.0116159512 -1.0567910136 -1.1639439434  1.0285357360
##  [631]  1.0287170046 -0.8163606178  0.4816647425  0.0125586535  0.6925387457
##  [636]  0.6491578354 -0.6807691864  0.1166537267 -0.3722781150 -0.4135713750
##  [641] -0.4941285461  1.2707555666 -0.8337837221 -0.2893695156  0.5371017167
##  [646] -0.3535333540  0.3611289460 -0.3673739928 -1.3080710336  0.3637817706
##  [651] -1.1572687441 -0.3872381630 -0.1758150260 -0.2159947164  0.1258227500
##  [656]  0.0438984650  0.5918778235  0.6706176848  0.5668216473 -0.4052435388
##  [661] -0.2615153397  1.2302738695 -0.4870056072  1.5697594279  0.6234854986
##  [666] -1.0273102110  1.8278200763 -1.0857039312  0.4080973394 -0.6008438129
##  [671] -1.0508209728  0.3579827422 -1.5325201664 -0.7177814655 -0.3144420588
##  [676] -2.6874894522 -1.2119780255  0.3346220516  2.1667085810  0.3637384635
##  [681]  0.7810413139  0.0012131688 -0.0729512571  0.3631191674  0.3984919384
##  [686] -0.0532299482  0.6510682754  0.5778796539  1.0688866360 -0.5411263211
##  [691] -1.3895590969  0.7888205558 -0.9058457129 -0.6309721328 -0.8332521517
##  [696]  0.0958541542  2.6330148016 -1.2020290219  1.7014891696  1.8550171445
##  [701]  0.9284096119 -0.2077142011 -1.0842780211 -1.4325740597  0.0805144484
##  [706] -1.6725917243 -0.9064265391 -0.3811321081 -0.3557082015 -0.6706446033
##  [711]  1.1977508750  1.0960182069  1.1522197235 -0.1379864824 -1.3182646301
##  [716] -1.0727799604  0.0109889535  0.1279865348  0.9208284135 -0.0012039422
##  [721] -1.6856605498 -1.6356278460  1.7214317019 -0.0977509910  0.3378791914
##  [726] -0.6112790396 -0.1910422663  0.0692944488  0.0963508398  0.1705435148
##  [731] -0.9348166056  0.1461115267 -0.7098149013  0.2674086747  1.1960925788
##  [736] -0.3402788338 -1.5903463770 -1.0719947144 -1.5483922891  1.1681143229
##  [741] -1.7985269736 -1.7281240505 -1.0196614706 -0.0568615308 -1.7869622102
##  [746] -1.1284217244  0.2722799504  0.9189133969 -0.7090647573  0.6003000410
##  [751] -1.5283093415 -0.2435600396 -0.4933806065 -0.1874864700  1.0143364639
##  [756] -0.6948941281 -0.5147438541 -1.1364128230  0.3553081904 -0.3773619206
##  [761] -0.0345682978  0.1083605979  0.5897290249  0.9444304224  0.8517425831
##  [766]  0.2379175295 -0.2150367051  1.1301200535  0.4487671961  0.7190443099
##  [771] -0.9667292013  1.0293226274  0.9913922770  1.4149251613 -0.1931119835
##  [776]  0.4439630435 -1.2939106933 -0.0950448014 -1.0530120040 -0.7591416907
##  [781]  0.9314736068  1.2139963558  1.1620938375  0.7098992109 -0.7908066361
##  [786] -1.3306966579 -0.5199056201 -0.5368424459  0.5620251460  1.5720794533
##  [791] -0.1850487624 -0.3736567400  2.0821118684 -0.3616935808 -1.2698788788
##  [796]  0.2082778930 -2.1340530176  0.3415775048 -0.2368577635 -1.6899524845
##  [801] -0.3329912726 -1.9723748662  0.3234789665 -1.1874700658 -0.0414601848
##  [806] -0.3775270438 -0.4870439375  0.7499905947 -0.5201677877 -0.9267980992
##  [811] -1.6319817841 -0.6357281968  0.0343794346  1.1970521476  0.8554260300
##  [816] -0.4301166918  0.9088851427 -0.1427528352 -0.3426432103  0.7550069793
##  [821]  0.6360370062 -0.8365801441  0.7614067014  0.5127185952 -0.2592356033
##  [826] -2.0394354172 -0.6523846497 -1.0285787494 -1.3977404781 -0.6230123821
##  [831]  0.6240424981  0.5647873335  0.5198905630  0.1334081368 -0.1489852536
##  [836]  0.5172161804 -1.6801112757  0.5673308435  0.2142074615 -0.7835945150
##  [841] -0.4563144734 -0.5214070441  0.1136650780  0.6822445933 -0.9198259829
##  [846]  0.1424297349  0.5696618252  0.5644320995  0.0993191743  1.2508426731
##  [851]  0.6506930139  1.9778310592  0.3874324167  1.2123943436 -1.5083062011
##  [856]  2.9107926616  0.5222019766  0.9849095062 -1.0832487521  0.6344751136
##  [861] -0.5580882248 -0.7053384417  1.1247785312 -0.1749895738  2.0893793288
##  [866] -0.2994414285  1.9086849859 -1.1841349494 -0.0402140535 -1.2630112827
##  [871] -0.4687769756 -2.6121945314 -1.8944673628  0.0476855538 -0.0593793415
##  [876] -0.5024713672  1.6095320803  1.0223939202 -0.7003069091  0.4709446716
##  [881]  0.9478946550  1.0686200368  1.3534243729 -0.3586898812 -0.2695721761
##  [886] -0.7578072027  1.8425644370  0.1444753795  0.0273664554 -0.0534053009
##  [891]  0.1867027956  1.1800751211  0.0486298890  0.8378754533 -0.3652152688
##  [896] -0.7561634274  1.1417571749  1.4866101877  0.2500777216 -0.2022458993
##  [901]  0.9174838695 -0.7094992637 -1.1646116943 -0.8202649416 -1.0620135613
##  [906]  0.0954991441 -0.3759500415  2.2045242637  0.9802237049 -1.0892271611
##  [911]  1.5332721575  0.4796330678 -1.4617102097  1.7039373024  0.3876100715
##  [916]  0.1883170355  0.9937509846 -1.2758325351  0.1802650095 -1.4314175550
##  [921] -0.8225903967  1.2933023113  0.1225358193 -3.0733699668  0.3340761360
##  [926]  2.3275984668  1.8490142978 -0.0468790548 -0.1032268618 -0.6468579687
##  [931] -0.3252756209  0.3729313419  0.8036420754 -1.4038757458 -0.7264375332
##  [936]  2.2957840344 -0.9750621821 -1.0327743562 -0.3601287806  1.4104336601
##  [941]  1.1665121221 -0.3481151064  2.3904092260  1.0574106824  1.3345350861
##  [946]  1.3129740101  0.0397543091  0.7751723361 -0.3404241340  1.0619885004
##  [951]  0.5966331256 -0.4669852096 -0.1062833415 -0.4232619373  0.0461574419
##  [956]  0.9725393953 -1.5342832174 -2.1679785052  0.1197959656  1.1843509707
##  [961] -0.5494044003  0.2514280487  0.1883532174  0.0788117086 -0.2163014217
##  [966] -0.0005669877  1.1387840369  0.2072201847  0.2923619437 -0.1758127217
##  [971] -1.8735818099  1.5641130417 -0.3691548941 -0.7544639836 -1.0437928004
##  [976] -0.4384926043 -0.4644900213 -1.7159094134 -0.1048763558  0.7061077808
##  [981] -0.5961921921 -1.1396608099  0.4053174340  0.2756589827  0.6279100878
##  [986] -0.5892602332  0.6853238730 -1.3839449775 -0.5367592390  0.0530129204
##  [991] -0.1730516881 -0.8941764878  0.0462334250  1.2606150791  0.7529683081
##  [996]  2.9195292218 -1.5063759747  0.0391252864  1.7064865971 -1.7661102359
norm2
##    [1]  9.948276 12.163696  8.819748 10.944762 10.898058  8.135216 11.107396
##    [8] 11.323815  9.941029  8.887388 10.915128 11.321554 11.518248 10.750503
##   [15]  8.647394  8.954257 10.410829 11.858763  9.466488  8.536995 10.190012
##   [22] 10.408860  8.949151  8.522930  9.423325 11.771174  8.527315 10.219849
##   [29]  9.257174 10.422982  9.476307 11.677229 11.211093  9.678932 10.485231
##   [36] 10.819017  9.558881  9.631691 10.563416  9.890096  9.704789 10.613707
##   [43]  9.430702 10.269874  9.262989 11.407047  9.307166  9.626291 10.234092
##   [50]  8.882853 10.824231  8.925522 10.982182 10.265856 11.674932  9.990992
##   [57] 10.319160 11.914758  9.732233 11.806150 10.082896 10.644119 10.575735
##   [64]  9.799152 10.254316 10.645296 10.545178 11.278592 10.724516 10.732058
##   [71] 10.731427 11.122003  9.376824  9.845212  8.552222 10.897432 10.444285
##   [78] 10.782920  9.510192  9.793332  9.594531  9.527016  9.125120 10.456592
##   [85] 10.666857  8.794739  9.514134  9.081589 10.218690 12.040877 10.260566
##   [92]  9.349595 10.568189 10.395534  7.696051 11.329438  9.341831  7.840997
##   [99]  9.399502 11.722518  9.843990 12.089073  9.479946 10.914326  9.561438
##  [106]  8.942590 10.227253  9.710973  9.841150 10.858670  9.867496  8.940652
##  [113]  9.545270 12.001449  8.626855 10.421092 10.401739  8.715539  9.319209
##  [120]  8.661202  9.100224  8.897765 10.251637 10.152648  9.920699  9.526427
##  [127] 11.218923 10.207086  7.770597 10.846758  9.395290  8.693164 10.456159
##  [134]  8.789830 11.424898  9.926471 10.797344 11.417498 12.524391  9.751251
##  [141]  9.978374  7.891222 11.004576 11.299371  9.083717 13.009522 10.380497
##  [148]  9.752563 10.399435  9.490971 11.362169 11.008205 11.738653 10.572146
##  [155]  9.793968  9.609511 10.047727  8.632593 10.110767 10.002055 10.226195
##  [162] 10.902612 10.722111 11.086446  9.109325  8.228692  9.058437 12.013636
##  [169]  9.825228  9.937820 10.976000  9.940104 10.508728  9.452929  9.960333
##  [176]  9.019191 10.069804  9.310953 10.844389 10.552114  9.715789  9.787242
##  [183] 11.376699  9.765301 12.497447 11.362513  9.676091 10.158701 10.024345
##  [190]  9.437416 10.818715 11.881402 10.180570 10.098744 10.302004 10.925558
##  [197] 10.606201  8.839156 10.088934  9.780708  9.499260  9.623739 10.234166
##  [204] 11.088513 10.831318 10.478830  7.485107 10.710847  9.009730 10.936796
##  [211]  9.589097 11.817237 10.163530  8.429001 10.397659  9.800690 11.022428
##  [218]  9.042078 11.529576 10.057601 11.310458  9.359857  8.740208 10.066611
##  [225]  9.397493 11.045549  9.170213 10.161519  9.964926  8.254145  9.136252
##  [232] 12.166400  9.403799  9.929232  9.446976 11.656680  9.910382 11.983422
##  [239]  9.726324  9.920432  9.531518 10.863005  8.261900 10.267480  9.458702
##  [246] 10.373638  9.655462 10.796685 10.695136 10.894395 11.134286 10.022624
##  [253]  9.889039  9.342400  8.267019  9.385334  9.540306  9.396257 10.692946
##  [260]  8.784625 11.996686  8.773164 11.754378  8.736269  8.019463  9.991673
##  [267] 10.865905 11.552337  8.720405  9.500327 10.596511 10.873587 10.137969
##  [274] 10.219320 10.557874  9.406421  9.593425  8.883547  9.381344  8.294288
##  [281] 10.691162  9.553701 10.636095  9.302347 10.799377  9.883279  9.524834
##  [288] 10.837519 10.744438 10.651131 11.271702  9.487992 11.775936  9.869324
##  [295] 11.110943 10.553613 11.272973 11.556832  9.991418  9.740758 10.049985
##  [302] 10.633825  9.201965 10.450884 12.279631 10.394670 10.806329  9.102899
##  [309] 10.560208  9.593789  7.395130  8.666013 11.032284  8.296718 11.981853
##  [316] 10.899648  9.069443 11.405492  9.739491 10.157935 11.249953  9.882826
##  [323]  9.318222 10.963687 10.550848 11.055994 10.758047 10.512853  8.408177
##  [330] 10.660018  9.720702 10.845141  9.554219  9.161763  9.958241 12.254978
##  [337] 10.657036  9.918270  9.924527  7.574575 10.607669  9.250059  9.619617
##  [344] 11.608517  8.920830  9.385666  9.298827  8.849471  8.339754 11.349597
##  [351]  9.363252  8.846702  8.162738  9.551480  9.004981 11.232211 10.991109
##  [358] 10.046025  9.913354  9.603297  9.525753  9.267826  9.977271 10.090533
##  [365]  9.842186 10.840766  9.957088 11.863525  9.872413 10.068601 10.984884
##  [372] 10.752348  9.681209 10.061098  8.174411 10.302437 10.119875  9.802552
##  [379] 11.275130  9.153088 10.635725  9.074956  9.939562  9.657726  9.812879
##  [386] 10.590363 10.809091  8.366105  7.960625 10.897317  9.002166  9.315946
##  [393]  8.623090 10.610404  9.735413 11.276083  9.860191  9.603593  9.429134
##  [400] 10.524968 10.456881 11.339936 10.058593 10.099852 11.499499  9.729655
##  [407]  9.122494 10.933970 11.277847 10.462933 10.492217  7.487085 11.285919
##  [414] 10.586555 11.528887  7.824589 11.071187 11.675790 10.857655 10.849859
##  [421]  8.971114  7.607781 10.094632  9.440730 10.657409 10.629557  9.788421
##  [428]  9.061952  9.853906  8.691746 11.629131  9.282407  9.890552  9.233268
##  [435]  8.306849 10.540530 10.970045 10.390963  9.400900 10.590132 10.752700
##  [442] 11.139432 12.665945 10.326905 11.262124 11.663352 10.905259  9.950040
##  [449]  9.063206  9.889878  8.404935  9.529891  9.337338 10.709908  9.125200
##  [456] 13.205049  8.810273 12.268688  9.213220  9.922637  9.522659 10.517790
##  [463]  9.684902  9.372309 11.278405  9.148838 10.216221 10.431221 10.840791
##  [470] 11.753057 11.255113 10.341582  9.520410 10.956360 10.486762  9.757188
##  [477]  8.307407  8.727377  9.925842  9.309910  9.486240  9.694699  9.790287
##  [484] 11.133599 10.212739 10.014077 11.025378  8.905810  9.232957 10.081696
##  [491]  8.544378  9.608138 10.597089  9.404218  7.408759  9.468710 11.481543
##  [498] 10.578556  8.897946  9.963145  9.253654 10.387465  9.648128 10.771421
##  [505]  8.568112 10.613385  8.888850  8.162502 11.199039  9.996740 12.477569
##  [512]  8.258754 10.334771  8.869754 12.113785 11.057581 10.918428 10.545565
##  [519]  9.105268  8.414915  9.291188  7.496162 11.367490  9.579464  9.338355
##  [526] 10.932385  8.754147 11.215517 13.178355  9.244679  9.147233 10.452979
##  [533]  9.748667  9.216779 10.623509 10.501942  9.861008  9.904007 10.857053
##  [540]  8.637152  8.726278  9.769214  9.730820 10.170390 10.158803  9.861938
##  [547]  8.468394  8.104941  9.941382  9.738015 10.771735 10.093447  8.358231
##  [554]  9.754714 10.421211 12.209076  8.585579 12.153251 10.707281  9.981852
##  [561]  9.931115 11.413703 10.913589 11.016442 10.637868 11.356440 10.491347
##  [568]  9.888038 11.157792  9.722681 10.013410 10.493192 10.096202 11.794434
##  [575]  8.951012  9.289357 11.484427 10.125410 11.594025  9.369598  8.311733
##  [582] 10.103256  7.608525 11.408894  9.358822  9.511506 10.623127 11.378543
##  [589] 10.838793  8.863748  9.919456  9.336947  9.211806 10.896942 11.340517
##  [596]  8.482970 10.535602  9.060463  9.891079  9.622834 11.500477 11.840888
##  [603] 10.094857 12.372186 10.419600 10.334186  9.061219  9.630204 10.440344
##  [610]  9.183989  9.634410  9.042377  9.157706 10.394512  9.002753 10.870891
##  [617]  9.837639 10.379416 10.044825 11.605064 10.599628 11.237375 10.092777
##  [624] 10.588007 10.089095 10.131328 10.278458  9.029272 10.890662  9.623127
##  [631] 10.648122  8.766938  8.638013  9.766397 10.987401 10.821301 10.427873
##  [638] 10.891152 11.963020 11.044731  9.496986  9.390699  9.460603 10.452216
##  [645]  8.655304 10.899633  9.091496 10.950325  8.935150  9.347721  9.759105
##  [652]  9.806917 10.419202  9.316791  8.405424  8.660176 10.220524 11.193064
##  [659] 11.327107  9.795964 10.825017  7.982684 10.500641 11.157187 10.132662
##  [666]  8.509193 11.029623  8.810498 11.238836 10.872249  7.142274 10.058086
##  [673]  8.860358  9.839060 11.057659 10.426494  9.005905  9.851915 10.980028
##  [680] 10.615576 10.420276 10.150718 10.906499 11.045269 10.774920 10.537880
##  [687] 11.481506 10.000303  9.756742  9.702010 11.022031 10.356500  9.498634
##  [694] 10.576357 11.197791  9.302257  8.257161 10.815616  9.484029  9.141883
##  [701]  9.911200 11.410378  9.796036 10.909955 10.030075  9.400965 12.599545
##  [708]  8.426967  8.834313  9.804977 10.268076  9.448291  9.887729 10.137539
##  [715]  9.497092  8.026460  9.942858  8.708056 10.489505  9.648161  9.264595
##  [722]  9.996470  8.663241 10.780583  7.388197 10.680455  9.588706  9.277117
##  [729]  8.993144 11.030792 10.431592 10.345116 10.220336  9.241649 11.210742
##  [736]  9.573617 11.304671  9.189307  9.028193 11.481862  8.958823 10.959369
##  [743]  8.124607  9.989210  8.568909 11.138042  9.366823 10.229336 11.363648
##  [750]  9.860179 11.083406  8.566922 11.008528 11.559699 11.196757  9.956713
##  [757]  9.063840  7.237665  9.474302 11.276967  9.953225  9.982308  8.079718
##  [764] 10.526785  9.422953  8.620897 10.810356  9.912489 10.725213 10.084389
##  [771] 12.173134  9.022174 10.023979  9.975222 10.364137  9.670557 10.189481
##  [778] 11.274997  9.554401 10.833990 10.940675  9.922848 11.385850  8.756671
##  [785] 10.220146 10.354981  8.518388  9.233524  9.868788  9.590497 11.691341
##  [792]  7.857701  9.034418 10.237049 10.299306 10.370190 10.876228 10.095106
##  [799] 10.756590  9.906762 11.440314  9.981467 10.753127 10.336342  8.914172
##  [806]  8.135846 10.331478  7.597903  9.616005 10.315670 11.024777  8.977120
##  [813]  9.813836  8.838796 10.888602 10.045735  8.239081 10.315872 11.063848
##  [820]  8.751219 10.422128 11.061841  9.258023 11.560810 10.554974  8.497895
##  [827]  9.439400  9.241949  8.662781  9.354413  9.139751  9.555545  9.150915
##  [834]  9.766773  9.695013 12.357236  9.905712 10.461605  9.270273  9.651428
##  [841]  9.416102 11.365693 10.880898  8.391897  8.535954  9.474026 10.043593
##  [848] 10.983689 11.324974 10.504750  8.268681  8.543686 10.572016  8.651964
##  [855]  9.455289  8.801291  8.933521  9.270596 10.665403  9.473695  8.546406
##  [862]  9.562108 10.263990 10.486225  9.118894 10.552861 11.624691  8.652364
##  [869] 11.159141  9.986058 11.690917 10.968144 10.249949 12.015836 10.130472
##  [876] 11.034745 10.178148  9.712343  9.277444  9.673382  9.228727 10.067155
##  [883]  8.937636  9.962034  9.445517 10.439345 10.400411  9.367148 10.139572
##  [890] 10.463356  8.679513  8.888739 10.244264  8.685877 10.816662 11.132840
##  [897]  9.474491 10.027643 10.700894 10.345581  9.618333  8.072834  8.617708
##  [904] 12.115029 10.454022  8.765532  8.847585  9.846499  9.173886 11.654753
##  [911] 10.366697 10.260558  9.117571  9.531956 10.443576  8.858401 10.127673
##  [918] 10.135911 12.523301 10.650774 10.359538 10.610071 10.988937  8.950384
##  [925]  9.848223  8.286035 10.115613 10.499035  8.693637 10.026031  9.768022
##  [932]  9.865396 11.245864 10.081377  8.543472 11.434158 10.471688  9.453140
##  [939]  7.361174  9.673667  9.709780 10.021821 10.345676  8.312130  9.208941
##  [946] 10.373478 11.016642  8.891887  7.661649 10.084583  9.410351  8.493501
##  [953]  9.438973  8.823859  8.639536 11.068439  8.875457 10.487587 11.271739
##  [960] 11.517721 11.932771 10.669382 10.855738 10.221976  9.588552 10.717781
##  [967] 10.631780  9.537711  8.985887  9.458319 11.695367 11.676840  9.620742
##  [974]  8.852039  9.911721  9.137656 11.344402 10.917665 11.234908 10.077747
##  [981] 11.284789  9.810855 11.904143  9.427968  8.585984  9.215765  9.929042
##  [988]  9.965898 12.511322 10.015578 11.047968  9.035464 11.439171 11.190435
##  [995]  9.624716  9.885982 10.587676 10.511487  9.724436 10.607297
plot(x=norm1, y=norm2, xlab = "X", ylab = "Y", xlim = c(-3,3), ylim = c(0,10), col=sample(colors(),100))

#colors() es una función en R que devuelve un vector con los nombres de todos los colores predefinidos en R (hay más de 600 colores).

#sample(colors(), 100) toma una muestra aleatoria de 100 colores del vector devuelto por colors().

#Esto significa que cada punto en el gráfico tendrá un color aleatorio seleccionado de esta muestra de 100 colores.

#reajusto límite de eje Y, ya que con 10 se queda corto. Obtengo antes el valor máximo de la variable

max(norm2)
## [1] 13.20505
plot(x=norm1, y=norm2, xlab = "X", ylab = "Y", xlim = c(-3,3), ylim = c(0,14), col=sample(colors(),100))

## GGPLOT2

library("tidyverse")
## Warning: package 'tidyverse' was built under R version 4.2.3
## Warning: package 'ggplot2' was built under R version 4.2.3
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'tidyr' was built under R version 4.2.3
## Warning: package 'readr' was built under R version 4.2.3
## Warning: package 'purrr' was built under R version 4.2.3
## Warning: package 'dplyr' was built under R version 4.2.3
## Warning: package 'stringr' was built under R version 4.2.3
## Warning: package 'forcats' was built under R version 4.2.3
## Warning: package 'lubridate' was built under R version 4.2.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Id<-c(seq(1:30))
Nota1<-c(round(runif(30, min=0, max=10),2))
Nota1
##  [1] 5.12 1.80 0.51 4.93 4.43 3.68 5.98 4.10 2.33 8.38 7.90 3.27 4.69 6.62 8.55
## [16] 1.97 4.15 7.89 4.09 0.11 5.96 5.98 1.67 0.86 1.29 8.42 6.42 4.25 8.55 5.33
Nota2<-c(round(runif(30, min=0, max=10),2))
Especialidad <-factor(c(1,1,2,3,2,3,1,2,3,1,1,2,3,1,2,3,1,2,3,3,2,1,3,1,1,1,1,2,3,2), levels = c(1,2,3), labels = c("Análisis clínicos", "Farmacia hospitalaria", "Microbiología"))
Especialidad
##  [1] Análisis clínicos     Análisis clínicos     Farmacia hospitalaria
##  [4] Microbiología         Farmacia hospitalaria Microbiología        
##  [7] Análisis clínicos     Farmacia hospitalaria Microbiología        
## [10] Análisis clínicos     Análisis clínicos     Farmacia hospitalaria
## [13] Microbiología         Análisis clínicos     Farmacia hospitalaria
## [16] Microbiología         Análisis clínicos     Farmacia hospitalaria
## [19] Microbiología         Microbiología         Farmacia hospitalaria
## [22] Análisis clínicos     Microbiología         Análisis clínicos    
## [25] Análisis clínicos     Análisis clínicos     Análisis clínicos    
## [28] Farmacia hospitalaria Microbiología         Farmacia hospitalaria
## Levels: Análisis clínicos Farmacia hospitalaria Microbiología
#definir como factor para que R entienda que se trata de una variable categórica y esos números corresponden a una categoría, a la que podría asignarle un nombre

datos<-data.frame(Id, Nota1, Nota2)
datos
##    Id Nota1 Nota2
## 1   1  5.12  5.88
## 2   2  1.80  8.79
## 3   3  0.51  2.25
## 4   4  4.93  1.14
## 5   5  4.43  8.58
## 6   6  3.68  0.14
## 7   7  5.98  1.27
## 8   8  4.10  3.74
## 9   9  2.33  0.83
## 10 10  8.38  0.37
## 11 11  7.90  8.13
## 12 12  3.27  6.12
## 13 13  4.69  7.96
## 14 14  6.62  1.82
## 15 15  8.55  0.06
## 16 16  1.97  4.23
## 17 17  4.15  2.70
## 18 18  7.89  6.35
## 19 19  4.09  4.25
## 20 20  0.11  2.96
## 21 21  5.96  1.76
## 22 22  5.98  8.15
## 23 23  1.67  5.30
## 24 24  0.86  5.84
## 25 25  1.29  7.98
## 26 26  8.42  9.14
## 27 27  6.42  9.54
## 28 28  4.25  1.11
## 29 29  8.55  8.84
## 30 30  5.33  9.10
print(ggplot(data=datos) + geom_point(aes(x=Nota1, y=Nota2), col="skyblue", size=1))

print(ggplot(data=datos) + geom_point(aes(x=Nota1, y=Nota2), col="red", size=1, shape=Especialidad))

#Shape-> definir la forma según una tercera variable, en este caso según la especialidad, variable categórica

#Guardar gráficos mediante código