Sunday, 15 September 2013

In R: How to compare a csv file and a list to keep common terms, and to list those separately -



In R: How to compare a csv file and a list to keep common terms, and to list those separately -

i have csv file several columns of data, 1 of wish utilize directly. have read csv file r this:

list1<-readlines("myfile.csv")

should read in though?:

list1<-read.csv(file="myfile.csv",sep=",",head=true)

let's csv file, column 5, lists ("one", "three", "four")

i have list in r this:

list2<-c("one","two","three","three")

i want compare 5th column of csv file list2 , have mutual terms pulled , listed separately. want maintain repeats if mutual term listed more once, illustration "three" listed twice in list2, want list twice in new list well.

i want not list column 5 of csv file entire row contains mutual term column 5.

this best effort not acknowledge if mutual term found more 1 time in csv file, nor list entire row mutual term found in csv file.

list1<-readlines("myfile.csv") list2<-c("one","two","three","three") intersect(list1,list2)

result: one, three

thanks help!

to figure out elements of list2 in column 5 of list1:

list2[list2 %in% list1[,5]]

and rows of list1 there entries in column 5 in list2:

list1[which(list1[,5] %in% list2),]

a little shorter:

list1[list1[,5] %in% list2,]

consider side note: improve practice not phone call these 2 objects list1 , list2 since list1 data.frame , list2 vector , list type of info construction in r (see ?list).

r list csv intercept

No comments:

Post a Comment