Est-il possible de sortir troncature lors de l'affichage du contenu de dataframes?
-
28-10-2019 - |
Question
J'ai une trame de données avec des colonnes très longues « commentaires ». Quand je les ai affichés, ils sont divisés en différents blocs, ce qui rend difficile à lire à travers les lignes. Est-il possible de changer un paramètre dans R ou modifier l'appel à data.frame aux chaînes de truncate à une certaine longueur?
Exemple: une trame de données 3 colonnes
data.frame(cbind(rep(1,5),rep(c("very very long obnoxious character string here" ,"dog","cat","dog",5)),rep(c("very very long obnoxious character string here" ,"dog","cat","dog",5))))
Résultant dataframe vu sur mon écran:
X1 X2
1 1 very very long obnoxious character string here
2 1 dog
3 1 cat
4 1 dog
5 1 5
X3
1 very very long obnoxious character string here
2 dog
3 cat
4 dog
5 5
La solution
Je recommande une sorte de façon explicite comme ceci:
f <- function(x) data.frame(lapply(x, substr, 1, 5))
utilisation:
> f(d)
X1 X2 X3
1 1 very very
2 1 dog dog
3 1 cat cat
4 1 dog dog
5 1 5 5
Bien qu'il soit possible de changer le comportement par défaut, je ne recommande pas:
body(format.data.frame)[[5]] <- quote(for (i in 1L:nc) rval[[i]] <- substr(format(x[[i]], ..., justify = justify), 1, 5))
unlockBinding("format.data.frame", baseenv())
assign("format.data.frame", format.data.frame, pos = baseenv())
lockBinding("format.data.frame", baseenv())
rm(format.data.frame)
utilisation:
> d
X1 X2 X3
1 1 very very
2 1 dog dog
3 1 cat cat
4 1 dog dog
5 1 5 5
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow