How to remove rows of a data frame using a separate vector - filtering

I am trying to filter a data frame that contains Argos locations of any implausible locations using sdafilter function that is part of the argosfilter package.
This is a section of the vector output when the sdafilter is run, it has 3 elements:
"removed" (location removed by the filter)
"end_location" (location at the end of the track where the algorithm could not be applied)
"not" (location not removed):
[1] "end_location" "end_location" "removed" "removed" "removed"
[6] "removed" "removed" "not" "not" "not"
[11] "not" "removed" "not" "not" "removed"
[16] "not" "not" "not" "not" "removed"
[21] "not" "not" "not" "not" "not"
[26] "not" "not" "not" "not" "not"
The data points that are returned in the vector as "removed" are points which are implausible given the set criteria specified in the sdafilter function. I want to apply this vector to my data frame to remove all rows that correspond with where "removed" is displayed in the vector.
Any suggestions or help would be much appreciated.

I suggest you look at the following example.
If you want to specifically store the not filtered values into a new data frame, you could use cbind like in the example below:
library(argosfilter)
data(seal)
lat<-seal$lat
lon<-seal$lon
dtime<-seal$dtime
mfilter<-vmask(lat,lon,dtime,2)
lon[4:8]
lat[4:8]
mfilter[4:8]
not_Removed <- cbind(lon[which(mfilter=="not")],lat[which(mfilter=="not")])
not_Removed[1:5,1:2]

Related

Get Event Log Message content in a Variable

