Creating subset matrix from bigger matrix in R - r

I have a big matrix (i.e 303*32). I want to create take out some columns and make a new matrix in R. I am unable to find any solution. Any help would be appreciated.
ads # Matrix of 303*32
new_mat <- matrix(c("speed","gaps","time")) # speed, gaps and times are names of cols which I am trying to exclude.
By using this code i am only getting cols and rows name .

If you want to exclude columns by name, you need something like:
new_mat <- ads[, !colnames(ads) %in% c("speed","gaps","time")]

Related

R how to create a dataframe by adding columns

I am very very new to R....I have been using Python and MATLAB my whole life.
So here is what I would like to do. During each loop, I compute a column that I would like to add on to a dataframe.
Problem is that I do not know the length of the column. So I cannot create the dataframe to a specific length. So I keep getting an error when I try to add the column to the empty original empty dataframe...
# extract the data where the column 7 has no data.
df_glm <- data.frame(matrix(ncol = 11, nrow = 0))
for (j in 1:ncol(data_cancer)){
col_ele <- data_cancer[,j]
col_filtered <- col_ele[col_bool7]
# make new dataframe by concetenating the filtered column.
df_glm[,i] <- col_filtered
}
data_cancer_filter <- data_cancer[,col_bool7]
How can I resolve this issue?
I am getting an error at df_glm[,i] because the column is as long as col_bool7. But I want to learn how to do this without creating dataframe of exact size beforehand.
If I am understanding this correctly, you're looping through columns and taking the rows where col_bool7 is TRUE and putting it in another dataframe. dplyr filter() would be an efficient solution:
library(dplyr)
df_glm = data_cancer %>%
filter(col_bool7)

Switching the order of columns of a matrix in R

Suppose I generate the following fictional matrix
mat <-matrix(1:12,3)
Now I would like to rearrange the order of the columns from 1:4 to 4:1
Manually I could do this by.
Z <- cbind(mat[,4],mat[,3],mat[,2],mat[,1])
Now when the matrix becomes large with for example 30 columns, doing this manually will be a tedious process.
Does anyone have a suggestion to rewrite the order of the columns with for example a loop?
We can use indexing i.e. create a sequence (:) from the last column index - ncol(mat) to 1 and use that as column index
mat[, ncol(mat):1]
Or with rev
mat[, rev(seq_len(ncol(mat)))]

how to extract rows from a table using for loops and perform a calculation

I have two data tables as .dbf files that I want to extract values from by row and populate an empty data frame with the new values using the following equation; (tmax_08 - tmin_08)/2 - 65. This will result in values both positive and negative in which I want to separate those and square each value and take the sum of squares at the end. Below is what I have so far.
Install.packages("foreign")
library(foreign)
tmax_08<- read.dbf("E:/Adam Stuff/Daymet_Daily_UTM/1km table/tmax/tmax_2008_daymet.dbf")
tmin_08 <- read.dbf("E:/Adam Stuff/Daymet_Daily_UTM/1km table/tmin/tmin_2008_daymet.dbf")
my.df <- data.frame(matrix(0, ncol = 366, nrow = 24024))
row <- tmax_08[1,]
row2 <- tmin_08[1,]
I have as you can see been able to extract the first row and all columns from each .dbf table, however automating this would be much better as I have a lot of files this needs to be done for. Thank you in advance for any help provided!

R - creating dataframe from colMeans function

I've been trying to create a dataframe from my original dataframe, where rows in the new dataframe would represent mean of every 20 rows of the old dataframe. I discovered a function called colMeans, which does the job pretty well, the only problem, which still persists is how to change that vector of results back to dataframe, which can be further analysed.
my code for colMeans: (matrix1 in my original dataframe converted to matrix, this was the only way I managed to get it to work)
a<-colMeans(matrix(matrix1, nrow=20));
But here I get the numeric sequence, which has all the results concatenated in one single column(if I try for example as.data.frame(a)). How am I supposed to get this result back into dataframe where each column includes only the results for specific column name and not all the averages.
I hope my question is clear, thanks for help.
Based on the methods('as.data.frame'), as.data.frame.list is an option to convert each element of a vector to columns of a data.frame
as.data.frame.list(a)
data
m1 <- matrix(1:20, ncol=4, dimnames=list(NULL, paste0('V', 1:4)))
a <- colMeans(m1)

Sorting list of matrices by the first column

I have a list containing 4 matrices, each with 21 random numbers in 3 columns and 7 rows.
I want to create new list using lapply function in which each matrix is sorted by the first column.
I tried:
#example data
set.seed(1)
list.a <- replicate(4, list(matrix(sample(1:99, 21), nrow=7)))
ordered <- order(list.a[,1])
lapply(list.a, function(x){[ordered,]})
but at the first step the R gives me error "incorrect number of dimensions". Don't know what to do. It works with one matrix, though.
Please help me. Thanks!
You were almost there - but you would need to iterate through the list to reorder each matrix.
Its easier to do this is one lapply statement
lapply(list.a, function(x) x[order(x[,1]),])
Note that x in the function call represents the matrices in the list.

Resources