Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
388 views
in Technique[技术] by (71.8m points)

r - list unique values for each column in a data frame

Suppose you have a very large input file in "csv" format. And you want to know the different values that occur in each column. How would you do that?

ex.

column1    column2    column3    column4
----------------------------------------
value11    value12    value13    value14
value21    value22    value23    value24
...
valueN1    valueN2    valueN3    valueN4

So I want my output to be something like:

column1 has these values: value11, value21, ...valueN1. but I don't need to see reoccurrences of the same value. I need this just to get an idea of what my data is all about.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Let dat be your data frame after reading in the csv file, you can do

ulst <- lapply(dat, unique)

If you further want to know the number of unique values for each column, do

k <- lengths(ulst)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...