I want to get the the first "WDS.Device.ID" (00-15-5D-8A-44-25) (without the [] brackets) into a variable.
I tried some RegEx things but without success as I lack the knowledge for it.
PS C:\Windows\system32> $result | fl
Message : A device query was successfully processed (status 0x0):
Input:
WDS.Request.Type='Deployment'
WDS.Client.Property.Architecture.Process='X64'
WDS.Client.Property.Architecture.Native='X64'
WDS.Client.Property.Firmware.Type='BIOS'
WDS.Client.Property.SMBIOS.Manufacturer='Microsoft Corporation'
WDS.Client.Property.SMBIOS.Model='Virtual Machine'
WDS.Client.Property.SMBIOS.Vendor='American Megatrends Inc.'
WDS.Client.Property.SMBIOS.Version='090008 '
WDS.Client.Property.SMBIOS.ChassisType='Desktop'
WDS.Client.Property.SMBIOS.UUID={CCD695BE-20AB-48CC-8F01-319B498F7A69}
WDS.Client.Request.Version=1.0.0.0
WDS.Client.Version=10.0.18362.1
WDS.Client.Host.Version=10.0.18362.1
WDS.Client.DDP.Default.Match=FALSE
WDS.Device.ID=[00-15-5D-8A-44-25]
WDS.Device.ID=[BE-95-D6-CC-AB-20-CC-48-8F-01-31-9B-49-8F-7A-69]
Output:
WDS.Client.Property.Architecture.Process='X64'
WDS.Client.Property.Architecture.Native='X64'
WDS.Client.Property.Firmware.Type='BIOS'
WDS.Client.Property.SMBIOS.Manufacturer='Microsoft Corporation'
WDS.Client.Property.SMBIOS.Model='Virtual Machine'
WDS.Client.Property.SMBIOS.Vendor='American Megatrends Inc.'
WDS.Client.Property.SMBIOS.Version='090008 '
WDS.Client.Property.SMBIOS.ChassisType='Desktop'
WDS.Client.Property.SMBIOS.UUID={CCD695BE-20AB-48CC-8F01-319B498F7A69}
WDS.Client.Request.Version=1.0.0.0
WDS.Client.Version=10.0.18362.1
WDS.Client.Host.Version=10.0.18362.1
WDS.Client.DDP.Default.Match=FALSE
WDS.Client.Request.ResendAuthenticated=TRUE
Turning my comment into an answer.
If the message you show is inside a string variable (let's call it $message), then you can use regex to get the value for the WDS.Device.ID without the brackets like this:
$devideID = ([regex]'(?i)WDS\.Device\.ID=\[((?:[0-9a-f]{2}-){5}[0-9a-f]{2})\]').Match($message).Groups[1].Value
Result:
00-15-5D-8A-44-25
Regex details:
WDS Match the characters “WDS” literally
\. Match the character “.” literally
Device Match the characters “Device” literally
\. Match the character “.” literally
ID= Match the characters “ID=” literally
\[ Match the character “[” literally
( Match the regular expression below and capture its match into backreference number 1
(?: Match the regular expression below
[0-9a-f] Match a single character present in the list below
A character in the range between “0” and “9”
A character in the range between “a” and “f”
{2} Exactly 2 times
- Match the character “-” literally
){5} Exactly 5 times
[0-9a-f] Match a single character present in the list below
A character in the range between “0” and “9”
A character in the range between “a” and “f”
{2} Exactly 2 times
)
] Match the character “]” literally
The (?i) in the regex makes it case-insensitive
here's another way to go about it. this presumes the $Result variable holds one multiline string AND that the 1st [ & the 1st ] are "bracketing" your target data. [grin]
$Result.Split('[')[1].Split(']')[0]
output = 00-15-5D-8A-44-25

Classify and filter entries based on string in Tableau

I'm trying to filter based on substrings within a string. These strings can contain A through E, or any combination of the five (such as ["C"] or ["A","C","D","E"]). Is there a way I could search through the entire string for each letter before returning a value?
The code I have currently (below) stops when the first IF statement is true. My goal is to be able to classify the entries by the letters in the string and use this calculation as a filter. So, an entry with the string ["A"] would be filtered under "A", but the string ["C","E"] would be filtered under both "C" and "E". Thank you for your help.
IF CONTAINS([Q2.6],"A") then "A"
ELSEIF CONTAINS([Q2.6],"B") then "B"
ELSEIF CONTAINS([Q2.6],"C") then "C"
ELSEIF CONTAINS([Q2.6],"D") then "D"
ELSEIF CONTAINS([Q2.6],"E") then "E"
END

cell2table removes values from first column if string is a single character

Suppose you have the following data:
A = [1,2,3;4,5,6];
headers = {'force', 'mass', 'acceleration'};
units = {'N','Kg','m/s^2'};
Let's say I want to convert it to a table, where headers will be the 'VariableNames':
table_of_data = cell2table([units; num2cell(A)]);
table_of_data.Properties.VariableNames = headers
table_of_data =
force mass acceleration
_____ ____ ____________
N 'Kg' 'm/s^2'
[2] [3]
[5] [6]
Note that the first two columns of A are removed. This is because MATLAB treats the single character N differently than 'Kg' and 'm/s^2'. If I insert a space after 'N ' I get:
table_of_data =
force mass acceleration
_____ ____ ____________
'N ' 'Kg' 'm/s^2'
[1] [2] [3]
[4] [5] [6]
How can I get a proper table, with all elements displayed without inserting a space 'N '?
It's no problem to use a single character in units if I add more rows to the cell array, such as [headers; units; num2cell(A)], so the following works:
table_of_data = cell2table([headers; units; num2cell(A)]);
table_of_data(1,:) = [];
table_of_data.Properties.VariableNames = headers
table_of_data =
force mass acceleration
_____ ____ ____________
'N ' 'Kg' 'm/s^2'
[1] [2] [3]
[4] [5] [6]
How can I solve this without turning to cumbersome workarounds?
This likely has to do with table's internal representation of the data. It seems like what it does is tries to vertically concatenate the data in a column and if the concatenation succeeds then it uses an array, otherwise it stores it as a cell .
In the case of a single character N and the numbers, 1 and 4, they can be concatenated without error; however, it converts them all to chars.
vertcat('N', 1, 4)
However, when you add the space, concatenation now fails
vertcat('N ', 1, 4)
And the output is displayed like a cell.
You have a few options:
Use table.Properties.VariableUnits to store the units rather than trying to incorporate the units into your table.
table_of_data.Properties.VariableUnits = units;
Display the units in the column headers
headers = {'force_N', 'mass_kg', 'acceleration_m_s2'};
Create a double-nested cell array to store all of the units, which explicitly causes it to be stored as a cell array internally.
table_of_data = cell2table([num2cell(units); num2cell(A)])

why can the character's order in regex expression affect sed?

The tv.txt file is as following:
mms://live21.gztv.com/gztv_gz 广州台[可于Totem/VLC/MPlayer播放,记得把高宽比设置成4:3]
mms://live21.gztv.com/gztv_news 广州新闻台·直播广州(可于Totem/VLC/MPlayer播放,记得把高宽比设置成4:3)
mms://live21.gztv.com/gztv_kids 广州少儿台(可于Totem/VLC/MPlayer播放,记得把高宽比设置成4:3)
mms://live21.gztv.com/gztv_econ 广州经济台
I want to group it into three groups.
sed -r 's/([^ ]*)\s([^][()]*)((\(.+\))*|(\[.+\])*)/\3/' tv.txt
got the result:
[可于Totem/VLC/MPlayer播放,记得把高宽比设置成4:3]
(可于Totem/VLC/MPlayer播放,记得把高宽比设置成4:3)
(可于Totem/VLC/MPlayer播放,记得把高宽比设置成4:3)
When I write it into
sed -r 's/([^ ]*)\s([^][()]*)((\(.+\))*|(\[.+\])*)/\3/' tv.txt
It can't work.
The only difference is [^][()] and [^[]()]; neither of the [^\[\]()] ,escape characters can not make it run properly.
I want to know the reason.
The POSIX rules for getting ] into a character class are a little arcane, but they make sense when you think about it hard.
For a positive (non-negated) character class, the ] must be the first character:
[]and]
This recognizes any character a, n, d or ] as part of the character class.
For a negated character class, the ] must be the first character after the ^:
[^]and]
This recognizes any character except a, n, d or ] as part of the character class.
Otherwise, the first ] after the [ marks the end of the character class. Inside a character class, most of the normal regex special characters lose their special meaning, and others (notably - minus) acquire special meanings. (If you want a - in a character class, it has to be 'first' or last, where 'first' means 'after the optional ^ and only if ] is not present'.)
In your examples:
[^][()] — this is a negated character class that recognizes any character except [, ], ( or ), but
[^[]()] — this is a negated character class that recognizes any character except [, followed by whatever () symbolizes in the regex family you're using, and ] which represents itself.

Delete a cell array column

Placed simple values into the cell array for testing.
model{1,1}=1;model{1,2}=2;model{1,3}=3;
model{2,1}=4;model{2,2}=5;model{2,3}=6;
i=2;//I want to remove the second column
temp={ model{:,1:i-1} model{:,i+1:size(model,2)} }
I wanted a result like this:
temp =
[1] [3]
[4] [6]
But I'm getting this:
temp =
[1] [4] [3] [6]
How can I get this right?
p.s: for anyone working on Cell Arrays, there's a nice technique for appending here.
You can reshape or delete the cells themselves using ()-addressing.
model(:,2) = [];
You have to transpose the two pieces, and change some parentheses:
temp= [{ model{:,1:i-1}}' {model{:,i+1:size(model,2)}}']
there is a function called fun_removecellrowcols, which removes specific row/columns indicated by the user. This affects the dimensions of the cell, due to the row/cols removal.
http://www.mathworks.com/matlabcentral/fileexchange/46196-fun-removecellrowcols
Regards,
José