kandi background
Explore Kits

chuck | An inapp HTTP inspector for Android OkHttp clients | Reactive Programming library

 by   jgilfelt Java Version: v1.0.4 License: Apache-2.0

 by   jgilfelt Java Version: v1.0.4 License: Apache-2.0

Download this library from

kandi X-RAY | chuck Summary

chuck is a Java library typically used in Programming Style, Reactive Programming applications. chuck has no bugs, it has no vulnerabilities, it has build file available, it has a Permissive License and it has high support. You can download it from GitHub, Maven.
Chuck is a simple in-app HTTP inspector for Android OkHttp clients. Chuck intercepts and persists all HTTP requests and responses inside your application, and provides a UI for inspecting their content. Apps using Chuck will display a notification showing a summary of ongoing HTTP activity. Tapping on the notification launches the full Chuck UI. Apps can optionally suppress the notification, and launch the Chuck UI directly from within their own interface. HTTP interactions and their contents can be exported via a share intent. The main Chuck activity is launched in its own task, allowing it to be displayed alongside the host app UI using Android 7.x multi-window support. Chuck requires Android 4.1+ and OkHttp 3.x. Warning: The data generated and stored when using this interceptor may contain sensitive information such as Authorization or Cookie headers, and the contents of request and response bodies. It is intended for use during development, and not in release builds or other production deployments.
Support
Support
Quality
Quality
Security
Security
License
License
Reuse
Reuse

kandi-support Support

  • chuck has a highly active ecosystem.
  • It has 4457 star(s) with 420 fork(s). There are 93 watchers for this library.
  • It had no major release in the last 12 months.
  • There are 43 open issues and 31 have been closed. On average issues are closed in 22 days. There are 8 open pull requests and 0 closed requests.
  • It has a neutral sentiment in the developer community.
  • The latest version of chuck is v1.0.4
chuck Support
Best in #Reactive Programming
Average in #Reactive Programming
chuck Support
Best in #Reactive Programming
Average in #Reactive Programming

quality kandi Quality

  • chuck has 0 bugs and 0 code smells.
chuck Quality
Best in #Reactive Programming
Average in #Reactive Programming
chuck Quality
Best in #Reactive Programming
Average in #Reactive Programming

securitySecurity

  • chuck has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
  • chuck code analysis shows 0 unresolved vulnerabilities.
  • There are 0 security hotspots that need review.
chuck Security
Best in #Reactive Programming
Average in #Reactive Programming
chuck Security
Best in #Reactive Programming
Average in #Reactive Programming

license License

  • chuck is licensed under the Apache-2.0 License. This license is Permissive.
  • Permissive licenses have the least restrictions, and you can use them in most projects.
chuck License
Best in #Reactive Programming
Average in #Reactive Programming
chuck License
Best in #Reactive Programming
Average in #Reactive Programming

buildReuse

  • chuck releases are available to install and integrate.
  • Deployable package is available in Maven.
  • Build file is available. You can build the component from source.
  • Installation instructions, examples and code snippets are available.
  • chuck saves you 1136 person hours of effort in developing the same functionality from scratch.
  • It has 2566 lines of code, 227 functions and 53 files.
  • It has medium code complexity. Code complexity directly impacts maintainability of the code.
chuck Reuse
Best in #Reactive Programming
Average in #Reactive Programming
chuck Reuse
Best in #Reactive Programming
Average in #Reactive Programming
Top functions reviewed by kandi - BETA

kandi has reviewed chuck and discovered the below as its top functions. This is intended to give you an instant insight into chuck implemented functionality, and help decide if they suit your requirements.

  • This method enqueues http activity
  • show notification
  • Get share text
  • Queries the given URI .
  • Extracts the database .
  • Determines whether the given buffer contains plain text .
  • On create cursor loader
  • Populates UI .
  • Set up the ViewPager .
  • cast a period to milliseconds

chuck Key Features

An in-app HTTP inspector for Android OkHttp clients

Setup

copy iconCopydownload iconDownload
 dependencies {
   debugCompile 'com.readystatesoftware.chuck:library:1.1.0'
   releaseCompile 'com.readystatesoftware.chuck:library-no-op:1.1.0'
 }

License

copy iconCopydownload iconDownload
Copyright (C) 2017 Jeff Gilfelt.

Having an issue in saving fitted and observed values in lm model?

