Grep part of dataframe in R?


 
Thread Tools Search this Thread
Top Forums Programming Grep part of dataframe in R?
# 1  
Old 02-21-2013
Grep part of dataframe in R?

Seems not very post about R language. Here is one: How to grep a sublist of a list like grep -f in unix? say I have a dataframe
Code:
ID v1 v2 v3
A 1 3 4 
B 4 5 6 
C 7 8 9 
D 1 3 4 
E 1 3 3 
F 2 4 5

and I only need
Code:
ID v1 v2 v3
A 1 3 4  
C 7 8 9 
E 1 3 3 
F 2 4 5

by like
Code:
grep vector=c("A", "C", "E", "F")

or
Code:
 grep vector=read.table(file="list.file")

? Thanks a lot!
# 2  
Old 02-21-2013
# 3  
Old 02-21-2013
Found the answer!

Got answer from stackoverflow!
Code:
>dfrm
A 1 3 4 
B 4 5 6
C 7 8 9
D 1 3 4
E 1 3 3
F 2 4 5

## and my list of the interest is stored in a file
Code:
subset <-read.table("infile", header=F)
>subset
1 A
2 C
3 E
4 F
> dfrm[rownames(dfrm) %in% subset[,1], ]
A 1 3 4 
C 7 8 9
E 1 3 3
F 2 4 5

subset[, 1] does the trick, subset is a dataframe too, that I should have been aware of!
R is also very powerful.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove duplicates in a dataframe (table) keeping all the different cells of just one of the columns

Hello all, I need to filter a dataframe composed of several columns of data to remove the duplicates according to one of the columns. I did it with pandas. In the main time, I need that the last column that contains all different data ( not redundant) is conserved in the output like this: A ... (5 Replies)
Discussion started by: pedro88
5 Replies

2. Shell Programming and Scripting

How to grep only some part of character?

Hi, I have log : # cat log $ cat 4 2014092014 2014102014 2014112023 2014123014 2014010100 2014010101 2014010102 2014010103 2014010104 2014020123 2014020115 2014020116 (3 Replies)
Discussion started by: justbow
3 Replies

3. Ubuntu

Merging strings that have identical rownames in a dataframe

Hi I have a data frame with repeated names in column 1, and different descriptors in column 2. I want to merge/cat strings that have same entry in column 1 into one row with any separator. Example for input: Cvel_1 KOG0155 Cvel_1 KOG0306 Cvel_1 KOG3259 Cvel_1 ... (4 Replies)
Discussion started by: Alyaa
4 Replies

4. UNIX for Dummies Questions & Answers

can I grep a part of one line ?

<exp code="12556a" message="ok, fine4" displayMessage="jksdfj ksd" \> <exp code="123456a" message="ok, 2fine" displayMessage="jksdfj ksd" \> <exp code="12dfgda" message="1ok, fine" displayMessage="jksdfj ksd" \> now I want to cut code attribute and message attribute, such as ... (2 Replies)
Discussion started by: vincent_W
2 Replies

5. Shell Programming and Scripting

How to get a part of the line(need help in using grep)

Hi, Suppose, DBconnection=jdbc: oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=x.x.x.x)(PORT=YYYY))(LOAD_BALANCE=yes)(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=project_db1))) This is a part of a file <filename> . I Need to get the value of SERVICE_NAME from this line… The... (4 Replies)
Discussion started by: Dpu
4 Replies

6. UNIX for Dummies Questions & Answers

How to use grep to get only part of a line...

Hello All. I have an output file which contains the phrase "Total DFT Energy =" and then a number. This occurs many times in the output file, and what I want is to pipe the numbers (which are all different) to a file so I can plot them. How do I grep "Total DFT Energy =" and then get the numbers... (3 Replies)
Discussion started by: EinsteinMcfly
3 Replies

7. Shell Programming and Scripting

grep part of file name

Hi, I have a problem to use grep in my script . If I want to grep the file for example : PTWO9089.txt The code below works . grep ^PONE But, I dont know on how to grep the file like this 9066PTWO.txt I'll try to use this code : grep PTWO^ ,but it doesn't work. For your info, the... (4 Replies)
Discussion started by: badbunny9316
4 Replies

8. Shell Programming and Scripting

Grep part of the line

How do I print only the values of the variables from set or env. For example I have the lines USER=USER XYZ='text' SHELL=/bin/bash How do I print only the part after or starting from = Let's say I have those lines in a list X If I run Y = echo $X | grep -o "=*$" Y won't contain... (3 Replies)
Discussion started by: Transsive
3 Replies

9. Shell Programming and Scripting

grep a part of a line

Hi, In a file FILE, there is a line like this one MOLECULE C2 H2 I want to extract "C2 H2". I can do it in two step in a script : VARIABLE="`grep MOLECULE FILE`" # Assign "MOLECULE C2 H2" VARIABLE=`echo ${VARIABLE/"MOLECULE "}` # Remove "MOLECULE ". Then, $echo $VARIABLE gives C2... (6 Replies)
Discussion started by: tipi
6 Replies

10. UNIX for Dummies Questions & Answers

grep within certain part of file

Hi, Is it possible to grep only on a certain part of a file? Say I have file in.txt which contains below: 11/16 13:07:19.5436 --- ERROR 123 detected. 11/16 13:08:19.5436 --- Generating a <reading> event 11/16 13:08:19.7784 ---- Sending a <writing> event 11/16 14:08:37.4516 ---... (2 Replies)
Discussion started by: Orbix
2 Replies
Login or Register to Ask a Question