0

I have the following like data in a tab-delimted text file named original

Name     Symbol       Value
abcd       A            56   
de45       C            67
ji98       H            90
k9ug       K            43
phzt       L            98
prex       P            21
kadf       T            32

Also I have list of selected Symbols stored in another tab delimited text file named duplicate

Symbol     Description
 K            Intel
 P            Diary
 C            Cape
 S            Sheath
 A            Aim

I want to extract the rows from original file which has same Symbol with duplicate. I want my output like the following:

Name     Symbol       Value
abcd       A            56   
de45       C            67
k9ug       K            43
prex       P            21

I tried using the following code but some how I could not get any results or only the row of A. Here is the code which I have used

result <- original[original$Symbol %in% duplicate$Symbol,]

Could anyone please help me.

5
  • Shouldn't the variable in the duplicate dataframe be Symbol, not symbol? Commented Sep 10, 2012 at 10:00
  • Check your spelling of "original" ;-) Commented Sep 10, 2012 at 10:09
  • @James, that would make a merge easier, but looking at the supplied attempt at extracting the rows, the OP is aware of the difference in capitalization. Commented Sep 10, 2012 at 10:17
  • @ James I changed the spelling but I got the "<0 rows> (or 0-length row.names) " as my result..I m ready to provide further information also..I dont know where I went wrong Commented Sep 10, 2012 at 11:17
  • @Thileepan, I hope you didn't change the spelling for both James's suggestion and mine. If you change as James suggested, then it doesn't match the data you provided. Read my updated answer with the assumptions. Commented Sep 10, 2012 at 11:44

1 Answer 1

8

This can be done with a simple merge:

merge(original, duplicate, by.x="Symbol", by.y="symbol")
#   Symbol Name Value Description
# 1      A abcd    56         Aim
# 2      C de45    67        Cape
# 3      K k9ug    43       Intel
# 4      P prex    21       Diary

You can manually drop the Description column before or after merging if it is not relevant.

Also, I don't know if this is a problem with the question as posted or if it is a problem with your code, but:

original[original$Symbol %in% duplicate$symbol, ]
#   Name Symbol Value
# 1 abcd      A    56
# 2 de45      C    67
# 4 k9ug      K    43
# 6 prex      P    21

Of course, you have to spell original correctly, which you did not!

Assumptions

  1. The correct capitalization of the word "symbol" in names(original) shows up with an upper-case S (Symbol).
  2. The correct capitalization of the word "symbol" in names(duplicate) shows up with a lower-case s (symbol).

If both are capitalized, then you can use either of the following solutions:

merge(original, duplicate)
original[original$Symbol %in% duplicate$Symbol, ]
Sign up to request clarification or add additional context in comments.

1 Comment

@ mrdwab I changed the spelling but I got the "<0 rows> (or 0-length row.names) " as my result..I m ready to provide further information also..I dont know where I went wrong

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.