copy iconCopydownload iconDownload
res <- by(mada4, mada4$aggre_per, \(x) {
  x <- cbind(x, yield[ ,2:5])
  sm <- summary(mod <- lm(I_S1 ~ value, x))
  o <- setNames(as.vector(sm$coefficients[, c(1, 4)])[c(1, 2, 4)],
                c('Intercept', 'slope', 'pval'))
  o <- c(o, sapply(sm[c('r.squared', 'adj.r.squared', 'fstatistic')], `[`, 1))
  o <- c(o, pearson=with(x, cor(I_S1, value)))[c(2, 1, 4, 5, 6, 3, 7)]
  ft <- cbind(x, x$I_S1, fitted(mod))
  return(`attr<-`(o, 'obsFit', ft))
})
do.call(rbind, res) |> signif(4)
#         slope Intercept r.squared adj.r.squared fstatistic.value    pval  pearson
# 3     -1.1680      6231   0.09105      0.026130          1.40200 0.25600 -0.30170
# 4     -0.2730      6129   0.00464     -0.066460          0.06526 0.80210 -0.06812
# 5     -1.0350      6240   0.01636     -0.053900          0.23290 0.63680 -0.12790
# 6     -2.8780      6508   0.13910      0.077660          2.26300 0.15470 -0.37300
# 7     -1.1220      6297   0.05134     -0.016420          0.75760 0.39880 -0.22660
# amj   -0.8696      6476   0.07058      0.004188          1.06300 0.32000 -0.26570
# mamjj -0.8889      6761   0.18210      0.123700          3.11700 0.09926 -0.42670
obsFits <- lapply(res, attr, 'obsFit')
obsFits$`3`  ## first aggre_per `3`
#    year aggre_per value I_S1 II_S1 III_S1 IV_S1 x$I_S1 fitted(mod)
# 1  2001         3   189 5204  5081   5071  5515   5204    6010.131
# 2  2002         3     6 5824  5674   5349  5491   5824    6223.904
# 3  2003         3   212 5481  5589   5703  5969   5481    5983.264
# 4  2004         3    81 5587  5809   5451  5971   5587    6136.292
# 5  2005         3   101 6260  6055   5764  6265   6260    6112.929
# 6  2006         3    84 5771  6231   5528  6242   5771    6132.788
# 7  2007         3   163 5899  5728   5629  6153   5899    6040.503
# 8  2008         3   198 6338  5971   5395  6188   6338    5999.618
# 9  2009         3   143 6340  6268   5797  6311   6340    6063.866
# 10 2010         3   128 6013  5999   5785  6447   6013    6081.389
# 11 2011         3   456 6075  6015   5887  6213   6075    5698.234
# 12 2012         3    93 6484  6624   5581  6410   6484    6122.274
# 13 2013         3    37 6771  6827   5899  6744   6771    6187.691
# 14 2014         3    23 6493  6453   6179  6754   6493    6204.045
# 15 2015         3    58 6386  6382   6086  6247   6386    6163.160
# 16 2016         3     0 6465  6340   5734  6415   6465    6230.913
-----------------------
res <- by(mada4, mada4$aggre_per, \(x) {
  x <- cbind(x, yield[ ,2:5])
  sm <- summary(mod <- lm(I_S1 ~ value, x))
  o <- setNames(as.vector(sm$coefficients[, c(1, 4)])[c(1, 2, 4)],
                c('Intercept', 'slope', 'pval'))
  o <- c(o, sapply(sm[c('r.squared', 'adj.r.squared', 'fstatistic')], `[`, 1))
  o <- c(o, pearson=with(x, cor(I_S1, value)))[c(2, 1, 4, 5, 6, 3, 7)]
  ft <- cbind(x, x$I_S1, fitted(mod))
  return(`attr<-`(o, 'obsFit', ft))
})
do.call(rbind, res) |> signif(4)
#         slope Intercept r.squared adj.r.squared fstatistic.value    pval  pearson
# 3     -1.1680      6231   0.09105      0.026130          1.40200 0.25600 -0.30170
# 4     -0.2730      6129   0.00464     -0.066460          0.06526 0.80210 -0.06812
# 5     -1.0350      6240   0.01636     -0.053900          0.23290 0.63680 -0.12790
# 6     -2.8780      6508   0.13910      0.077660          2.26300 0.15470 -0.37300
# 7     -1.1220      6297   0.05134     -0.016420          0.75760 0.39880 -0.22660
# amj   -0.8696      6476   0.07058      0.004188          1.06300 0.32000 -0.26570
# mamjj -0.8889      6761   0.18210      0.123700          3.11700 0.09926 -0.42670
obsFits <- lapply(res, attr, 'obsFit')
obsFits$`3`  ## first aggre_per `3`
#    year aggre_per value I_S1 II_S1 III_S1 IV_S1 x$I_S1 fitted(mod)
# 1  2001         3   189 5204  5081   5071  5515   5204    6010.131
# 2  2002         3     6 5824  5674   5349  5491   5824    6223.904
# 3  2003         3   212 5481  5589   5703  5969   5481    5983.264
# 4  2004         3    81 5587  5809   5451  5971   5587    6136.292
# 5  2005         3   101 6260  6055   5764  6265   6260    6112.929
# 6  2006         3    84 5771  6231   5528  6242   5771    6132.788
# 7  2007         3   163 5899  5728   5629  6153   5899    6040.503
# 8  2008         3   198 6338  5971   5395  6188   6338    5999.618
# 9  2009         3   143 6340  6268   5797  6311   6340    6063.866
# 10 2010         3   128 6013  5999   5785  6447   6013    6081.389
# 11 2011         3   456 6075  6015   5887  6213   6075    5698.234
# 12 2012         3    93 6484  6624   5581  6410   6484    6122.274
# 13 2013         3    37 6771  6827   5899  6744   6771    6187.691
# 14 2014         3    23 6493  6453   6179  6754   6493    6204.045
# 15 2015         3    58 6386  6382   6086  6247   6386    6163.160
# 16 2016         3     0 6465  6340   5734  6415   6465    6230.913
-----------------------
res <- by(mada4, mada4$aggre_per, \(x) {
  x <- cbind(x, yield[ ,2:5])
  sm <- summary(mod <- lm(I_S1 ~ value, x))
  o <- setNames(as.vector(sm$coefficients[, c(1, 4)])[c(1, 2, 4)],
                c('Intercept', 'slope', 'pval'))
  o <- c(o, sapply(sm[c('r.squared', 'adj.r.squared', 'fstatistic')], `[`, 1))
  o <- c(o, pearson=with(x, cor(I_S1, value)))[c(2, 1, 4, 5, 6, 3, 7)]
  ft <- cbind(x, x$I_S1, fitted(mod))
  return(`attr<-`(o, 'obsFit', ft))
})
do.call(rbind, res) |> signif(4)
#         slope Intercept r.squared adj.r.squared fstatistic.value    pval  pearson
# 3     -1.1680      6231   0.09105      0.026130          1.40200 0.25600 -0.30170
# 4     -0.2730      6129   0.00464     -0.066460          0.06526 0.80210 -0.06812
# 5     -1.0350      6240   0.01636     -0.053900          0.23290 0.63680 -0.12790
# 6     -2.8780      6508   0.13910      0.077660          2.26300 0.15470 -0.37300
# 7     -1.1220      6297   0.05134     -0.016420          0.75760 0.39880 -0.22660
# amj   -0.8696      6476   0.07058      0.004188          1.06300 0.32000 -0.26570
# mamjj -0.8889      6761   0.18210      0.123700          3.11700 0.09926 -0.42670
obsFits <- lapply(res, attr, 'obsFit')
obsFits$`3`  ## first aggre_per `3`
#    year aggre_per value I_S1 II_S1 III_S1 IV_S1 x$I_S1 fitted(mod)
# 1  2001         3   189 5204  5081   5071  5515   5204    6010.131
# 2  2002         3     6 5824  5674   5349  5491   5824    6223.904
# 3  2003         3   212 5481  5589   5703  5969   5481    5983.264
# 4  2004         3    81 5587  5809   5451  5971   5587    6136.292
# 5  2005         3   101 6260  6055   5764  6265   6260    6112.929
# 6  2006         3    84 5771  6231   5528  6242   5771    6132.788
# 7  2007         3   163 5899  5728   5629  6153   5899    6040.503
# 8  2008         3   198 6338  5971   5395  6188   6338    5999.618
# 9  2009         3   143 6340  6268   5797  6311   6340    6063.866
# 10 2010         3   128 6013  5999   5785  6447   6013    6081.389
# 11 2011         3   456 6075  6015   5887  6213   6075    5698.234
# 12 2012         3    93 6484  6624   5581  6410   6484    6122.274
# 13 2013         3    37 6771  6827   5899  6744   6771    6187.691
# 14 2014         3    23 6493  6453   6179  6754   6493    6204.045
# 15 2015         3    58 6386  6382   6086  6247   6386    6163.160
# 16 2016         3     0 6465  6340   5734  6415   6465    6230.913

Select repeated values using an array containing repeated elements

copy iconCopydownload iconDownload
with keys as
(select 1 as key
union all select 1
union all select 2
union all select 3
union all select 3)
select name
from keys natural join myTable;
-----------------------
select * 
from myTable m
join unnest(array[1,1,2,3,3]) keys (k)
on m.key = keys.k

How to iterate through all tags of a website in Python with Beautifulsoup?

copy iconCopydownload iconDownload
from bs4 import BeautifulSoup
import urllib.request

html = urllib.request.urlopen('http://py4e-data.dr-chuck.net/comments_1430669.html').read()
soup = BeautifulSoup(html, 'html.parser')
data = []

for tr in soup.find_all('tr'):
    row = [td.text for td in tr.find_all('td')]
    data.append(row[1])     # or data.append(row) for both
    
print(data)
['Comments', '100', '100', '99', '96', '93', '93', '89', '88', '85', '84', '84', '81', '79', '76', '74', '73', '71', '70', '67', '61', '60', '60', '59', '54', '53', '53', '52', '50', '46', '46', '45', '41', '38', '37', '37', '36', '34', '26', '24', '24', '23', '23', '21', '17', '17', '16', '14', '12', '11', '7']
for name, comment in data[1:]:
    print(name, comment)
Melodie 100
Machaela 100
Rhoan 99
Murrough 96
Lilygrace 93
Ellenor 93
Verity 89
Karlie 88
-----------------------
from bs4 import BeautifulSoup
import urllib.request

html = urllib.request.urlopen('http://py4e-data.dr-chuck.net/comments_1430669.html').read()
soup = BeautifulSoup(html, 'html.parser')
data = []

for tr in soup.find_all('tr'):
    row = [td.text for td in tr.find_all('td')]
    data.append(row[1])     # or data.append(row) for both
    
print(data)
['Comments', '100', '100', '99', '96', '93', '93', '89', '88', '85', '84', '84', '81', '79', '76', '74', '73', '71', '70', '67', '61', '60', '60', '59', '54', '53', '53', '52', '50', '46', '46', '45', '41', '38', '37', '37', '36', '34', '26', '24', '24', '23', '23', '21', '17', '17', '16', '14', '12', '11', '7']
for name, comment in data[1:]:
    print(name, comment)
Melodie 100
Machaela 100
Rhoan 99
Murrough 96
Lilygrace 93
Ellenor 93
Verity 89
Karlie 88
-----------------------
from bs4 import BeautifulSoup
import urllib.request

html = urllib.request.urlopen('http://py4e-data.dr-chuck.net/comments_1430669.html').read()
soup = BeautifulSoup(html, 'html.parser')
data = []

