Mostrando entradas con la etiqueta t. Mostrar todas las entradas
Mostrando entradas con la etiqueta t. Mostrar todas las entradas

2020-12-21

How to create a symmetric matrix in R

Title

Problem

We want to create a symmetric matrix based on the following example.

Example

  V1 V2 V3 V4 V5
1  0  2  3  4  5
2  0  0  6  8 10
3  0  0  0 12 15
4  0  0  0  0 20
5  0  0  0  0  0
df <- structure(list(V1 = c(0L, 0L, 0L, 0L, 0L), V2 = c(2L, 0L, 0L, 
0L, 0L), V3 = c(3L, 6L, 0L, 0L, 0L), V4 = c(4L, 8L, 12L, 0L, 
0L), V5 = c(5L, 10L, 15L, 20L, 0L)), .Names = c("V1", "V2", "V3", 
"V4", "V5"), class = "data.frame", row.names = c("1", "2", "3", 
"4", "5"))

Solution

  • Adding the transpose of the matrix.
  • df + t(df)
  • Function lower.tri
  • We assign to the lower triangle of the matrix the transpose of the lower triangle.

    df[lower.tri(df)] <- t(df)[lower.tri(df)]
      V1 V2 V3 V4 V5
    1  0  2  3  4  5
    2  2  0  6  8 10
    3  3  6  0 12 15
    4  4  8 12  0 20
    5  5 10 15 20  0

    Using upper.tri would not create a symmetrical matrix.

    df[lower.tri(df)] <- df[upper.tri(df)]
      V1 V2 V3 V4 V5
    1  0  2  3  4  5
    2  2  0  6  8 10
    3  3  8  0 12 15
    4  6 12 10  0 20
    5  4  5 15 20  0
    

References

2019-01-18

Cómo transponer un data frame en R

Problema

Queremos transponer un data frame.

df <-
  structure(
    list(
      Country.Name = c("Country1", "Country2", "Country3"),
      `1997` = c(1L, 2L, 4L),
      `1998` = c(1L, 4L, 2L),
      `1999` = c(1L, 7L, 1L),
      `2000` = c(1L, 10L, 5L)
    ),
    .Names = c("Country.Name",
               "1997", "1998", "1999", "2000"),
    class = "data.frame",
    row.names = c(NA,-3L)
  )
  Country.Name 1997 1998 1999 2000
1     Country1    1    1    1    1
2     Country2    2    4    7   10
3     Country3    4    2    1    5

Solución

Empleamos la función t que transpone una matriz o data frame.

# Transpone todas las columnas menos la primer
df_transpose <- data.frame(t(df[-1]))
# Añadimos los nombres de las columnas
colnames(df_transpose) <- df[, 1]
df_transpose
     Country1 Country2 Country3
1997        1        2        4
1998        1        4        2
1999        1        7        1
2000        1       10        5

Entradas relacionadas

Referenciass

2019-01-16

How do I transpose a data frame in R?

Problem

We need to transpose a data frame.

df <-
  structure(
    list(
      Country.Name = c("Country1", "Country2", "Country3"),
      `1997` = c(1L, 2L, 4L),
      `1998` = c(1L, 4L, 2L),
      `1999` = c(1L, 7L, 1L),
      `2000` = c(1L, 10L, 5L)
    ),
    .Names = c("Country.Name",
               "1997", "1998", "1999", "2000"),
    class = "data.frame",
    row.names = c(NA,-3L)
  )
  Country.Name 1997 1998 1999 2000
1     Country1    1    1    1    1
2     Country2    2    4    7   10
3     Country3    4    2    1    5

Solution

The t function will return the transpose of a matrix or data frame.

# Transpose all but the firs column
df_transpose <- data.frame(t(df[-1]))
# Add column names
colnames(df_transpose) <- df[, 1]
df_transpose
     Country1 Country2 Country3
1997        1        2        4
1998        1        4        2
1999        1        7        1
2000        1       10        5

Related posts

References

2016-04-03

