how to get lengths of data frames [duplicate] - r

This question already has answers here:
number of rows each data frame in a list [duplicate]
(2 answers)
How to count rows?
(3 answers)
Closed 5 years ago.
I have a list of data frames with different lengths. How do I get the length of each data frame? I tried nrow function but it returns "NULL".

The lengths is for getting the length of list of vectors. Here, we need the nrow or ncol
sapply(lst, nrow)
sapply(lst, ncol)
depending upon what the OP considers as length

Related

How to get list of all combinations of pairs of character strings in R [duplicate]

This question already has answers here:
How to generate all possible combinations of vectors without caring for order?
(2 answers)
Closed 3 years ago.
I have multiple character strings, let's say 'pred_1', 'pred_2' and 'pred_3'. Now I want to get a list with all pairs of the strings. So, the resulting list should contain 'pred_1 pred_2', 'pred_1 pred_3' and 'pred_2 pred_3'. Does anyone know how to automate this for more than three character strings?
An option is combn
combn(v1, 2, simplify = FALSE)
data
v1 <- paste0("pred_", 1:3)

How can I split a tibble or data.frame that only has one column into four columns in R? [duplicate]

This question already has answers here:
How to reshape data from long to wide format
(14 answers)
Closed 4 years ago.
I have a tibble that I'm trying to separate into multiple columns:
The tibble only has one column and should look like the right side of the image when done.
It can be done with matrix (assuming that the length of column is a multiple of 4)
matrix(data$col1, ncol=4, byrow = TRUE)

Bring each value in the list to its own column [duplicate]

This question already has answers here:
How do I make a matrix from a list of vectors in R?
(6 answers)
Combine a list of data frames into one data frame by row
(10 answers)
Closed 5 years ago.
I have a dat frame called crimeLARaw_tbl_final.
One of the columns called location_1.coordinates, is a ‘list’ data type column which contains a list of values:
location_1.coordinates
c(-118.3157, 34.0454)
c(-118.2717, 34.001)
c(-118.2671, 34.0294)
c(-118.4517, 33.967)
How could I bring each value in the list to its own column?
long lat
-118.3157 34.0454
-118.2717 34.001
-118.2671 34.0294
-118.4517 33.967
I have tried with dplyr and stringr but not getting anywhere.
Thank you

Average a list of matrix [duplicate]

This question already has answers here:
Element-wise mean over list of matrices [duplicate]
(2 answers)
Closed 6 years ago.
For example, I have a list of matrix like this
list2<-lapply(1:2, function(x) matrix(rnorm(6, 10, 1), nrow=2, ncol=3))
list2
How do I get a matrix which has the same size with each matrix in each list, and the value in each cell equal to the average of corresponding cell across lists.
We can do this by adding the corresponding cells in each list elements and divide by the length of the list
Reduce(`+`, list2)/length(list2)
Or another option is to unlist the list, create a 3D array, use apply to get the mean
apply(array(unlist(list2), c(2,3,2)), c(1,2), mean)

How to create data frame from list in R? [duplicate]

This question already has answers here:
Most efficient list to data.frame method?
(2 answers)
Closed 9 years ago.
I have a list of 3000 vectors in R. How can I create a data frame from the first 1000 elements of this list?
Cheers
You can do:
data.frame(myList[1:1000])
This makes the assumption that the vectors are all the same length. If they are not, values will be recycled.

Resources