for tr in soup.find_all('tr'):
    row = [td.text for td in tr.find_all('td')]
    data.append(row[1])     # or data.append(row) for both
    
print(data)
['Comments', '100', '100', '99', '96', '93', '93', '89', '88', '85', '84', '84', '81', '79', '76', '74', '73', '71', '70', '67', '61', '60', '60', '59', '54', '53', '53', '52', '50', '46', '46', '45', '41', '38', '37', '37', '36', '34', '26', '24', '24', '23', '23', '21', '17', '17', '16', '14', '12', '11', '7']
for name, comment in data[1:]:
    print(name, comment)
Melodie 100
Machaela 100
Rhoan 99
Murrough 96
Lilygrace 93
Ellenor 93
Verity 89
Karlie 88
-----------------------
from bs4 import BeautifulSoup
import urllib.request

html = urllib.request.urlopen('http://py4e-data.dr-chuck.net/comments_1430669.html').read()
soup = BeautifulSoup(html, 'html.parser')
data = []

for tr in soup.find_all('tr'):
    row = [td.text for td in tr.find_all('td')]
    data.append(row[1])     # or data.append(row) for both
    
print(data)
['Comments', '100', '100', '99', '96', '93', '93', '89', '88', '85', '84', '84', '81', '79', '76', '74', '73', '71', '70', '67', '61', '60', '60', '59', '54', '53', '53', '52', '50', '46', '46', '45', '41', '38', '37', '37', '36', '34', '26', '24', '24', '23', '23', '21', '17', '17', '16', '14', '12', '11', '7']
for name, comment in data[1:]:
    print(name, comment)
Melodie 100
Machaela 100
Rhoan 99
Murrough 96
Lilygrace 93
Ellenor 93
Verity 89
Karlie 88
-----------------------
import urllib.request
from bs4 import BeautifulSoup
html = urllib.request.urlopen('http://py4e-data.dr-chuck.net/comments_1430669.html').read()
soup = BeautifulSoup(html,'html.parser')
tags = soup.find_all("span")
for i in tags:
  print(i.string)
import urllib.request
from bs4 import BeautifulSoup
html = urllib.request.urlopen('http://py4e-data.dr-chuck.net/comments_1430669.html').read()
soup = BeautifulSoup(html,'html.parser')
tags = soup.find_all("span")
comments = {}
for index, tag in enumerate(tags):
  commentorName = tag.find_previous('tr').text
  commentorComments = tag.string
  comments[commentorName] = commentorComments
print(comments)
{'Melodie100': '100', 'Machaela100': '100', 'Rhoan99': '99', 'Murrough96': '96', 'Lilygrace93': '93', 'Ellenor93': '93', 'Verity89': '89', 'Karlie88': '88', 'Berlin85': '85', 'Skylar84': '84', 'Benny84': '84', 'Crispin81': '81', 'Asya79': '79', 'Kadi76': '76', 'Dua74': '74', 'Stephany73': '73', 'Eila71': '71', 'Jennah70': '70', 'Eduardo67': '67', 'Shannan61': '61', 'Chymari60': '60', 'Inez60': '60', 'Charlene59': '59', 'Rosalin54': '54', 'James53': '53', 'Rhy53': '53', 'Zein52': '52', 'Ayren50': '50', 'Marissa46': '46', 'Mcbride46': '46', 'Ruben45': '45', 'Mikee41': '41', 'Carmel38': '38', 'Idahosa37': '37', 'Brooklin37': '37', 'Betsy36': '36', 'Kayah34': '34', 'Szymon26': '26', 'Tea24': '24', 'Queenie24': '24', 'Nima23': '23', 'Eassan23': '23', 'Haleema21': '21', 'Rahma17': '17', 'Rob17': '17', 'Roma16': '16', 'Jeffrey14': '14', 'Yorgos12': '12', 'Denon11': '11', 'Jasmina7': '7'}
-----------------------
import urllib.request
from bs4 import BeautifulSoup
html = urllib.request.urlopen('http://py4e-data.dr-chuck.net/comments_1430669.html').read()
soup = BeautifulSoup(html,'html.parser')
tags = soup.find_all("span")
for i in tags:
  print(i.string)
import urllib.request
from bs4 import BeautifulSoup
html = urllib.request.urlopen('http://py4e-data.dr-chuck.net/comments_1430669.html').read()
soup = BeautifulSoup(html,'html.parser')
tags = soup.find_all("span")
comments = {}
for index, tag in enumerate(tags):
  commentorName = tag.find_previous('tr').text
  commentorComments = tag.string
  comments[commentorName] = commentorComments
print(comments)
{'Melodie100': '100', 'Machaela100': '100', 'Rhoan99': '99', 'Murrough96': '96', 'Lilygrace93': '93', 'Ellenor93': '93', 'Verity89': '89', 'Karlie88': '88', 'Berlin85': '85', 'Skylar84': '84', 'Benny84': '84', 'Crispin81': '81', 'Asya79': '79', 'Kadi76': '76', 'Dua74': '74', 'Stephany73': '73', 'Eila71': '71', 'Jennah70': '70', 'Eduardo67': '67', 'Shannan61': '61', 'Chymari60': '60', 'Inez60': '60', 'Charlene59': '59', 'Rosalin54': '54', 'James53': '53', 'Rhy53': '53', 'Zein52': '52', 'Ayren50': '50', 'Marissa46': '46', 'Mcbride46': '46', 'Ruben45': '45', 'Mikee41': '41', 'Carmel38': '38', 'Idahosa37': '37', 'Brooklin37': '37', 'Betsy36': '36', 'Kayah34': '34', 'Szymon26': '26', 'Tea24': '24', 'Queenie24': '24', 'Nima23': '23', 'Eassan23': '23', 'Haleema21': '21', 'Rahma17': '17', 'Rob17': '17', 'Roma16': '16', 'Jeffrey14': '14', 'Yorgos12': '12', 'Denon11': '11', 'Jasmina7': '7'}
-----------------------
import urllib.request
from bs4 import BeautifulSoup
html = urllib.request.urlopen('http://py4e-data.dr-chuck.net/comments_1430669.html').read()
soup = BeautifulSoup(html,'html.parser')
tags = soup.find_all("span")
for i in tags:
  print(i.string)
import urllib.request
from bs4 import BeautifulSoup
html = urllib.request.urlopen('http://py4e-data.dr-chuck.net/comments_1430669.html').read()
soup = BeautifulSoup(html,'html.parser')
tags = soup.find_all("span")
comments = {}
for index, tag in enumerate(tags):
  commentorName = tag.find_previous('tr').text
  commentorComments = tag.string
  comments[commentorName] = commentorComments
print(comments)
{'Melodie100': '100', 'Machaela100': '100', 'Rhoan99': '99', 'Murrough96': '96', 'Lilygrace93': '93', 'Ellenor93': '93', 'Verity89': '89', 'Karlie88': '88', 'Berlin85': '85', 'Skylar84': '84', 'Benny84': '84', 'Crispin81': '81', 'Asya79': '79', 'Kadi76': '76', 'Dua74': '74', 'Stephany73': '73', 'Eila71': '71', 'Jennah70': '70', 'Eduardo67': '67', 'Shannan61': '61', 'Chymari60': '60', 'Inez60': '60', 'Charlene59': '59', 'Rosalin54': '54', 'James53': '53', 'Rhy53': '53', 'Zein52': '52', 'Ayren50': '50', 'Marissa46': '46', 'Mcbride46': '46', 'Ruben45': '45', 'Mikee41': '41', 'Carmel38': '38', 'Idahosa37': '37', 'Brooklin37': '37', 'Betsy36': '36', 'Kayah34': '34', 'Szymon26': '26', 'Tea24': '24', 'Queenie24': '24', 'Nima23': '23', 'Eassan23': '23', 'Haleema21': '21', 'Rahma17': '17', 'Rob17': '17', 'Roma16': '16', 'Jeffrey14': '14', 'Yorgos12': '12', 'Denon11': '11', 'Jasmina7': '7'}

