mutate() and str_replace() function - rstudio

I'm trying to remove any strings that contains the word "Seattle" in the column named "County" in the table named Population
Population %>%
mutate( str_replace(County, "Seattle", ""))
It gives me an error message.

I suspect you are getting an error because in your mutate you aren't defining what column you're mutating...
Also, I think you will have better success with an if_else statement detecting the string pattern of Seattle using grepl and then replacing the contents. Below is the code I've used for something similar.
Population %>%
mutate(County = if_else(grepl("Seattle", County),"",County))
The grepl will detect the string pattern in the County field and provide a TRUE/FALSE return. From there, you just define what to do if it is found to be true, i.e. replace it with nothing (""), or keep the value as is (County).

Related

If results are not >= "Number" then show blank

New to building Crystal Reports and SQL.
I'm trying to write a script to where if results is >= 12.1 then show result else show no results.
Same goes for the <=9.9.
Here is what I have so far:
if {Test.Name} = "xyz" and {TestResults.numresult}>= 12.1 then {TestResults.numresult} else "";
Below is an image showing the same results across the board. I just want the results to show when its <=9.9 or >=12.1.
Hope this make sense.
Your statement returns a number from one branch and a string from the other. It must return the same data type.
One option is to use a True/False expression in a Suppress expression.
Another option is to return a zero in the other branch and use number formatting to suppress if zero (it's a built-in option for numbers).
Another option is to modify your expression so it returns a string from both branches. For example:
if {Test.Name} = "xyz" and {TestResults.numresult}>= 12.1 then ToText({TestResults.numresult}, 1, ",") else "";
The 1 argument requests 1 decimal point. The "," argument requests a comma as thousands separator. You can adjust those to match your number formatting requirements.

How do I identify whether a column entry starts with a letter or a number using m code in power query?

I have a column that contains either letters or numbers. I want to add a column identifying whether each cell contains a letter or a number. The problem is that there are thousands of records in this particular database.
I tried the following syntax:
= Table.AddColumn(Source, "Column2", each if [Column1] is number then "Number" else "Letters")
My problem is that when I enter this, it returns everything as "Letter" because it looks at the column type instead of the actual value in the cell. This remains the case even when I change the column type from Text to General. Either way, it still produces "Letter" as it automatically assigns text as the data type since the column contains both text and numbers.
Use this expression:
= Table.AddColumn(Source, "Column2", each if List.Contains({"0".."9"}, Text.Start([Column1], 1)) then "Numbers" else "Letters")
Note: It would have been smart to add sample data to your question so I wouldn't have to guess what your data actually looks like!
Add column, custom column with
= try if Value.Is(Number.From([Column1]), type number) then "number" else "not" otherwise "not"
Peter's method works if the choice is AAA/111 but this one tests for A11 and 1BC as well

exctract substring from a large string under certain conditions in painless

In painless I would like to create a script which reads a keyword field called 'objldn' and extracts only five consecutive characters sometimes present in a precise position. Infact, in the keyword field 'objldn' there are a large variety of long strings among which there are some of them with a third underscore. After the third underscore, if it is present, I can fetch the consecutive 5 chars.
Whith the following lines of code I implement what I want:
def LU = doc['objldn'].value.splitOnToken('_');
return LU[3].substring(0, 5);
But the system returnes an error message "out of bounds":
Request error: array_index_out_of_bounds_exception, Index 3 out of
bounds for length 3 in "def LU =
doc['objldn'].value.splitOnToken('_'); ..." (Painless script)
error executing runtime field or scripted field on index pattern
return LU[3].substring(0, 5);
^---- HERE
may be it is due to the fact that many strings do not have the third underscore or do not even have one and therefore I need to implement firstly a IF statement which evaluates if a third underscore is in the string and only if it is present it proceeds to execute splitOnToken()... but I am not able to do it correctly. Can you help me to add the IF statement in the script please?
Why not simply checking the length of the LU array?
def LU = doc['objldn'].value.splitOnToken('_');
return LU.length >= 4 ? LU[3].substring(0, 5) : null;

How to filter datas in OpenERP using domain list

I want to filter recods in OPenERP using domain filter expression
In the recored I have a field of list of users, so i want get the record where the user logged in the list
[(user.id , 'in' , 'user_ids')]
This doesn't work
it return this error :
File "/usr/lib/pymodules/python2.7/openerp/osv/orm.py", line 2356, in search
return self._search(cr, user, args, offset=offset, limit=limit, order=order, context=context, count=count)
File "/usr/lib/pymodules/python2.7/openerp/osv/orm.py", line 4846, in _search
self._apply_ir_rules(cr, user, query, 'read', context=context)
File "/usr/lib/pymodules/python2.7/openerp/osv/orm.py", line 4728, in _apply_ir_rules
rule_where_clause, rule_where_clause_params, rule_tables = rule_obj.domain_get(cr, uid, self._name, mode, context=context)
File "/usr/lib/pymodules/python2.7/openerp/addons/base/ir/ir_rule.py", line 156, in domain_get
query = self.pool.get(model_name)._where_calc(cr, SUPERUSER_ID, dom, active_test=False)
File "/usr/lib/pymodules/python2.7/openerp/osv/orm.py", line 4676, in _where_calc
e = expression.expression(cr, user, domain, self, context)
File "/usr/lib/pymodules/python2.7/openerp/osv/expression.py", line 632, in __init__
self.parse(cr, uid, context=context)
File "/usr/lib/pymodules/python2.7/openerp/osv/expression.py", line 759, in parse
field_path = left.split('.', 1)
AttributeError: 'int' object has no attribute 'split'
Please help me.
Your domain syntax is wrong.
It should be [('user_ids', '=' , user.id)]
Each tuple in the search domain needs to have 3 elements, in the form: ('field_name', 'operator', value), where:
field_name must be a valid name of field of the object model, possibly following many-to-one relationships using dot-notation, e.g 'street' or 'partner_id.country' are valid values.
operator must be a string with a valid comparison operator from this list:
=, !=, >, >=, <, <=, like, ilike, in, not in, child_of, parent_left, parent_right
The semantics of most of these operators are obvious.
The child_of operator will look for records who are children or grand-children of a given record,
according to the semantics of this model (i.e following the relationship field named by
self._parent_name, by default parent_id.
value must be a valid value to compare with the values of field_name, depending on its type.
Domain criteria can be combined using 3 logical operators than can be added between tuples: '&' (logical AND, default), '|' (logical OR), '!' (logical NOT).
These are prefix operators and the arity of the '&' and '|' operator is 2, while the arity of the '!' is just 1. Be very careful about this when you combine them the first time.
Here is an example of searching for Partners named ABC from Belgium and Germany whose language is not english ::
[('name','=','ABC'),'!',('language.code','=','en_US'),'|',('country_id.code','=','be'),('country_id.code','=','de')]
The '&' is omitted as it is the default, and of course we could have used '!=' for the language, but what this domain really represents is::
(name is 'ABC' AND (language is NOT english) AND (country is Belgium OR Germany))
In a simple case this is right, BUT if you want to filter current object by its functional field, you'll be very surprised that code in function of this field will not execute; instead fnct_search part of this field will be executed, that will allow you to do various things.
Left part of filter expression is evaluated in context of current object, and right part — in context of inner context (read current user).
Left part is evaluated after right, so you can add a functional field to a user model, make some calculations there, and then receive those calculations on the object's side and take them into account.
See this answer for details: https://stackoverflow.com/a/21336100/674557

How do I search an array using a partial string, and return the index?

I want to search through an array using a partial string, and then get the index where that string is found. For example:
a = ["This is line 1", "We have line 2 here", "and finally line 3", "potato"]
a.index("potato") # this returns 3
a.index("We have") # this returns nil
Using a.grep will return the full string, and using a.any? will return a correct true/false statement, but neither returns the index where the match was found, or at least I can't figure out how to do it.
I'm working on a piece of code that reads a file, looks for a specific header, and then returns the index of that header so it can use it as an offset for future searches. Without starting my search from a specific index, my other searches will get false positives.
Use a block.
a.index{|s| s.include?("We have")}
or
a.index{|s| s =~ /We have/}

Resources