last day (14 days later) » 

12:47 PM
0
Q: Remove and set aside elements of vectors within a list that don't exist in another vector

ShawnI'm trying to form arguments for use in the reshape() function. I have a vector of column names, some of which should be merged by reshape() because they share the same letter at the end: > v <- c("x","da","db","ea","eb","ec","fb") Most of these columns are comprised of a combination of pre a...

 
@akrun I don't think OP wants to explicitly exclude 'x' and 'c'. They want code that identifies 'x' and 'c' based on the rule that they do not share a terminal character with any other string in the vector.
 
@akrun That's pretty cool, but I don't know about c being a unique postfix ahead of time (I do, however know about x ahead of time). That part needs to be deduced in the program, which is why I've structured things this way.
@akrun I'm not sure I follow your meaning. Suffice it to say that v pre and post are already pre-defined for purposes of this problem, so we can use those resources to deduce the desired result.
 
@Shawn will the 'post' values ever be in 'pre' also? Or are they mutually exclusive?
 
In actuality, pre and post are quite long strings (words separated by underscores or decimals). So they may share bits and pieces here and there but they would never be identical as a whole. In fact, pre values always have decimals inside while post values never do.
 
Could you provide examples of a few actual pre and post strings?
 
12:47 PM
Pre: "192.168.10.2_card02_port01" "192.168.10.2_card3_port2" and Post: "state" "line_speed" "bits_sent" and v: "elapsed_time" "192.168.10.2_card02_port01_state" "192.168.10.2_card02_port01_line_speed" "192.168.10.2_card02_port01_bits_sent" "192.168.10.2_card3_port2_state" etc
 
Thx, this helps. And one or two v values to compare it to.
 
Never tried chat before... I appreciate your help thusfar
 
Np
I see the v values. give me a sec
 
yeah I could get more comprehensive with that to show the oddball ones that look like they should be repeats, but actually aren't (like c in my problem)
 
its ok
 
12:51 PM
I'm used to SQL, so R seemed like a really good fit for pre-processing things that I'm about to squeeze into a database, but it's pretty tough to debug since a lot of it comes off as cryptic to me =)
it's one of those languages where people's answers to questions bring up even more questions than were answered... for me anyway
it's one of those languages where people's answers to questions bring up even more questions than were answered... for me anyway
 
freq <- lapply(Map(function(x) grep(x, v), post), length)
that counts how many times each post value appears in v
look at it
index <- Map(function(x) grep(x, v), names(freq)[freq>1])
This line locates which values appear more than once. That way, x and ec shouldn't show up.
 
didn't know you could use length like that.... that's grabbing the length of v automatically?
 
yes
test with : v[unlist(index)]
which gives, [1] "da" "ea" "db" "eb" "fb"
and the opposite gives the ones that were left out: v[unlist(index)]
v[-unlist(index)]
sorry the opposite is: v[-unlist(index)]
giving: [1] "x" "ec"
 
wow ok so this is getting really close... now the trick is to use those indeces found in index to get the actual values to show
 
that's what I did with v[unlist(index)] and v[-unlist(index)]
 
1:00 PM
sorry my workspace isn't on this computer, so I can't cut/paste easily...
right you got them to appear in a vector with that
but my understanding is that reshape() needs to take them in as a list of vectors
so more similar in structure to what you achieved with index, but instead of listing the matched indeces, list the actual values that they represent
see desired output from original problem... not just a vector but a list of vectors
though your v[-unlist(index)] answers my second desired output perfectly
 
try this: lapply(index, function(x) v[x])
it gives `$a
[1] "da" "ea"

$b
[1] "db" "eb" "fb"`
as a list
 
that's it... i was trying lov[index] which was complaining about the index data type... I really need to figure out how to get my mind into gear for these apply() functions
 
they take a while. keep at it, you're on your way
 
I hope! You wanna throw all this up in an answer for credit?
 
I'll post an answer to help others
 
1:06 PM
cool!
now that the sample is solved... i'm gonna cross my fingers that i structured this well enough to work with my real data =)
 
good luck
 
worked perfectly... thanks again!
take care!
 
happy to help
 

last day (14 days later) »