Filter dataset from multiple columns of another dataset

copy iconCopydownload iconDownload
tmp = dfB.reindex(dfA['index'])
out = tmp[tmp['pet'].eq(dfA.set_index('index')['pet'])].rename_axis([None])
out = dfB[dfB.index.map(dfA.set_index('index')['pet']) == dfB['pet']]
    pet
2   dog
5   cat
9  bird
-----------------------
tmp = dfB.reindex(dfA['index'])
out = tmp[tmp['pet'].eq(dfA.set_index('index')['pet'])].rename_axis([None])
out = dfB[dfB.index.map(dfA.set_index('index')['pet']) == dfB['pet']]
    pet
2   dog
5   cat
9  bird
-----------------------
tmp = dfB.reindex(dfA['index'])
out = tmp[tmp['pet'].eq(dfA.set_index('index')['pet'])].rename_axis([None])
out = dfB[dfB.index.map(dfA.set_index('index')['pet']) == dfB['pet']]
    pet
2   dog
5   cat
9  bird
-----------------------
import pandas as pd

dfa = pd.DataFrame({'name': {0: 'Alice', 1: 'Bob', 2: 'Chuck', 3: 'Daren', 4: 'Emily'},
 'index': {0: 2, 1: 5, 2: 12, 3: 4, 4: 9},
 'pet': {0: 'dog', 1: 'cat', 2: 'cat', 3: 'bird', 4: 'bird'}})

dfb = pd.DataFrame({'pet': {0: 'dog',
  1: 'cat',
  2: 'dog',
  3: 'bird',
  4: 'cat',
  5: 'cat',
  6: 'bird',
  7: 'cat',
  8: 'bird',
  9: 'bird'}})

dfm = pd.merge(dfa, dfb, left_on=['index', 'pet'], right_on=[dfb.index, 'pet'])
dfm = dfm[['index', 'pet']].set_index('index', drop=True)
    pet
index   
2   dog
5   cat
9   bird
-----------------------
import pandas as pd

dfa = pd.DataFrame({'name': {0: 'Alice', 1: 'Bob', 2: 'Chuck', 3: 'Daren', 4: 'Emily'},
 'index': {0: 2, 1: 5, 2: 12, 3: 4, 4: 9},
 'pet': {0: 'dog', 1: 'cat', 2: 'cat', 3: 'bird', 4: 'bird'}})

dfb = pd.DataFrame({'pet': {0: 'dog',
  1: 'cat',
  2: 'dog',
  3: 'bird',
  4: 'cat',
  5: 'cat',
  6: 'bird',
  7: 'cat',
  8: 'bird',
  9: 'bird'}})

dfm = pd.merge(dfa, dfb, left_on=['index', 'pet'], right_on=[dfb.index, 'pet'])
dfm = dfm[['index', 'pet']].set_index('index', drop=True)
    pet
index   
2   dog
5   cat
9   bird
-----------------------
(df2.loc[pd.Series(df2.reset_index()
                   .to_records(index=False)
                   .tolist())
         .isin(df1[['index','pet']]
               .to_records(index=False)
               .tolist())])
    pet
2   dog
5   cat
9  bird
-----------------------
(df2.loc[pd.Series(df2.reset_index()
                   .to_records(index=False)
                   .tolist())
         .isin(df1[['index','pet']]
               .to_records(index=False)
               .tolist())])
    pet
2   dog
5   cat
9  bird

Sort dataframe by value in a tuple

copy iconCopydownload iconDownload
import pandas as pd

df = pd.DataFrame({
    'name': ['Alice', 'Bob', 'Chuck', 'Daren', 'Elisa'],
    'age': [8, 7, 9, 12, 13],
    'scores': [(False, 0, 89.1), (True, 136, 79.05), (True, 138, 75.0), (True, 146, 77.25), (True, 146, 77.25)],
})

print(df)
#    name  age              scores
#0  Alice    8    (False, 0, 89.1)
#1    Bob    7  (True, 136, 79.05)
#2  Chuck    9   (True, 138, 75.0)
#3  Daren   12  (True, 146, 77.25)
#4  Elisa   13  (True, 146, 77.25)

df2 = df[df['scores'].apply(lambda x: x[0])]

print(df2)
#    name  age              scores
#1    Bob    7  (True, 136, 79.05)
#2  Chuck    9   (True, 138, 75.0)
#3  Daren   12  (True, 146, 77.25)
#4  Elisa   13  (True, 146, 77.25)
-----------------------
df.loc[df.scores.str[0]==True]

Create loop to subset data by month and year

copy iconCopydownload iconDownload
# set as data.table
setDT(data_cleaning)


# create year month column
data_cleaning[, year_month := substr(date, 1, 7)]


# split and put into list
split(data_cleaning, data_cleaning$year_month)
-----------------------
library("tidyverse")
data_nested <- sample_data %>%
    mutate(year_month = format.Date(date, "%Y-%m")) %>%
    group_by(year_month) %>%
    nest()
map2(data_nested$data, data_nested$year_month,
     ~ write_csv(x = .x, file = paste0(.y, ".csv")))
-----------------------
library("tidyverse")
data_nested <- sample_data %>%
    mutate(year_month = format.Date(date, "%Y-%m")) %>%
    group_by(year_month) %>%
    nest()
map2(data_nested$data, data_nested$year_month,
     ~ write_csv(x = .x, file = paste0(.y, ".csv")))

Create new column using str.contains and based on if-else condition

copy iconCopydownload iconDownload
>>> import pandas as pd
>>> from io import StringIO

>>> df = pd.read_csv(StringIO("""
... id,url_text
... 1,Tim Kaine
... 2,Tim Kain
... 3,Tim
... 4,Lindsey Graham.com
... """), sep=',')
>>> df
    id  url_text
0   1   Tim Kaine
1   2   Tim Kain
2   3   Tim
3   4   Lindsey Graham.com
patterns = '(%s)' % '|'.join(pol_names_list)
df['pol_name_block'] = df['url_text'].str.extract(patterns)
    id  url_text            pol_name_block
0   1   Tim Kaine           Tim Kaine
1   2   Tim Kain            NaN
2   3   Tim                 NaN
3   4   Lindsey Graham.com  Lindsey Graham
-----------------------
>>> import pandas as pd
>>> from io import StringIO

>>> df = pd.read_csv(StringIO("""
... id,url_text
... 1,Tim Kaine
... 2,Tim Kain
... 3,Tim
... 4,Lindsey Graham.com
... """), sep=',')
>>> df
    id  url_text
0   1   Tim Kaine
1   2   Tim Kain
2   3   Tim
3   4   Lindsey Graham.com
patterns = '(%s)' % '|'.join(pol_names_list)
df['pol_name_block'] = df['url_text'].str.extract(patterns)
    id  url_text            pol_name_block
0   1   Tim Kaine           Tim Kaine
1   2   Tim Kain            NaN
2   3   Tim                 NaN
3   4   Lindsey Graham.com  Lindsey Graham
-----------------------
>>> import pandas as pd
>>> from io import StringIO

>>> df = pd.read_csv(StringIO("""
... id,url_text
... 1,Tim Kaine
... 2,Tim Kain
... 3,Tim
... 4,Lindsey Graham.com
... """), sep=',')
>>> df
    id  url_text
0   1   Tim Kaine
1   2   Tim Kain
2   3   Tim
3   4   Lindsey Graham.com
patterns = '(%s)' % '|'.join(pol_names_list)
df['pol_name_block'] = df['url_text'].str.extract(patterns)
    id  url_text            pol_name_block
0   1   Tim Kaine           Tim Kaine
1   2   Tim Kain            NaN
2   3   Tim                 NaN
3   4   Lindsey Graham.com  Lindsey Graham
-----------------------
>>> import pandas as pd
>>> from io import StringIO

>>> df = pd.read_csv(StringIO("""
... id,url_text
... 1,Tim Kaine
... 2,Tim Kain
... 3,Tim
... 4,Lindsey Graham.com
... """), sep=',')
>>> df
    id  url_text
0   1   Tim Kaine
1   2   Tim Kain
2   3   Tim
3   4   Lindsey Graham.com
patterns = '(%s)' % '|'.join(pol_names_list)
df['pol_name_block'] = df['url_text'].str.extract(patterns)
    id  url_text            pol_name_block
0   1   Tim Kaine           Tim Kaine
1   2   Tim Kain            NaN
2   3   Tim                 NaN
3   4   Lindsey Graham.com  Lindsey Graham
-----------------------
pattern = fr"({'|'.join(pol_names_list)})"
df['pol_name_block'] = df['url_text'].str.extract(pattern)
print(df)

# Output <- with the sample of @tlentali
   id        url_text  pol_name_block
0   1       Tim Kaine       Tim Kaine
1   2        Tim Kain             NaN
2   3             Tim             NaN
3   4  Lindsey Graham  Lindsey Graham
# New sample, same pattern
>>> df
   id                      url_text
0   1  Tim Kaine and Lindsey Graham
1   2                      Tim Kain
2   3                           Tim
3   4                Lindsey Graham

# findall
>>> df['url_text'].str.findall(pattern)
0    [Tim Kaine, Lindsey Graham]
1                             []
2                             []
3               [Lindsey Graham]
Name: url_text, dtype: object

# extractall
>>> df['url_text'].str.extractall(pattern)
                      0
  match                
0 0           Tim Kaine
  1      Lindsey Graham
3 0      Lindsey Graham

-----------------------
pattern = fr"({'|'.join(pol_names_list)})"
df['pol_name_block'] = df['url_text'].str.extract(pattern)
print(df)

# Output <- with the sample of @tlentali
   id        url_text  pol_name_block
0   1       Tim Kaine       Tim Kaine
1   2        Tim Kain             NaN
2   3             Tim             NaN
3   4  Lindsey Graham  Lindsey Graham
# New sample, same pattern
>>> df
   id                      url_text
0   1  Tim Kaine and Lindsey Graham
1   2                      Tim Kain
2   3                           Tim
3   4                Lindsey Graham

# findall
>>> df['url_text'].str.findall(pattern)
0    [Tim Kaine, Lindsey Graham]
1                             []
2                             []
3               [Lindsey Graham]
Name: url_text, dtype: object

# extractall
>>> df['url_text'].str.extractall(pattern)
                      0
  match                
0 0           Tim Kaine
  1      Lindsey Graham
3 0      Lindsey Graham

Combinatorics: St. Peter's Game algorithm

copy iconCopydownload iconDownload
-- Remove the n-th element (zero-indexed) of a run-length encoded sequence of a.
chuck :: Int -> [(a, Int)] -> [(a, Int)]
chuck _ [] = error "unexpected empty list"
chuck n ((a,m) : l)
  | n < m = rpt a (m-n-1) ++ l ++ rpt a n
  | otherwise = chuck (n-m) (l ++ [(a,m)])
  where rpt a 0 = []
        rpt a n = [(a,n)]
ghci
> line
[(1,4),(0,5),(1,2),(0,1),(1,3),(0,1),(1,1),(0,2),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1)]
> chuck 8 line
[(1,2),(0,1),(1,3),(0,1),(1,1),(0,2),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1),(1,4),(0,4)]
> chuck 8 $ chuck 8 line
[(0,1),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1),(1,4),(0,4),(1,2),(0,1),(1,3),(0,1),(1,1)]
tally :: [(Int,Int)] -> (Int, Int)
tally xs = (sum (map snd (filter ((== 0) . fst) xs)), sum (map snd (filter ((== 1) . fst) xs)))
> tally line
(15,15)
> tally $ chuck 8 line
(14,15)
> tally $ chuck 8 $ chuck 8 line
(13,15)
> :t iterate
iterate :: (a -> a) -> a -> [a]
> take 16 $ map tally $ iterate (chuck 8) line
[(15,15),(14,15),(13,15),(12,15),(11,15),(10,15),(9,15),(8,15),(7,15),(6,15),(5,15),(4,15),(3,15),(2,15),(1,15),(0,15)]
-----------------------
-- Remove the n-th element (zero-indexed) of a run-length encoded sequence of a.
chuck :: Int -> [(a, Int)] -> [(a, Int)]
chuck _ [] = error "unexpected empty list"
chuck n ((a,m) : l)
  | n < m = rpt a (m-n-1) ++ l ++ rpt a n
  | otherwise = chuck (n-m) (l ++ [(a,m)])
  where rpt a 0 = []
        rpt a n = [(a,n)]
ghci
> line
[(1,4),(0,5),(1,2),(0,1),(1,3),(0,1),(1,1),(0,2),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1)]
> chuck 8 line
[(1,2),(0,1),(1,3),(0,1),(1,1),(0,2),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1),(1,4),(0,4)]
> chuck 8 $ chuck 8 line
[(0,1),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1),(1,4),(0,4),(1,2),(0,1),(1,3),(0,1),(1,1)]
tally :: [(Int,Int)] -> (Int, Int)
tally xs = (sum (map snd (filter ((== 0) . fst) xs)), sum (map snd (filter ((== 1) . fst) xs)))
> tally line
(15,15)
> tally $ chuck 8 line
(14,15)
> tally $ chuck 8 $ chuck 8 line
(13,15)
> :t iterate
iterate :: (a -> a) -> a -> [a]
> take 16 $ map tally $ iterate (chuck 8) line
[(15,15),(14,15),(13,15),(12,15),(11,15),(10,15),(9,15),(8,15),(7,15),(6,15),(5,15),(4,15),(3,15),(2,15),(1,15),(0,15)]
-----------------------
-- Remove the n-th element (zero-indexed) of a run-length encoded sequence of a.
chuck :: Int -> [(a, Int)] -> [(a, Int)]
chuck _ [] = error "unexpected empty list"
chuck n ((a,m) : l)
  | n < m = rpt a (m-n-1) ++ l ++ rpt a n
  | otherwise = chuck (n-m) (l ++ [(a,m)])
  where rpt a 0 = []
        rpt a n = [(a,n)]
ghci
> line
[(1,4),(0,5),(1,2),(0,1),(1,3),(0,1),(1,1),(0,2),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1)]
> chuck 8 line
[(1,2),(0,1),(1,3),(0,1),(1,1),(0,2),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1),(1,4),(0,4)]
> chuck 8 $ chuck 8 line
[(0,1),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1),(1,4),(0,4),(1,2),(0,1),(1,3),(0,1),(1,1)]
tally :: [(Int,Int)] -> (Int, Int)
tally xs = (sum (map snd (filter ((== 0) . fst) xs)), sum (map snd (filter ((== 1) . fst) xs)))
> tally line
(15,15)
> tally $ chuck 8 line
(14,15)
> tally $ chuck 8 $ chuck 8 line
(13,15)
> :t iterate
iterate :: (a -> a) -> a -> [a]
> take 16 $ map tally $ iterate (chuck 8) line
[(15,15),(14,15),(13,15),(12,15),(11,15),(10,15),(9,15),(8,15),(7,15),(6,15),(5,15),(4,15),(3,15),(2,15),(1,15),(0,15)]
-----------------------
-- Remove the n-th element (zero-indexed) of a run-length encoded sequence of a.
chuck :: Int -> [(a, Int)] -> [(a, Int)]
chuck _ [] = error "unexpected empty list"
chuck n ((a,m) : l)
  | n < m = rpt a (m-n-1) ++ l ++ rpt a n
  | otherwise = chuck (n-m) (l ++ [(a,m)])
  where rpt a 0 = []
        rpt a n = [(a,n)]
ghci
> line
[(1,4),(0,5),(1,2),(0,1),(1,3),(0,1),(1,1),(0,2),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1)]
> chuck 8 line
[(1,2),(0,1),(1,3),(0,1),(1,1),(0,2),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1),(1,4),(0,4)]
> chuck 8 $ chuck 8 line
[(0,1),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1),(1,4),(0,4),(1,2),(0,1),(1,3),(0,1),(1,1)]
tally :: [(Int,Int)] -> (Int, Int)
tally xs = (sum (map snd (filter ((== 0) . fst) xs)), sum (map snd (filter ((== 1) . fst) xs)))
> tally line
(15,15)
> tally $ chuck 8 line
(14,15)
> tally $ chuck 8 $ chuck 8 line
(13,15)
> :t iterate
iterate :: (a -> a) -> a -> [a]
> take 16 $ map tally $ iterate (chuck 8) line
[(15,15),(14,15),(13,15),(12,15),(11,15),(10,15),(9,15),(8,15),(7,15),(6,15),(5,15),(4,15),(3,15),(2,15),(1,15),(0,15)]
-----------------------
-- Remove the n-th element (zero-indexed) of a run-length encoded sequence of a.
chuck :: Int -> [(a, Int)] -> [(a, Int)]
chuck _ [] = error "unexpected empty list"
chuck n ((a,m) : l)
  | n < m = rpt a (m-n-1) ++ l ++ rpt a n
  | otherwise = chuck (n-m) (l ++ [(a,m)])
  where rpt a 0 = []
        rpt a n = [(a,n)]
ghci
> line
[(1,4),(0,5),(1,2),(0,1),(1,3),(0,1),(1,1),(0,2),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1)]
> chuck 8 line
[(1,2),(0,1),(1,3),(0,1),(1,1),(0,2),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1),(1,4),(0,4)]
> chuck 8 $ chuck 8 line
[(0,1),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1),(1,4),(0,4),(1,2),(0,1),(1,3),(0,1),(1,1)]
tally :: [(Int,Int)] -> (Int, Int)
tally xs = (sum (map snd (filter ((== 0) . fst) xs)), sum (map snd (filter ((== 1) . fst) xs)))
> tally line
(15,15)
> tally $ chuck 8 line
(14,15)
> tally $ chuck 8 $ chuck 8 line
(13,15)
> :t iterate
iterate :: (a -> a) -> a -> [a]
> take 16 $ map tally $ iterate (chuck 8) line
[(15,15),(14,15),(13,15),(12,15),(11,15),(10,15),(9,15),(8,15),(7,15),(6,15),(5,15),(4,15),(3,15),(2,15),(1,15),(0,15)]
-----------------------
-- Remove the n-th element (zero-indexed) of a run-length encoded sequence of a.
chuck :: Int -> [(a, Int)] -> [(a, Int)]
chuck _ [] = error "unexpected empty list"
chuck n ((a,m) : l)
  | n < m = rpt a (m-n-1) ++ l ++ rpt a n
  | otherwise = chuck (n-m) (l ++ [(a,m)])
  where rpt a 0 = []
        rpt a n = [(a,n)]
ghci
> line
[(1,4),(0,5),(1,2),(0,1),(1,3),(0,1),(1,1),(0,2),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1)]
> chuck 8 line
[(1,2),(0,1),(1,3),(0,1),(1,1),(0,2),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1),(1,4),(0,4)]
> chuck 8 $ chuck 8 line
[(0,1),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1),(1,4),(0,4),(1,2),(0,1),(1,3),(0,1),(1,1)]
tally :: [(Int,Int)] -> (Int, Int)
tally xs = (sum (map snd (filter ((== 0) . fst) xs)), sum (map snd (filter ((== 1) . fst) xs)))
> tally line
(15,15)
> tally $ chuck 8 line
(14,15)
> tally $ chuck 8 $ chuck 8 line
(13,15)
> :t iterate
iterate :: (a -> a) -> a -> [a]
> take 16 $ map tally $ iterate (chuck 8) line
[(15,15),(14,15),(13,15),(12,15),(11,15),(10,15),(9,15),(8,15),(7,15),(6,15),(5,15),(4,15),(3,15),(2,15),(1,15),(0,15)]
-----------------------
-- Remove the n-th element (zero-indexed) of a run-length encoded sequence of a.
chuck :: Int -> [(a, Int)] -> [(a, Int)]
chuck _ [] = error "unexpected empty list"
chuck n ((a,m) : l)
  | n < m = rpt a (m-n-1) ++ l ++ rpt a n
  | otherwise = chuck (n-m) (l ++ [(a,m)])
  where rpt a 0 = []
        rpt a n = [(a,n)]
ghci
> line
[(1,4),(0,5),(1,2),(0,1),(1,3),(0,1),(1,1),(0,2),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1)]
> chuck 8 line
[(1,2),(0,1),(1,3),(0,1),(1,1),(0,2),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1),(1,4),(0,4)]
> chuck 8 $ chuck 8 line
[(0,1),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1),(1,4),(0,4),(1,2),(0,1),(1,3),(0,1),(1,1)]
tally :: [(Int,Int)] -> (Int, Int)
tally xs = (sum (map snd (filter ((== 0) . fst) xs)), sum (map snd (filter ((== 1) . fst) xs)))
> tally line
(15,15)
> tally $ chuck 8 line
(14,15)
> tally $ chuck 8 $ chuck 8 line
(13,15)
> :t iterate
iterate :: (a -> a) -> a -> [a]
> take 16 $ map tally $ iterate (chuck 8) line
[(15,15),(14,15),(13,15),(12,15),(11,15),(10,15),(9,15),(8,15),(7,15),(6,15),(5,15),(4,15),(3,15),(2,15),(1,15),(0,15)]
-----------------------
-- Remove the n-th element (zero-indexed) of a run-length encoded sequence of a.
chuck :: Int -> [(a, Int)] -> [(a, Int)]
chuck _ [] = error "unexpected empty list"
chuck n ((a,m) : l)
  | n < m = rpt a (m-n-1) ++ l ++ rpt a n
  | otherwise = chuck (n-m) (l ++ [(a,m)])
  where rpt a 0 = []
        rpt a n = [(a,n)]
ghci
> line
[(1,4),(0,5),(1,2),(0,1),(1,3),(0,1),(1,1),(0,2),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1)]
> chuck 8 line
[(1,2),(0,1),(1,3),(0,1),(1,1),(0,2),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1),(1,4),(0,4)]
> chuck 8 $ chuck 8 line
[(0,1),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1),(1,4),(0,4),(1,2),(0,1),(1,3),(0,1),(1,1)]
tally :: [(Int,Int)] -> (Int, Int)
tally xs = (sum (map snd (filter ((== 0) . fst) xs)), sum (map snd (filter ((== 1) . fst) xs)))
> tally line
(15,15)
> tally $ chuck 8 line
(14,15)
> tally $ chuck 8 $ chuck 8 line
(13,15)
> :t iterate
iterate :: (a -> a) -> a -> [a]
> take 16 $ map tally $ iterate (chuck 8) line
[(15,15),(14,15),(13,15),(12,15),(11,15),(10,15),(9,15),(8,15),(7,15),(6,15),(5,15),(4,15),(3,15),(2,15),(1,15),(0,15)]
-----------------------
line = [(1,4),(0,5),(1,2),(0,1),(1,3),(0,1),(1,1),
        (0,2),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1)]
unRLE rle = [c | (c,n) <- rle, c <- replicate n c]
test = count9 1 (sum [n | (0,n) <- line]) -- 15 
                [] $ unRLE line

count9 _ 0 rev line   = reverse rev ++ line
count9 9 n rev (0:xs) = count9 1 (n-1) rev xs
 -- removing 1 is error:
count9 9 n rev (1:xs) = error "attempt to remove 1"
count9 i n rev (x:xs) = count9 (i+1) n (x:rev) xs
count9 i n rev []     = count9 i n [] (reverse rev)
> test
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
-----------------------
line = [(1,4),(0,5),(1,2),(0,1),(1,3),(0,1),(1,1),
        (0,2),(1,2),(0,3),(1,1),(0,2),(1,2),(0,1)]
