Removing the "" in the matrix in R in order to work with rows with non empty elements for a list -


i have matrix called: combination_mat , looks this:

combination_mat <- read.csv("data.csv")

combination_mat <- as.matrix(na.omit(combination_mat))

combination_mat

    x1        x2        x3       x4       1  "ff"      "wgs10yr" "wtb3ms" "stlfsi"   2  "ff"      "wgs10yr" "stlfsi" ""       3  "wgs10yr" "wtb3ms"  "stlfsi" ""       4  "ff"      "wtb3ms"  "stlfsi" ""       5  "ff"      "wgs10yr" "wtb3ms" ""       6  "ff"      "wtb3ms"  ""       ""       7  "ff"      "stlfsi"  ""       ""       8  "wgs10yr" "stlfsi"  ""       ""       9  "wgs10yr" "wtb3ms"  ""       ""       10 "ff"      "wgs10yr" ""       ""       11 "stlfsi"  "wtb3ms"  ""       ""       12 "ff"      ""        ""       ""       13 "wgs10yr" ""        ""       ""       14 "wtb3ms"  ""        ""       ""       15 "stlfsi"  ""        ""       "" 

however, after using lapply() looping each row, end list elements , emplty elements e.g. "".

the result looking looks this:

combination_mat

    x1        x2        x3       x4       1  "ff"      "wgs10yr" "wtb3ms" "stlfsi"   2  "ff"      "wgs10yr" "stlfsi"      3  "wgs10yr" "wtb3ms"  "stlfsi"      4  "ff"      "wtb3ms"  "stlfsi"     5  "ff"      "wgs10yr" "wtb3ms"       6  "ff"      "wtb3ms"       7  "ff"      "stlfsi"       8  "wgs10yr" "stlfsi"       9  "wgs10yr" "wtb3ms"       10 "ff"      "wgs10yr"       11 "stlfsi"  "wtb3ms"       12 "ff"       13 "wgs10yr"      14 "wtb3ms"      15 "stlfsi"              

some links searched: remove spaces cells in matrix r: data.frame rows list remove empty elements string array

thanks!

you can't keep data matrix/ data.frame whilst removing elements, columns of matrix have different lengths. there couple of ways of dealing it.

method 1: (frank's comment) convert matrix list , remove of elements, access elements of list if columns of matrix:

m = matrix(c("a","b","c",""),2,2)  apply(m,1,function(x) x[x!=""]) 

method 2: keep matrix form of result, convert "" entries na , filter them later calculations.

m = matrix(c("a","b","c",""),2,2)  m[m == ""] <- na 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -