Matrix from count of rows and columns of different excel file
I have 8 excel files. Each file has some number of rows and columns. I want to read 8 excel files in R studio and then create a matrix with number of rows and columns of each file. So the matrix should look like this
Name_of_Excel_file CountOfRows CountOfColumns
Excel1 100000 25
Excel2 50000 100
Excel3 10000 300
Excel4 3000 10
Excel5 80000 50
Excel6 50000 250
Excel7 40000 10
Excel8 20000 10
Could someone please help?
Solution 1:
I'll show you a probably long-winded way of getting your answer. I included commands for if you were actually loading the excel docs below:
# Load libraries:
library(readxl)
library(tidyverse)
# Read excel docs:
df1 <- data.frame(read_xlsx("df1.xlsx"))
df2 <- data.frame(read_xlsx("df2.xlsx"))
Then to simulate what you would do, I just created my own data frames that represent the excel docs to make it easy to reproduce:
# First "excel" doc:
x1 <- c(1,2,3,4)
y1 <- c(2,3,4,1)
df1 <- data.frame(x1,y1)
# Second excel doc:
x2 <- c(1,2,3,8)
y2 <- c(2,3,4,3)
df2 <- data.frame(x2,y2)
# Create variables for rows and cols each:
df1row <- nrow(df1)
df1col <- ncol(df1)
df2row <- nrow(df2)
df2col <- ncol(df2)
# Make data frames for each:
dfdims <- data.frame(df1row,
df1col)
df2dims <- data.frame(df2row,
df2col)
# Combine them:
all_dims <- data.frame(df1col, df1row, df2col, df2row)
all_dims
# Pivot so rows categorize docs:
pivot_dims <- all_dims %>%
pivot_longer(cols = 1:4,
names_to = "Excel Doc",
values_to = "Dims")
Should look like this when you print the pivot_dims function: