Making a data frame from vectors.

JORGE LUIS VILLALBA ACEVEDO

Making a data frame from vectors

Una gran ventaja de los data frames, es que R tiene diversas funciones para leer y guardar las tablas que representan, en archivos de texto, y otros formatos.

Gender=c("M","F","M","F","M","F")
 Height=c(1.83,1.76,1.82,1.60,1.90,1.66)
 Weight=c(67,58,66,48,75,55)
df <- data.frame(Gender,Height,Weight);df
  Gender Height Weight
1      M   1.83     67
2      F   1.76     58
3      M   1.82     66
4      F   1.60     48
5      M   1.90     75
6      F   1.66     55
class(df)
[1] "data.frame"
str(df)
'data.frame':   6 obs. of  3 variables:
 $ Gender: Factor w/ 2 levels "F","M": 2 1 2 1 2 1
 $ Height: num  1.83 1.76 1.82 1.6 1.9 1.66
 $ Weight: num  67 58 66 48 75 55

Sin variable cualitativa factor

Gender=c("M","F","M","F","M","F")
 Height=c(1.83,1.76,1.82,1.60,1.90,1.66)
 Weight=c(67,58,66,48,75,55)
df1 <- data.frame(Gender,Height,Weight,stringsAsFactors=FALSE);df1
  Gender Height Weight
1      M   1.83     67
2      F   1.76     58
3      M   1.82     66
4      F   1.60     48
5      M   1.90     75
6      F   1.66     55
class(df1)
[1] "data.frame"
str(df1)
'data.frame':   6 obs. of  3 variables:
 $ Gender: chr  "M" "F" "M" "F" ...
 $ Height: num  1.83 1.76 1.82 1.6 1.9 1.66
 $ Weight: num  67 58 66 48 75 55

Dimension de un data frame

dim(df)
[1] 6 3
nrow(df)
[1] 6
length(df)
[1] 3

Como extraer elementos de un data frame

df$Weight
[1] 67 58 66 48 75 55
class(df$Weight)
[1] "numeric"
df[3]
  Weight
1     67
2     58
3     66
4     48
5     75
6     55
df[[3]]
[1] 67 58 66 48 75 55
df[3, 2]
[1] 1.82
df[3, 2] <- 1.06
df
  Gender Height Weight
1      M   1.83     67
2      F   1.76     58
3      M   1.06     66
4      F   1.60     48
5      M   1.90     75
6      F   1.66     55

Body Mass Index (BMI)

df$BMI <- df$Weight/(df$Height)^2
df <- cbind(df, jorgeR=c(1,2,3,4,5,6))
df
  Gender Height Weight      BMI jorgeR
1      M   1.83     67 20.00657      1
2      F   1.76     58 18.72417      2
3      M   1.06     66 58.73977      3
4      F   1.60     48 18.75000      4
5      M   1.90     75 20.77562      5
6      F   1.66     55 19.95936      6

colnames() y rownames():

[1] "Gender" "Height" "Weight" "BMI"    "jorgeR"
[1] "1" "2" "3" "4" "5" "6"
[1] "Gender" "Height" "Weight" "BMI"    "jorgeR"
rownames(df) <- c("Jack","Julia","Henry","Emma","William","Elsa")
names(df)[3] <- c("Peso") 
colnames(df)[2] <- c("Altura")
df
        Gender Altura Peso      BMI jorgeR
Jack         M   1.83   67 20.00657      1
Julia        F   1.76   58 18.72417      2
Henry        M   1.06   66 58.73977      3
Emma         F   1.60   48 18.75000      4
William      M   1.90   75 20.77562      5
Elsa         F   1.66   55 19.95936      6

read.table("")

tabla <- read.table("DF.txt",header=TRUE);tabla
  Precio Piso Area Cuartos Edad Calentador
1  52.00  111  830       5  6.2         no
2  54.75  128  710       5  7.5         no
3  57.50  101 1000       5  4.2         no
4  57.50  131  690       6  8.8         no
5  59.75   93  900       5  1.9         si
class(tabla)
[1] "data.frame"
is.data.frame(tabla)
[1] TRUE
str(tabla)
'data.frame':   5 obs. of  6 variables:
 $ Precio    : num  52 54.8 57.5 57.5 59.8
 $ Piso      : num  111 128 101 131 93
 $ Area      : int  830 710 1000 690 900
 $ Cuartos   : int  5 5 5 6 5
 $ Edad      : num  6.2 7.5 4.2 8.8 1.9
 $ Calentador: Factor w/ 2 levels "no","si": 1 1 1 1 2