J'ai la matrice suivante:

r1 <- c("1","01","001","0001","01","100")
r2 <- c("1","10","001","0001","10","100")
r3 <- c("1","01","100","1000","10","010")
r4 <- c("1","01","010","0100",NA,"001")
r5<- c(NA, "01","010","0010","10","001")

n.mat <- rbind(r1,r2,r3,r4,r5)

Et je voudrais supprimer les colonnes contenant un seul caractère comme dans la première colonne (dans l'exemple). Et j'aimerais ignorer les NA. Donc, même s'il y a un NA dans la première colonne (NA est évidemment de deux caractères) je voudrais le supprimer. La sortie devrait donc ressembler à ceci:

r1 <- c("01","001","0001","01","100")
r2 <- c("10","001","0001","10","100")
r3 <- c("01","100","1000","10","010")
r4 <- c("01","010","0100",NA,"001")
r5 <- c("01","010","0010","10","001")

n.mat <- rbind(r1,r2,r3,r4,r5)

Pour le moment, j'ai cette commande, mais cela ne semble pas fonctionner:

n.mat[apply(n.mat, 2, sum) > 1, ]
1
Luker354 23 oct. 2020 à 11:54

3 réponses

Meilleure réponse

Nous pouvons utiliser colSums pour sélectionner des colonnes contenant plus d'un caractère.

n.mat[, colSums(nchar(n.mat) > 1, na.rm = TRUE) > 0]

#   [,1] [,2]  [,3]   [,4] [,5] 
#r1 "01" "001" "0001" "01" "100"
#r2 "10" "001" "0001" "10" "100"
#r3 "01" "100" "1000" "10" "010"
#r4 "01" "010" "0100" NA   "001"
#r5 "01" "010" "0010" "10" "001"
3
Ronak Shah 23 oct. 2020 à 09:07
n.mat[,apply(nchar(n.mat),2,max,na.rm=T)>1]
   [,1] [,2]  [,3]   [,4] [,5] 
r1 "01" "001" "0001" "01" "100"
r2 "10" "001" "0001" "10" "100"
r3 "01" "100" "1000" "10" "010"
r4 "01" "010" "0100" NA   "001"
r5 "01" "010" "0010" "10" "001"
2
user2974951 23 oct. 2020 à 08:58

Est-ce que ça marche:

> n.mat[, apply(n.mat, 2, function(x) sum(nchar(x), na.rm = 1)/sum(!is.na(x)) > 1)]
   [,1] [,2]  [,3]   [,4] [,5] 
r1 "01" "001" "0001" "01" "100"
r2 "10" "001" "0001" "10" "100"
r3 "01" "100" "1000" "10" "010"
r4 "01" "010" "0100" NA   "001"
r5 "01" "010" "0010" "10" "001"
1
Karthik S 23 oct. 2020 à 09:03