{"id":190,"date":"2026-09-03T10:42:01","date_gmt":"2026-09-03T10:42:01","guid":{"rendered":"https:\/\/alpeshconnect.in\/blog\/?p=190"},"modified":"2026-09-03T10:44:03","modified_gmt":"2026-09-03T10:44:03","slug":"write-r-script-for-customer-details","status":"publish","type":"post","link":"https:\/\/alpeshconnect.in\/blog\/2026\/09\/03\/write-r-script-for-customer-details\/","title":{"rendered":"Write R script For Customer Details"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Write R script to connect to a SQLite database and import a table named customer, which contains columns: CustID, CustName, Bill, and City. Display the imported data using R commands<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Step 1: Install and load required packages<br># install.packages(\"RSQLite\")  # Run this once if not installed<br>library(RSQLite)<br><br># Step 2: Create a SQLite database and insert sample Indian customer data<br># (In exam, database already exists, but for demonstration we'll create one)<br><br># Create a connection to a new SQLite database file<br>conn &lt;- dbConnect(SQLite(), dbname = \"customer_data.db\")<br><br># Create the customer table with sample Indian data<br>customer_data &lt;- data.frame(<br>  CustID = c(101, 102, 103, 104, 105, 106, 107, 108),<br>  CustName = c(\"Rajesh Kumar\", \"Priya Sharma\", \"Amit Patel\", \"Sneha Reddy\", <br>               \"Vikram Singh\", \"Ananya Gupta\", \"Rahul Joshi\", \"Meera Nair\"),<br>  Bill = c(1500, 2500, 3200, 1800, 4500, 2100, 3800, 2900),<br>  City = c(\"Mumbai\", \"Delhi\", \"Ahmedabad\", \"Hyderabad\", <br>           \"Jaipur\", \"Kolkata\", \"Pune\", \"Chennai\")<br>)<br><br># Write the data to the database table<br>dbWriteTable(conn, \"customer\", customer_data, overwrite = TRUE)<br><br># Step 3: Import the customer table from the database<br># Query to select all data from customer table<br>imported_data &lt;- dbGetQuery(conn, \"SELECT * FROM customer\")<br><br># Step 4: Display the imported data<br>cat(\"\\n========== Imported Customer Data ==========\\n\")<br>print(imported_data)<br><br># Step 5: Display summary and structure of the data<br>cat(\"\\n========== Structure of Data ==========\\n\")<br>str(imported_data)<br><br>cat(\"\\n========== Summary of Data ==========\\n\")<br>summary(imported_data)<br><br># Step 6: Display specific columns<br>cat(\"\\n========== Customer Names and Bills ==========\\n\")<br>print(imported_data[, c(\"CustName\", \"Bill\")])<br><br># Step 7: Display customers from specific city (e.g., Mumbai)<br>cat(\"\\n========== Customers from Mumbai ==========\\n\")<br>mumbai_customers &lt;- subset(imported_data, City == \"Mumbai\")<br>print(mumbai_customers)<br><br># Step 8: Close the database connection<br>dbDisconnect(conn)<br><br># Alternative method: If database already exists, just connect and import<br># conn &lt;- dbConnect(SQLite(), dbname = \"existing_database.db\")<br># imported_data &lt;- dbGetQuery(conn, \"SELECT * FROM customer\")<br># print(imported_data)<br># dbDisconnect(conn)<br><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>After importing the customer table from the SQLite database, create a histogram of the bill column.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Step 1: Load required libraries\nlibrary(RSQLite)\nlibrary(ggplot2)  # For advanced plotting\n\n# Step 2: Connect to database and import data\nconn &lt;- dbConnect(SQLite(), dbname = \"customer_data.db\")\ncustomer_data &lt;- dbGetQuery(conn, \"SELECT * FROM customer\")\n\n# Step 3: Create histogram using base R\ncat(\"\\n========== Creating Histogram (Base R) ==========\\n\")\n\n# Set up graphics parameters for better display\npar(mfrow = c(1, 2))  # For side-by-side plots\n\n# Histogram 1: Basic histogram with default settings\nhist(customer_data$Bill,\n     main = \"Distribution of Customer Bills\",\n     xlab = \"Bill Amount (\u20b9)\",\n     ylab = \"Number of Customers\",\n     col = \"lightblue\",\n     border = \"black\",\n     breaks = 5)  # Number of bins\n\n# Histogram 2: Enhanced histogram with more details\nhist(customer_data$Bill,\n     main = \"Bill Distribution Analysis\",\n     xlab = \"Bill Amount (\u20b9)\",\n     ylab = \"Frequency\",\n     col = c(\"#FF6B6B\", \"#4ECDC4\", \"#45B7D1\", \"#96CEB4\", \"#FFEAA7\"),\n     border = \"white\",\n     breaks = 6,\n     probability = TRUE)  # Show density instead of frequency\n\n# Add density curve\nlines(density(customer_data$Bill), col = \"red\", lwd = 2)\n\n# Add vertical lines for mean and median\nabline(v = mean(customer_data$Bill), col = \"blue\", lwd = 2, lty = 2)\nabline(v = median(customer_data$Bill), col = \"green\", lwd = 2, lty = 3)\n\n# Add legend\nlegend(\"topright\", \n       legend = c(\"Mean\", \"Median\", \"Density\"),\n       col = c(\"blue\", \"green\", \"red\"),\n       lty = c(2, 3, 1),\n       lwd = 2,\n       cex = 0.8)\n\n# Step 4: Create histogram using ggplot2 (More modern and attractive)\ncat(\"\\n========== Creating Histogram (ggplot2) ==========\\n\")\n\n# Create a data frame with bill categories\ncustomer_data$Bill_Category &lt;- cut(customer_data$Bill, \n                                   breaks = c(0, 2000, 3000, 4000, 5000),\n                                   labels = c(\"Low (0-2000)\", \"Medium (2000-3000)\", \n                                              \"High (3000-4000)\", \"Very High (4000+)\"))\n\n# Basic ggplot histogram\np &lt;- ggplot(customer_data, aes(x = Bill)) +\n  geom_histogram(binwidth = 500, \n                 fill = \"#2E86AB\", \n                 color = \"white\",\n                 alpha = 0.7) +\n  labs(title = \"Distribution of Customer Bills\",\n       subtitle = \"Sample Indian Customer Data\",\n       x = \"Bill Amount (\u20b9)\",\n       y = \"Number of Customers\") +\n  theme_minimal() +\n  theme(plot.title = element_text(hjust = 0.5, face = \"bold\"),\n        plot.subtitle = element_text(hjust = 0.5))\n\nprint(p)\n\n# Step 5: Additional analysis - Bill statistics by city\ncat(\"\\n========== Bill Statistics by City ==========\\n\")\ncity_stats &lt;- aggregate(Bill ~ City, data = customer_data, FUN = summary)\nprint(city_stats)\n\n# Step 6: Create bar plot of average bill by city\ncity_avg &lt;- aggregate(Bill ~ City, data = customer_data, FUN = mean)\nbarplot(city_avg$Bill,\n        names.arg = city_avg$City,\n        col = rainbow(length(city_avg$City)),\n        main = \"Average Bill by City\",\n        xlab = \"City\",\n        ylab = \"Average Bill (\u20b9)\",\n        ylim = c(0, 3500))\n\n# Add text labels on bars\ntext(x = 1:length(city_avg$Bill), \n     y = city_avg$Bill + 100, \n     labels = paste(\"\u20b9\", round(city_avg$Bill, 0)),\n     cex = 0.8)\n\n# Step 7: Create boxplot to compare bills across cities\nboxplot(Bill ~ City, data = customer_data,\n        main = \"Bill Distribution by City\",\n        xlab = \"City\",\n        ylab = \"Bill Amount (\u20b9)\",\n        col = c(\"lightblue\", \"lightgreen\", \"lightpink\", \"lightyellow\",\n                \"lightcyan\", \"lavender\", \"moccasin\", \"peachpuff\"))\n\n# Step 8: Close database connection\ndbDisconnect(conn)\n\n# Step 9: Save plots (optional)\n# ggsave(\"customer_bill_histogram.png\", plot = p, width = 8, height = 6)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Write R script to connect to a SQLite database and import a table named customer, which contains columns: CustID, CustName, Bill, and City. Display the imported data using R commands # Step 1: Install and load required packages# install.packages(&#8220;RSQLite&#8221;) # Run this once if not installedlibrary(RSQLite)# Step 2: Create a SQLite database and insert sample [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":189,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,54],"tags":[25,26,56,57],"class_list":["post-190","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bca","category-statistical-analysis-using-r","tag-bca","tag-bca-paper-solution","tag-r-language","tag-statistical-analysis-using-r"],"_links":{"self":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/comments?post=190"}],"version-history":[{"count":3,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/190\/revisions"}],"predecessor-version":[{"id":193,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/190\/revisions\/193"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/media\/189"}],"wp:attachment":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/media?parent=190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/categories?post=190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/tags?post=190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}