unRLE rle = [c | (c,n) <- rle, c <- replicate n c]
test = count9 1 (sum [n | (0,n) <- line]) -- 15 
                [] $ unRLE line

count9 _ 0 rev line   = reverse rev ++ line
count9 9 n rev (0:xs) = count9 1 (n-1) rev xs
 -- removing 1 is error:
count9 9 n rev (1:xs) = error "attempt to remove 1"
count9 i n rev (x:xs) = count9 (i+1) n (x:rev) xs
count9 i n rev []     = count9 i n [] (reverse rev)
> test
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]

Cannot get values from request in Django - Empty QueryDict

copy iconCopydownload iconDownload
import json

data = json.loads(request.body)
print(data['title'])
import json

print(request.data['title'])
-----------------------
import json

data = json.loads(request.body)
print(data['title'])
import json

print(request.data['title'])

How to display plain text in browser, fetching from API

copy iconCopydownload iconDownload
result.innerText = jsonResult.value.joke;
-----------------------
const fetchDataBtn = document.querySelector("#fetchdata");
const result = document.querySelector("#result");

// gets data from API and sets the content of #result div
async function getData() {
  result.innerText = "Loading....";
  try {
    const res = await fetch("http://api.icndb.com/jokes/random");
    const jsonResult = await res.json();
    result.textContent = jsonResult.value.joke;
  } catch (error) {
    console.log(error);
  }
}

// add event listener for #fetch-data button
fetchDataBtn.addEventListener("click", getData);
<div class="landing">
  <div class="title">
    <h1>Chuck Norris Jokes</h1>
  </div>
  <button id="fetchdata">Get More Jokes</button>
  <div id="result"></div>
</div>
-----------------------
const fetchDataBtn = document.querySelector("#fetchdata");
const result = document.querySelector("#result");

// gets data from API and sets the content of #result div
async function getData() {
  result.innerText = "Loading....";
  try {
    const res = await fetch("http://api.icndb.com/jokes/random");
    const jsonResult = await res.json();
    result.textContent = jsonResult.value.joke;
  } catch (error) {
    console.log(error);
  }
}

// add event listener for #fetch-data button
fetchDataBtn.addEventListener("click", getData);
<div class="landing">
  <div class="title">
    <h1>Chuck Norris Jokes</h1>
  </div>
  <button id="fetchdata">Get More Jokes</button>
  <div id="result"></div>
</div>

Community Discussions

Trending Discussions on chuck
  • Having an issue in saving fitted and observed values in lm model?
  • sending info from api to discord in a message (discord.js)
  • Select repeated values using an array containing repeated elements
  • How to iterate through all tags of a website in Python with Beautifulsoup?
  • Filter dataset from multiple columns of another dataset
  • Sort dataframe by value in a tuple
  • Create loop to subset data by month and year
  • Create new column using str.contains and based on if-else condition
  • Combinatorics: St. Peter's Game algorithm
  • Cannot get values from request in Django - Empty QueryDict
Trending Discussions on chuck

QUESTION

Having an issue in saving fitted and observed values in lm model?

Asked 2022-Apr-09 at 12:31

I run a linear regression model with looping over the categorical levels of one column of my data. I want to save some of the model summary output.

I achieved to save some of them include coefficients, R2 and Pvalue. However, when I save the fitted and observed values in a data.frame called obsFit, it returns the fitted and observed values of only one categorical level as I need 7 levels.

Any ideas and thoughts?

So sorry for hardcoding R, I am still a novice who is in the learning process.

Here is the chuck of code I used.

# Wilayah 1 vs MADA4 model
> Y1 <- yield[ ,2:5]
> mada4 <- stns[["S1"]][["Mada4_sum"]]
> unique(mada4$aggre_per)
[1] "3"     "4"     "5"     "6"     "7"     "mamjj"
[7] "amj"  
> 
> # create an empty matrix to fill results
> df <- matrix(rep(NA, 9))
> 
> for (agg.per in unique(mada4$aggre_per)) {
+   
+   subdata <- subset(mada4, aggre_per == agg.per)
+   subdata <- cbind(subdata, Y1$I_S1)
+   names(subdata)[4] <- "I_S1"
+   
+   #save model summary
+   mod <- lm('I_S1 ~ value', subdata)
+   slope     <- summary(mod)[[4]][2,1]
+   intercept <- summary(mod)[[4]][1,1]
+   r2        <- summary(mod)[[8]]
+   adjr2     <- summary(mod)[[9]]
+   fstat     <- summary(mod)[[10]][[1]]
+   pval      <- summary(mod)[[4]][2,4]
+   pearson   <- cor(subdata$I_S1,subdata$value )
+   res <- c("Mada4_sum", agg.per, slope, intercept, r2, adjr2, fstat, pval, pearson)
+   df <- cbind(df, res)
+   
+   #save the fitted and observed yields as data.frame
+   obsFit <- data.frame(cbind("Mada4_sum", agg.per,subdata$I_S1, fitted(mod)))
+   
+ }
> 
> df <- data.frame(t(df[,-1]))
> df[,-2:-1] <- apply (df[,-2:-1], 2, as.numeric)
> df[,-2:-1] <- apply (df[,-2:-1], 2, round, digit =2)
> rownames(df) <- 1:nrow(df)
> names(df) <-c( "WI",
+                "aggre_per",
+                "slope",
+                "intercept",
+                "R2",
+                "adjustedR2",
+                "F-stat",
+                "p-value",
+                "correlation")
> head(df)
         WI aggre_per slope intercept   R2 adjustedR2
1 Mada4_sum         3 -1.17   6230.91 0.09       0.03
2 Mada4_sum         4 -0.27   6128.81 0.00      -0.07
3 Mada4_sum         5 -1.04   6240.46 0.02      -0.05
4 Mada4_sum         6 -2.88   6507.82 0.14       0.08
5 Mada4_sum         7 -1.12   6296.72 0.05      -0.02
6 Mada4_sum     mamjj -0.89   6760.92 0.18       0.12
  F-stat p-value correlation
1   1.40    0.26       -0.30
2   0.07    0.80       -0.07
3   0.23    0.64       -0.13
4   2.26    0.15       -0.37
5   0.76    0.40       -0.23
6   3.12    0.10       -0.43
> #check levels of `aggre_per`
> unique(df$aggre_per)
[1] "3"     "4"     "5"     "6"     "7"     "mamjj"
[7] "amj"  
> 
> names(obsFit) <- c("WI", "Aggr.per", "Observed", "Fitted")
> print(obsFit)
           WI Aggr.per Observed           Fitted
103 Mada4_sum      amj     5204 6019.05130014577
104 Mada4_sum      amj     5824 6061.66361454587
105 Mada4_sum      amj     5481 6129.49546195825
106 Mada4_sum      amj     5587 5962.52476063545
107 Mada4_sum      amj     6260 6124.27762754192
108 Mada4_sum      amj     5771   6017.312022007
109 Mada4_sum      amj     5899 5888.60543973734
110 Mada4_sum      amj     6338 6189.50055774613
111 Mada4_sum      amj     6340 6023.39949549272
112 Mada4_sum      amj     6013 6146.01860427665
113 Mada4_sum      amj     6075 6221.67720331355
114 Mada4_sum      amj     6484 5959.91584342728
115 Mada4_sum      amj     6771 5998.17996248043
116 Mada4_sum      amj     6493 6249.50565353401
117 Mada4_sum      amj     6386 6290.37868979533
118 Mada4_sum      amj     6465 6109.49376336229
> 

Hereis the dput of my data?