Cómo crear un histograma a partir de múltiples columnas en R

Title

Problema

Tenemos un data frame con múltiples columnas —en nuestro ejemplo 49— y deseamos utilizar todas las columnas como una única variable. También calcularemos la media de todas las columnas.

df <- read.table(textConnection("1.52 0.39 1.30 1.10 0.82 0.59 1.11 0.44 0.70 0.57 0.65 1.06 0.55 0.34 1.09 0.46 0.43 0.99 0.41 1.04 1.25 0.75 0.54 0.62 0.56 0.58 0.85 0.38 0.66 0.83 1.00 0.56 0.66 0.40 1.44 1.28 0.58 1.05 0.64 0.54 0.52 1.28 0.51 0.64 0.82 0.45 0.83 0.58 0.51"), header = FALSE)
    V1   V2  V3  V4   V5   V6   V7   V8  V9  V10  V11  V12  V13  V14  V15  V16  V17  V18  V19  V20  V21  V22  V23  V24  V25  V26  V27  V28
1 1.52 0.39 1.3 1.1 0.82 0.59 1.11 0.44 0.7 0.57 0.65 1.06 0.55 0.34 1.09 0.46 0.43 0.99 0.41 1.04 1.25 0.75 0.54 0.62 0.56 0.58 0.85 0.38
   V29  V30 V31  V32  V33 V34  V35  V36  V37  V38  V39  V40  V41  V42  V43  V44  V45  V46  V47  V48  V49
1 0.66 0.83   1 0.56 0.66 0.4 1.44 1.28 0.58 1.05 0.64 0.54 0.52 1.28 0.51 0.64 0.82 0.45 0.83 0.58 0.51

Solución

  • Histograma
  • Simplemente transponemos el data frame antes de crear el histograma.

    hist(t(df))
    

  • Calculamos la media
  • Tenemos dos alternativas: mean o rowMeans.

    mean(t(df)) # or 
    rowMeans(df)
    

    [1] 0.752449 
    

    Entradas relacionadas

    Referencias

    2015-09-28

    Cómo crear una matriz simétrica en R

    Title

    Problema

    Partimos de la siguiente matriz/data frame y deseamos crear una matriz simétrica.

    Datos

      V1 V2 V3 V4 V5
    1  0  2  3  4  5
    2  0  0  6  8 10
    3  0  0  0 12 15
    4  0  0  0  0 20
    5  0  0  0  0  0
    
    df <- structure(list(V1 = c(0L, 0L, 0L, 0L, 0L), V2 = c(2L, 0L, 0L, 
    0L, 0L), V3 = c(3L, 6L, 0L, 0L, 0L), V4 = c(4L, 8L, 12L, 0L, 
    0L), V5 = c(5L, 10L, 15L, 20L, 0L)), .Names = c("V1", "V2", "V3", 
    "V4", "V5"), class = "data.frame", row.names = c("1", "2", "3", 
    "4", "5"))
    

    Soluciones

    • Sumando la matriz transpuesta
    • df + t(df)
    • Función lower.tri.
    • df[lower.tri(df)] <- t(df)[lower.tri(df)]
        V1 V2 V3 V4 V5
      1  0  2  3  4  5
      2  2  0  6  8 10
      3  3  6  0 12 15
      4  4  8 12  0 20
      5  5 10 15 20  0

      Asignamos a la parte inferior de una matriz triangular superior, los valores de la parte superior. Con lower.tri obtenemos el triángulo inferior. Le asignamos la parte superior que transponemos previamente la matriz —t(df)—, por eso volvemos a usar lower.tri . Pues si usáramos upper.tri el resultado no sería una matriz simétrica.

      df[lower.tri(df)] <- df[upper.tri(df)]
        V1 V2 V3 V4 V5
      1  0  2  3  4  5
      2  2  0  6  8 10
      3  3  8  0 12 15
      4  6 12 10  0 20
      5  4  5 15 20  0
      

    Entradas relacionadas

    Referencias

    Nube de datos