Extract fractions numbers from text and sum - odd results? - google-sheets

I have a cell containing fractions as text, like:
2/4 + 2/4 +2/4
I want to extract fractions as numbers, and sum them.
I tried a simple:
=SPLIT(A2, "+")
that correctly split numbers in three columns
and then
=SUM(SPLIT(A2, "+"))
but got this wrong result:
4/19
Can you help understand how to get right result?

try:
=INDEX(QUERY(, "select "&A2), 2)

Related

What is the easiest and most accurate way to average feet/inches?

Using the format #'##, what would be the best way to average values using a formula?
For example: if I have the following values -
The average of these heights should return 5'01. What would be the best way to achieve this using a formula? Thank you!
try:
=INDEX(JOIN("'", TEXT({"", "0."}&SPLIT(AVERAGE(QUERY(QUERY(SPLIT(A1:A3, "'"),
"select Col1+Col2/12"), "offset 1", 0)), ".")*{1, 12}, {"0", "0#"})))
Given your post data, which runs A1:A3 (and I'm assuming further down than Row 3):
=ArrayFormula(INT(AVERAGE(FILTER({VALUE(REGEXEXTRACT(A:A,"\d+"))+VALUE(REGEXEXTRACT(A:A,"\d+$"))/12},A:A<>"")))&"'"&TEXT(ROUND(MOD(AVERAGE(FILTER({VALUE(REGEXEXTRACT(A:A,"\d+"))+VALUE(REGEXEXTRACT(A:A,"\d+$"))/12},A:A<>"")),1)*12),"00"))
In summary, REGEXEXTRACT is used to separate the left and right portions of the original data. VALUE converts them from strings to numbers. /12 and *12 do the conversion from inches to decimal and back to inches. The ampersand (&) symbols are used to concatenated the final INTeger portion, the feet symbol (') and the converted decimal-to-inches value. TEXT assures that the final result is in the format 00.
I've come up with a formula that might solve your problem but it is a bit long.
=ROUND(ARRAYFORMULA(AVERAGE((REGEXEXTRACT(A1:A3, "(\d)'\d+")*12)+REGEXEXTRACT(A1:A3, "\d'(\d+)")))/12)&"'"&MOD(ARRAYFORMULA(AVERAGE((REGEXEXTRACT(A1:A3, "(\d)'\d+")*12)+REGEXEXTRACT(A1:A3, "\d'(\d+)"))),12)
What the formula does is it calculates the values of A1:A3 into inches and gets the average before converting it again into feet'inches format. Please see result below.

How do I write a formula to code numbers into letters?

We work in gemstones and would like to figure out a formula for codifying the prices. This would depend on the client we could adjust the price without them seeing our baseline.
$1710 would show on the tag: 1GA0.
Essentially, the alphabet would be assigned to the numbers, we'd like to keep the first digit but anything after that would be a letter.
Is there a way to do this in google sheets to generate this?
Suppose the following are true:
1.) Your raw price data runs A2:A.
2.) Your raw price data are actual numbers.
Use the following array formula in the second cell of an otherwise empty column:
=ArrayFormula(IF(A2:A="",,REGEXREPLACE(LEFT(A2:A)&TRANSPOSE(QUERY(TRANSPOSE(IF(SPLIT(REGEXREPLACE(MID(A2:A&REPT("~",10),2,10),"(.)","$1~"),"~")&""="0",0,CHAR(64+SPLIT(REGEXREPLACE(MID(A2:A&REPT("~",10),2,10),"(.)","$1~"),"~"))))," ",10)),"\s|#","")))
This will produce all converted results for A2:A.
As this is a unique custom formula that will not be often requested by future site visitors, and since it is rather complex, I'm providing it as-is and without explanation at this time.
Can you try this:
Formula for B1:
=JOIN("", LEFT(A1, 1), ARRAYFORMULA(IFERROR(SUBSTITUTE(ADDRESS(1, MID(A1, ROW($A$2:INDIRECT("A" & LEN(A1))), 1) * 1, 4), "1", ""), "0")))
Output:
Step by step formula behavior:
Step 1: splits the 2nd digit to the last digit into separate cells
Step 2: convert valid numbers (1-9) into letters
Step 3: convert #VALUE to "0"
Step 4: combine the 1st number and the converted value.
Note:
1st number and zeroes are as is, everything else is converted.
If $ is a necessity and is present together with a ,, then adjust your formula into:
Adjusted formula:
=JOIN("", MID(TO_TEXT(A1), "2", "1"), ARRAYFORMULA(IFERROR(SUBSTITUTE(ADDRESS(1, MID(REGEXREPLACE(TO_TEXT(A1), ",", ""), ROW($A$3:INDIRECT("A" & LEN(REGEXREPLACE(TO_TEXT(A1), ",", "")))), 1) * 1, 4), "1", ""), "0")))
Adjustments:
LEFT(A1, 1) -> MID(TO_TEXT(A1), "2", "1")
since we consider the $, we need to get the 2nd character instead of the first 1.
A1 -> REGEXREPLACE(TO_TEXT(A1), ",", "")
since getting the value directly will yield to a number but the length will result into the string's length, we need to compare them equally, thus converting the number into text and then removing the , to return the proper value as string.
$A$2 -> $A$3
we start to convert the third character instead of the 2nd.
Adjusted formula now should yield the following output:
References:
Convert number to letter
Split number into digits