> dput(mada4)
structure(list(year = c(2001, 2002, 2003, 2004, 2005, 2006, 2007, 
2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2001, 2002, 
2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 
2014, 2015, 2016, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 
2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2001, 2002, 2003, 
2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 
2015, 2016, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 
2010, 2011, 2012, 2013, 2014, 2015, 2016, 2001, 2002, 2003, 2004, 
2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 
2016, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 
2011, 2012, 2013, 2014, 2015, 2016), aggre_per = c("3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", 
"4", "4", "4", "4", "5", "5", "5", "5", "5", "5", "5", "5", "5", 
"5", "5", "5", "5", "5", "5", "5", "6", "6", "6", "6", "6", "6", 
"6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "7", "7", "7", 
"7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", 
"mamjj", "mamjj", "mamjj", "mamjj", "mamjj", "mamjj", "mamjj", 
"mamjj", "mamjj", "mamjj", "mamjj", "mamjj", "mamjj", "mamjj", 
"mamjj", "mamjj", "amj", "amj", "amj", "amj", "amj", "amj", "amj", 
"amj", "amj", "amj", "amj", "amj", "amj", "amj", "amj", "amj"
), value = c(189, 6, 212, 81, 101, 84, 163, 198, 143, 128, 456, 
93, 37, 23, 58, 0, 210, 196, 70, 122, 132, 184, 387, 57, 232, 
153, 11, 325, 228, 75, 41, 31, 141, 95, 219, 217, 178, 154, 139, 
112, 164, 62, 106, 202, 101, 123, 108, 252, 175, 186, 110, 252, 
95, 190, 150, 161, 125, 165, 176, 67, 221, 63, 65, 139, 100, 
267, 207, 202, 48, 322, 240, 250, 270, 298, 159, 164, 178, 29, 
94, 164, 815, 750, 818, 874, 554, 934, 1079, 778, 934, 806, 908, 
851, 765, 313, 366, 586, 526, 477, 399, 591, 405, 528, 676, 330, 
521, 380, 293, 594, 550, 261, 214, 422)), row.names = c(1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 
31L, 32L, 33L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 
45L, 46L, 47L, 48L, 49L, 50L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 
59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 69L, 70L, 71L, 72L, 
73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 86L, 
87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 
100L, 101L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 
112L, 113L, 114L, 115L, 116L, 117L, 118L), class = "data.frame")
> 
> dput(yield)
structure(list(date = structure(c(11323, 11688, 12053, 12418, 
12784, 13149, 13514, 13879, 14245, 14610, 14975, 15340, 15706, 
16071, 16436, 16801), class = "Date"), I_S1 = c(5204, 5824, 5481, 
5587, 6260, 5771, 5899, 6338, 6340, 6013, 6075, 6484, 6771, 6493, 
6386, 6465), II_S1 = c(5081, 5674, 5589, 5809, 6055, 6231, 5728, 
5971, 6268, 5999, 6015, 6624, 6827, 6453, 6382, 6340), III_S1 = c(5071, 
5349, 5703, 5451, 5764, 5528, 5629, 5395, 5797, 5785, 5887, 5581, 
5899, 6179, 6086, 5734), IV_S1 = c(5515, 5491, 5969, 5971, 6265, 
6242, 6153, 6188, 6311, 6447, 6213, 6410, 6744, 6754, 6247, 6415
), I_S2 = c(4735, 5227, 5415, 5894, 3294, 5113, 6056, 6216, 5752, 
5758, 5540, 5418, 6461, 5346, 6437, 5633), II_S2 = c(4837, 5209, 
5111, 5482, 3203, 5389, 5209, 6041, 5617, 5927, 4993, 5342, 6380, 
5122, 6320, 5470), III_S2 = c(4720, 4986, 4846, 4720, 3968, 4912, 
4437, 5675, 5368, 5638, 4892, 4849, 6117, 4996, 5586, 4666), 
    IV_S2 = c(5054, 5584, 5546, 5666, 5116, 6288, 6089, 6529, 
    5880, 6156, 5263, 5358, 6804, 5212, 6174, 5560)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -16L))

ANSWER

Answered 2022-Apr-09 at 12:31

You could do this in a by which splits the data by condition and applies a function, later rbind the resulting list. The obsFit you could pass through as an attribute.

res <- by(mada4, mada4$aggre_per, \(x) {
  x <- cbind(x, yield[ ,2:5])
  sm <- summary(mod <- lm(I_S1 ~ value, x))
  o <- setNames(as.vector(sm$coefficients[, c(1, 4)])[c(1, 2, 4)],
                c('Intercept', 'slope', 'pval'))
  o <- c(o, sapply(sm[c('r.squared', 'adj.r.squared', 'fstatistic')], `[`, 1))
  o <- c(o, pearson=with(x, cor(I_S1, value)))[c(2, 1, 4, 5, 6, 3, 7)]
  ft <- cbind(x, x$I_S1, fitted(mod))
  return(`attr<-`(o, 'obsFit', ft))
})

Results

do.call(rbind, res) |> signif(4)
#         slope Intercept r.squared adj.r.squared fstatistic.value    pval  pearson
# 3     -1.1680      6231   0.09105      0.026130          1.40200 0.25600 -0.30170
# 4     -0.2730      6129   0.00464     -0.066460          0.06526 0.80210 -0.06812
# 5     -1.0350      6240   0.01636     -0.053900          0.23290 0.63680 -0.12790
# 6     -2.8780      6508   0.13910      0.077660          2.26300 0.15470 -0.37300
# 7     -1.1220      6297   0.05134     -0.016420          0.75760 0.39880 -0.22660
# amj   -0.8696      6476   0.07058      0.004188          1.06300 0.32000 -0.26570
# mamjj -0.8889      6761   0.18210      0.123700          3.11700 0.09926 -0.42670

obsFits

obsFits <- lapply(res, attr, 'obsFit')
obsFits$`3`  ## first aggre_per `3`
#    year aggre_per value I_S1 II_S1 III_S1 IV_S1 x$I_S1 fitted(mod)
# 1  2001         3   189 5204  5081   5071  5515   5204    6010.131
# 2  2002         3     6 5824  5674   5349  5491   5824    6223.904
# 3  2003         3   212 5481  5589   5703  5969   5481    5983.264
# 4  2004         3    81 5587  5809   5451  5971   5587    6136.292
# 5  2005         3   101 6260  6055   5764  6265   6260    6112.929
# 6  2006         3    84 5771  6231   5528  6242   5771    6132.788
# 7  2007         3   163 5899  5728   5629  6153   5899    6040.503
# 8  2008         3   198 6338  5971   5395  6188   6338    5999.618
# 9  2009         3   143 6340  6268   5797  6311   6340    6063.866
# 10 2010         3   128 6013  5999   5785  6447   6013    6081.389
# 11 2011         3   456 6075  6015   5887  6213   6075    5698.234
# 12 2012         3    93 6484  6624   5581  6410   6484    6122.274
# 13 2013         3    37 6771  6827   5899  6744   6771    6187.691
# 14 2014         3    23 6493  6453   6179  6754   6493    6204.045
# 15 2015         3    58 6386  6382   6086  6247   6386    6163.160
# 16 2016         3     0 6465  6340   5734  6415   6465    6230.913

Note: R >= 4.1 used.

Source https://stackoverflow.com/questions/71807304

Community Discussions, Code Snippets contain sources that include Stack Exchange Network

Vulnerabilities

No vulnerabilities reported

Install chuck

Add the dependency in your build.gradle file. Add it alongside the no-op variant to isolate Chuck from release builds as follows:.

Support

For any new features, suggestions and bugs create an issue on GitHub. If you have any questions check and ask questions on community page Stack Overflow .

DOWNLOAD this Library from

Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
over 430 million Knowledge Items
Find more libraries
Reuse Solution Kits and Libraries Curated by Popular Use Cases

Save this library and start creating your kit

Share this Page

share link
Consider Popular Reactive Programming Libraries
Compare Reactive Programming Libraries with Highest Support
Compare Reactive Programming Libraries with Highest Quality
Compare Reactive Programming Libraries with Highest Security
Compare Reactive Programming Libraries with Permissive License
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
over 430 million Knowledge Items
Find more libraries
Reuse Solution Kits and Libraries Curated by Popular Use Cases

Save this library and start creating your kit

  • © 2022 Open Weaver Inc.