-6

I have a sequence data and I importing it in the data frame. I want to have each distinct letters from the entire data frame and not from the each row.

The data frame looks like this:

enter image description here

Thanks for your help.

user2100721
  • 3,557
  • 2
  • 20
  • 29
Krishnang Dalal
  • 83
  • 1
  • 1
  • 9

1 Answers1

1

See this

df<-data.frame(V1=c("m h j i d a","j h o f k l","g k o d m a"))
unique(unlist(apply(df,1,strsplit,split=" ")))
user2100721
  • 3,557
  • 2
  • 20
  • 29
  • Or `split=""` if there are not spaces between the letters. It is difficult to tell based on the question. – Matthew Lundberg Jun 27 '16 at 16:13
  • Awesome! Thanks a lot. Just a quick question. You used 'apply' function and as far as I know apply is useful when the data in the data frame is of equal length. Do you know anyway to generalize this for even unequal data? – Krishnang Dalal Jun 27 '16 at 16:14
  • @Krish As far as I know "unequal data" is not a specific, well-defined thing in R. Maybe you should consider making a proper example with code (like the first line of this answer has). – Frank Jun 27 '16 at 16:16
  • @MatthewLundberg : Definitely. Depending on the pic, I did that. Maybe **Krish** can help us on this occasion by providing sample of data frame. – user2100721 Jun 27 '16 at 16:22
  • @Krish : I am unable to understand you. Will you please explain **Do you know anyway to generalize this for even unequal data?** by providing the reproducible data frame. – user2100721 Jun 27 '16 at 16:23
  • Yes sure, I am writing a function to get the unique values so my code in the function needs to be general. Suppose my data frame is as follows: df <- data.frame(V1=c("m h j k l", "e J h", "e r t g h j k l")) will 'apply' work here too? – Krishnang Dalal Jun 27 '16 at 16:34
  • @Krish : Why not? Use the code. Btw, if you got your answer from my post please accept it. – user2100721 Jun 27 '16 at 18:00
  • Yes I used it for different types of data and it worked well. I apologise for not being an ideal Stack user as this is my first experience here. Thank you. – Krishnang Dalal Jun 28 '16 at 20:52