Google Sheets: Sum if value is round number

I'm sure there's a pretty simple solution, but I cant' get to it.
I'm trying to sum a list of numbers, but only the values that are round numbers/whole integers.
Eg column A:
1
1.5
3
2.4
2
sum of the whole numbers
1 + 3 + 2 = 6
Any hint?
Let's suppose your numbers list begins in A2 and runs downward (i.e., A2:A). You can use this:
=SUM(FILTER(A2:A,A2:A=INT(A2:A)))
In plain English, this reads as follows: "Sum only those numbers in A2:A where the original value is the same as the integer-only portion of that value."
Try:
=sumproduct((A1:A5)*(A1:A5=int(A1:A5)))
this will also work in Excel
use dot detection:
=INDEX(SUM(IF(REGEXMATCH(""&A:A, "\."),,A:A)))
Another solution, you can use SEARCH to search for the decimal point:
=SUM(A1:A)-SUM(FILTER(A1:A,SEARCH(".",TO_TEXT(A1:A))))
or =SUM(FILTER(A1:A,NOT(ISNUMBER(SEARCH(".",A1:A)))))
as JvdV mentioned in his comment.
Either try QUERY():
=SUM(QUERY(A:A,"where A matches '\d+'"))
Or FILTER():
=SUM(FILTER(A:A,MOD(A:A,1)=0))
Note: This 1st option makes use of the possibility to use a regular expression inside the "where" clause of QUERY(). Use =SUM(QUERY(A:A,"where A matches '-?\d+'")) if you want to account for positive and negative integers.

Google sheets count occurrences as fractions

Have a column that contains text separated by a ',' and I am trying to work out the fraction of occurrences for each string in the column as detailed below
a,b
a
g,g,f
a
b
b,a,f,f
Output needed in another column
0.5
1
0.33
1
1
0.25
Not sure the best approach here is to use a query and SQL do this? I'm sure there is a simple way?
My current thinking
=QUERY(IF D3 contains "," (len(D3)-len(SUBSTITUTE(D3,",","")) else 1))
Assuming your data starting in D3, try
=arrayformula(if(len(D3:D), trunc(1/len(substitute(D3:D, ",",)),2),))
and see if that works?
(Change range to suit)
SPLIT by ,
Divide 1 by COUNT of the resulting array
=1/COUNTA(SPLIT(A2,","))

Split sentence with many words in to two approximately equal by length pieces

I have sentences with length between 50-70 symbols in my google spreadsheet. I have to split them into two approximately equal pieces by whitespace.
for example:
"This is sentence with 5 words."
I want to get this:
"This is sentence", "with 5 words."
Is there any way to do that?
Here's the formula to do this:
=SPLIT(JOIN("",
ArrayFormula(TRANSPOSE(SPLIT(A1," "))&
if(MOD(row(INDIRECT("A1:A"&COUNTA(SPLIT(A1," ")))),3)=0,CHAR(10)," "))),CHAR(10))
change this:
A1 to cell that contains text, change it 2 times
3 to number of words in pieces
For example, this text in cell A1:
one to 3 for 5 six 7 eit nine ten will be converted into pieces:
one to 3
for 5 six
7 eit nine
ten
note that last piece contains only one word, it's the rest after all words were splited.
Edit
The key tho this formula is number of words, counted in it:
=COUNTA(SPLIT(A1," "))
To get approximate number of words in each part, use this formula:
=CEILING(COUNTA(SPLIT(A1," "))/2)
And the total formula will be:
=SPLIT(JOIN("", ArrayFormula(TRANSPOSE(SPLIT(A1," "))& if(MOD(row(INDIRECT("A1:A"&COUNTA(SPLIT(A1," ")))),CEILING(COUNTA(SPLIT(A1," "))/2))=0,CHAR(10)," "))),CHAR(10))
This formula doesn't count number of letters in sentence, so it can give not equal parts.
Also try
This formulas count number of lettets and will split the text accordingly. It might work better for big sentences.
Formula for part1, paste in B1:
=TRIM(REGEXEXTRACT(A1,REPT("[^ ]+ ",counta(SPLIT(LEFT(A1,CEILING(LEN(A1)/2))," ")))))
Formula for part2, paste in C1:
=TRIM(SUBSTITUTE(A1,B1,""))
To change "This is sentence with 5 words." to "This is sentence", "with 5 words." try:
=join(""", """,(SPLIT(JOIN("", ArrayFormula(TRANSPOSE(SPLIT(A1," "))& if(MOD(row(INDIRECT("A1:A"&COUNTA(SPLIT(A1," ")))),CEILING(COUNTA(SPLIT(A1," "))/2))=0,CHAR(10)," "))),CHAR(10))))

Resources