Science_blog: R

Search This Blog

Showing posts with label R. Show all posts
Showing posts with label R. Show all posts

Thursday, 2 March 2023

Create dataframe in R

#####################How to create dataframe of different variables from .csv############

> install.packages ("tidyverse")

Check this link for installing tidyverse on updated Ubuntu

(https://rajivagriscienceblog.blogspot.com/2023/02/how-to-install-r-library-tidyverse-for.html)

> setwd("/home/rajiv/for_1695_datast_confidential/04_weather")

if it is windows then it should be ("C: //home/rajiv/for_1695_datast_confidential/04_weather")

> library('tidyverse')

> ec1 <- read.csv("ec1_weather_data.csv", header = TRUE, sep = ",")

1. data.frame

ec1weather <- data.frame(ec1$site,

                                      ec1$date,

                                      ec1$temeprarute)


2. Tibble

> ec1weather <- tibble(ec1$site,

                                      ec1$date,

                                      ec1$temeprarute)

3. cbind


> ec1weather <- cbind(ec1$site,

                                      ec1$date,

                                      ec1$temeprarute)

Note: The basic difference between three is that cbind and data.frame convert all variables in character (chr) but tibble convert as it is ie.e date to date, int to int and chr to chr. Remember type of data or class or str of data is necessary before to data analysis.  If date is chr then you can not plot a graph with ggplot. It will show error "error discrete value supplied to continuous scale"

To plot a graph with ggplot, check here:
https://rajivagriscienceblog.blogspot.com/2023/02/plooting-with-common-axis-in-ggarrange.html

Tuesday, 28 February 2023

How to insert date in row in R

 #########Import your file#########

> culti <- read.csv("cultivation2.csv", header = TRUE, sep = ",")

> culti$date <- as.Date(culti$hdate)

> cult2 <- data.frame(culti$date,   culti$site,    culti$code)

> colnames(cult2) <- c("date", "site", "crop")

> cutiec1 <- cult2 %>%

  filter(site == 1)


###########Do not forget to check your structure of data#######

> str(cutiec1) ("It should be character otherwise it would not work")

'data.frame': 11 obs. of  3 variables:

 $ date: Date, format: "2009-12-12" ...

 $ site: int  1 1 1 1 1 1 1 1 1 1 ...

 $ crop: chr  "CC" "SM" "WW" "WR" ...

####Change date to character format all rows

> cutiec1$date <- as.character(cutiec1$date)

> cutiec1$site <- as.character(cutiec1$site)

###option 1#####

> cutiec1[nrow(cutiec1)+ 1, ] <- c("2018-09-29")

but this would create problems like this 

date       site       crop

1  2009-08-02          1       <NA>

2  2009-12-12          1         CC

3  2010-10-14          1         SM

4  2011-07-28          1         WW

5  2012-07-20          1         WR

6  2013-08-04          1         WW

7  2013-12-11          1         CC

8  2014-10-09          1         SM

9  2015-07-22          1         WW

10 2016-11-01          1         GM

11 2017-07-30          1         WW

12 2018-07-09          1         WR

13 2018-09-29 2018-09-29 2018-09-29

Hence follow this ---

############Option 2##########

> cutiec1 <- cutiec1 %>% add_row(date="2009-08-02", site="1", .before = 1)

> cutiec1 <- cutiec1 %>% add_row(date="2018-09-29", site="1", .after = 12)

######then character date to POSIXct date

> cutiec1$date <- as.POSIXct(cutiec1$date, tz = "UTC")

#########Then convert POSIXct date to date format

> cutiec1$date <- as.Date(cutiec1$date)

Then do "pad". It will creat date from 2009-08-02 to 2018-09-29 at interval of day

> cutiec12 <- pad(cutiec1, interval = "day",)


It shows 3346 variables for all 9 years on daily basis.


#############################Done##############################################

Monday, 20 February 2023

ggarrange: Plotting with common axis in R

##########Import your file####################################

> setwd("/home/rajiv/for_1695_datast_confidential/04_weather")

> ec1w <- read.csv("ec1_weather_data.csv", header = TRUE, sep = ",")

> head(ec1w)

###Convert date to as date with column "Date"

> ec1w$Date <- as.Date(ec1w$date) 

############How to plot graph in ggplot2######################

1. > install.packages("ggplot2")

> library('ggplot2')

###plotting graph with ggplot2

> ggplot2(ec4w)+

  geom_line(aes(x=Date, y=pr))

2. >   install.packages("gtidyverse")

> library('tidyverse')

3. > install.packages("ggpubr",

                 repos = c("https://cran.rediris.org/", "https://cloud.r-project.org/"),

                 dependencies = TRUE)

> library('ggpubr)

##############ploting with ggarrange

> ec1 <- ggplot2(dataframe1)+

  geom_line(aes(x=Date, y=at))+

  geom_col(aes(x=Date, y=pr), color = "red")

> ec2 <- ggplot2(dataframe2)+

  geom_line(aes(x=Date, y=at))+

  geom_col(aes(x=Date, y=pr), color = "red")


> ec3 <- ggplot2(dataframe3)+

  geom_line(aes(x=Date, y=at))+

  geom_col(aes(x=Date, y=pr), color = "red")


> ggpubr::ggarrange(ec1, ec2, ec3,  

          ncol = 1, nrow = 3)

#################Plotting with common axis with ggarrange

> library(grid)

> library(gridExtra)

> ec111 <-   ggpubr::ggarrange(ec1 + rremove("ylab") + rremove("xlab"), 

                             ec2 + rremove("ylab") + rremove("xlab"),

                             ec3 + rremove("ylab") + rremove("xlab"), 

                             labels = c("EC1",

                                        "EC2",

                                        "EC3"),

                             ncol = 1, 

                             nrow = 3,

                             hjust = -15,

                             common.legend = TRUE, 

                             legend = "bottom",

                             align = "hv", 

                             font.label = list(size = 10, color = "black", face = "bold", family = NULL, position = "top"))

then,

> annotate_figure(ec111, left = textGrob("Temperature (black) and Precipitation (red)", rot = 90, vjust = 1, gp = gpar(cex = 1.3)),

                bottom = textGrob("Date", gp = gpar(cex = 1.3)))


          


I made this with my data.


Thursday, 16 February 2023

Filter row wise data from data frame with dates in R

What Is the Best Way to Filter by Date in R?


Method 1: After Date Filter Rows


df %>% filter(date_column > '2022-01-01')


Method 2: Filter Rows Before Date


df %>% filter(date_column < '2022-01-01')


Method 3: Filter Rows Between Two Dates


df %>% filter(between(date_column, as.Date('2022-01-20'), as.Date('2022-02-20')))



Let’s create a data frame


df <- data.frame(day=seq(as.Date('2022-01-01'), by = 'week', length.out=10),

                 sales=c(40, 35, 39, 44, 48, 51, 23, 29, 60, 65))

Now we can view the data frame


df

          day sales

1  2022-01-01   240

2  2022-01-08   335

3  2022-01-15   359

4  2022-01-22   544

5  2022-01-29   548

6  2022-02-05   251

7  2022-02-12   223

8  2022-02-19   529

9  2022-02-26   660

10 2022-03-05   165


Example 1: Filter Rows After Date

To filter for rows in the data frame with a date after 1/25/2022, use the following code.


library(tidyverse)

filter for rows with dates after 1/25/2022


df %>% filter(day > '2022-01-25')

        day sales

1 2022-01-29   548

2 2022-02-05   251

3 2022-02-12   223

4 2022-02-19   529

5 2022-02-26   660

6 2022-03-05   165

Each row in the generated data frame has a date that is later than 1/25/2022.


Best Books on Data Science with Python – Data Science Tutorials


Example 2: Filter Rows Before Date

To filter for rows in the data frame with a date before 1/25/2022, we can use the following code.


library(dplyr)

Let’s filter for rows with dates before 1/25/2022


df %>% filter(day < '2022-01-25')

        day sales

1 2022-01-01   240

2 2022-01-08   335

3 2022-01-15   359

4 2022-01-22   544

Each entry in the generated data frame has a date that is prior to 1/25/2022.


Example 3: Filter Rows Between Two Dates

To filter for rows in the data frame with a date between 1/20/2022 and 2/20/2022, use the following code.


library(tidyverse)

filter for rows with dates between 1/20/2022 and 2/20/2022


Best Data Science YouTube Tutorials Free to Learn – Data Science Tutorials


df %>% filter(between(daty, as.Date('2022-01-20'), as.Date('2022-02-20')))

         day sales

1 2022-01-22   544

2 2022-01-29   548

3 2022-02-05   251

4 2022-02-12   223

5 2022-02-19   529

The dates in the rows of the generated data frame range from 1/20/2022 to 2/20/2022.


If none of the ways above work, you may need to use them as.Date() function to convert the dates you’re working with to a recognized date format.

How to install R library 'tidyverse' for updated R version 4.2.1 on latest Ubuntu 22.2

Dear all,

I was trying to install tidyverse in Rv4.2.1 in Ubuntu 22.2 ans whenever i as trying to install tidyverse I got an error "ERROR: configuration failed for package ‘curl’

* removing ‘/home/rajiv/R/x86_64-pc-linux-gnu-library/4.2/curl’

Warning in install.packages :

  installation of package ‘curl’ had non-zero exit status

* installing *source* package ‘xml2’ ...

** package ‘xml2’ successfully unpacked and MD5 sums checked

** using staged installation

Package libxml-2.0 was not found in the pkg-config search path.

Perhaps you should add the directory containing `libxml-2.0.pc'

to the PKG_CONFIG_PATH environment variable

No package 'libxml-2.0' found

Package libxml-2.0 was not found in the pkg-config search path.

Perhaps you should add the directory containing `libxml-2.0.pc'

to the PKG_CONFIG_PATH environment variable

No package 'libxml-2.0' found

Using PKG_CFLAGS=

Using PKG_LIBS=-lxml2

------------------------- ANTICONF ERROR ---------------------------

Configuration failed because libxml-2.0 was not found. Try installing:

 * deb: libxml2-dev (Debian, Ubuntu, etc)

 * rpm: libxml2-devel (Fedora, CentOS, RHEL)

 * csw: libxml2_dev (Solaris)

If libxml-2.0 is already installed, check that 'pkg-config' is in your

PATH and PKG_CONFIG_PATH contains a libxml-2.0.pc file. If pkg-config

is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:

R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'

--------------------------------------------------------------------

ERROR: configuration failed for package ‘xml2’

* removing ‘/home/rajiv/R/x86_64-pc-linux-gnu-library/4.2/xml2’

Warning in install.packages :

  installation of package ‘xml2’ had non-zero exit status

ERROR: dependency ‘curl’ is not available for package ‘httr’

* removing ‘/home/rajiv/R/x86_64-pc-linux-gnu-library/4.2/httr’

Warning in install.packages :

  installation of package ‘httr’ had non-zero exit status

ERROR: dependency ‘httr’ is not available for package ‘gargle’

* removing ‘/home/rajiv/R/x86_64-pc-linux-gnu-library/4.2/gargle’

Warning in install.packages :

  installation of package ‘gargle’ had non-zero exit status

ERROR: dependencies ‘httr’, ‘xml2’ are not available for package ‘rvest’

* removing ‘/home/rajiv/R/x86_64-pc-linux-gnu-library/4.2/rvest’

Warning in install.packages :

  installation of package ‘rvest’ had non-zero exit status

ERROR: dependencies ‘gargle’, ‘httr’ are not available for package ‘googledrive’

* removing ‘/home/rajiv/R/x86_64-pc-linux-gnu-library/4.2/googledrive’

Warning in install.packages :

  installation of package ‘googledrive’ had non-zero exit status

ERROR: dependencies ‘curl’, ‘gargle’, ‘googledrive’, ‘httr’ are not available for package ‘googlesheets4’

* removing ‘/home/rajiv/R/x86_64-pc-linux-gnu-library/4.2/googlesheets4’

Warning in install.packages :

  installation of package ‘googlesheets4’ had non-zero exit status

ERROR: dependencies ‘googledrive’, ‘googlesheets4’, ‘httr’, ‘rvest’, ‘xml2’ are not available for package ‘tidyverse’

* removing ‘/home/rajiv/R/x86_64-pc-linux-gnu-library/4.2/tidyverse’

Warning in install.packages :

  installation of package ‘tidyverse’ had non-zero exit status

"

#############################################################################

Well, to solve this i googled and got some way thereafter i tried myself. So solution is here:

1. > find("install.packages")

then i got 

[1] "package:utils"

2. > sessionInfo()

then i got 

R version 4.2.1 (2022-06-23)

Platform: x86_64-pc-linux-gnu (64-bit)

Running under: Ubuntu 22.10

Matrix products: default

BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.1

LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.1

locale:

 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=de_DE.UTF-8       

 [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=de_DE.UTF-8    LC_MESSAGES=en_US.UTF-8   

 [7] LC_PAPER=de_DE.UTF-8       LC_NAME=C                  LC_ADDRESS=C              

[10] LC_TELEPHONE=C             LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C       

attached base packages:

[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):

[1] compiler_4.2.1 tools_4.2.1   


Unfortunately, tidyverse is not installed.

Well, i again i googled.


3. > utils::install.packages("tidyverse")

I got this error

ERROR: configuration failed for package ‘curl’

* removing ‘/home/rajiv/R/x86_64-pc-linux-gnu-library/4.2/curl’

* installing *source* package ‘xml2’ ...

** package ‘xml2’ successfully unpacked and MD5 sums checked

** using staged installation

Package libxml-2.0 was not found in the pkg-config search path.

Perhaps you should add the directory containing `libxml-2.0.pc'

to the PKG_CONFIG_PATH environment variable

No package 'libxml-2.0' found

Package libxml-2.0 was not found in the pkg-config search path.

Perhaps you should add the directory containing `libxml-2.0.pc'

to the PKG_CONFIG_PATH environment variable

No package 'libxml-2.0' found

Using PKG_CFLAGS=

Using PKG_LIBS=-lxml2

------------------------- ANTICONF ERROR ---------------------------

Configuration failed because libxml-2.0 was not found. Try installing:

 * deb: libxml2-dev (Debian, Ubuntu, etc)

 * rpm: libxml2-devel (Fedora, CentOS, RHEL)

 * csw: libxml2_dev (Solaris)

If libxml-2.0 is already installed, check that 'pkg-config' is in your

PATH and PKG_CONFIG_PATH contains a libxml-2.0.pc file. If pkg-config

is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:

R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'

--------------------------------------------------------------------

ERROR: configuration failed for package ‘xml2’

* removing ‘/home/rajiv/R/x86_64-pc-linux-gnu-library/4.2/xml2’

ERROR: dependency ‘curl’ is not available for package ‘httr’

* removing ‘/home/rajiv/R/x86_64-pc-linux-gnu-library/4.2/httr’

ERROR: dependency ‘httr’ is not available for package ‘gargle’

* removing ‘/home/rajiv/R/x86_64-pc-linux-gnu-library/4.2/gargle’

ERROR: dependencies ‘httr’, ‘xml2’ are not available for package ‘rvest’

* removing ‘/home/rajiv/R/x86_64-pc-linux-gnu-library/4.2/rvest’

ERROR: dependencies ‘gargle’, ‘httr’ are not available for package ‘googledrive’

* removing ‘/home/rajiv/R/x86_64-pc-linux-gnu-library/4.2/googledrive’

ERROR: dependencies ‘curl’, ‘gargle’, ‘googledrive’, ‘httr’ are not available for package ‘googlesheets4’

* removing ‘/home/rajiv/R/x86_64-pc-linux-gnu-library/4.2/googlesheets4’

ERROR: dependencies ‘googledrive’, ‘googlesheets4’, ‘httr’, ‘rvest’, ‘xml2’ are not available for package ‘tidyverse’

* removing ‘/home/rajiv/R/x86_64-pc-linux-gnu-library/4.2/tidyverse’


The downloaded source packages are in

‘/tmp/RtmpAPocAk/downloaded_packages’

Warning messages:

1: In utils::install.packages("tidyverse") :

  installation of package ‘curl’ had non-zero exit status

2: In utils::install.packages("tidyverse") :

  installation of package ‘xml2’ had non-zero exit status

3: In utils::install.packages("tidyverse") :

  installation of package ‘httr’ had non-zero exit status

4: In utils::install.packages("tidyverse") :

  installation of package ‘gargle’ had non-zero exit status

5: In utils::install.packages("tidyverse") :

  installation of package ‘rvest’ had non-zero exit status

6: In utils::install.packages("tidyverse") :

  installation of package ‘googledrive’ had non-zero exit status

7: In utils::install.packages("tidyverse") :

  installation of package ‘googlesheets4’ had non-zero exit status

8: In utils::install.packages("tidyverse") :

  installation of package ‘tidyverse’ had non-zero exit status

...................................................................................................................................

4. So, i tried to install Rculr througn Rconsol

> install.packages("RCurl")

and geot this error 

ERROR: dependency ‘curl’ is not available for package ‘httr’

* removing ‘/home/rajiv/R/x86_64-pc-linux-gnu-library/4.2/httr’

ERROR: dependency ‘httr’ is not available for package ‘gargle’

* removing ‘/home/rajiv/R/x86_64-pc-linux-gnu-library/4.2/gargle’

ERROR: dependency ‘httr’ is not available for package ‘rvest’

* removing ‘/home/rajiv/R/x86_64-pc-linux-gnu-library/4.2/rvest’

ERROR: dependencies ‘gargle’, ‘httr’ are not available for package ‘googledrive’

* removing ‘/home/rajiv/R/x86_64-pc-linux-gnu-library/4.2/googledrive’

ERROR: dependencies ‘curl’, ‘gargle’, ‘googledrive’, ‘httr’ are not available for package ‘googlesheets4’

* removing ‘/home/rajiv/R/x86_64-pc-linux-gnu-library/4.2/googlesheets4’

ERROR: dependencies ‘googledrive’, ‘googlesheets4’, ‘httr’, ‘rvest’ are not available for package ‘tidyverse’

* removing ‘/home/rajiv/R/x86_64-pc-linux-gnu-library/4.2/tidyverse’

5. Then i  tried with linux terminal 

6. sudo apt-get update

7. sudo apt-get install libxml2-dev

8. sudo apt-get install r-cran-xml

9. sudo apt-get install libcurl4-openssl-dev r-base

10. R -q -e "install.packages(c('curl'))"

11. sudo apt install r-cran-curl

12. Then again I tried 

> utils::install.packages("tidyverse")

and got no error 

13. then 

> library('tidyverse')

and showed 

── Attaching packages ───────────────────────────────────────────────── tidyverse 1.3.2 ──

✔ ggplot2 3.4.1     ✔ purrr   1.0.1

✔ tibble  3.1.8     ✔ dplyr   1.1.0

✔ tidyr   1.3.0     ✔ stringr 1.5.0

✔ readr   2.1.4     ✔ forcats 1.0.0

── Conflicts ──────────────────────────────────────────────────── tidyverse_conflicts() ──

✖ dplyr::filter() masks stats::filter()

✖ dplyr::lag()    masks stats::lag()


Hurreyyyyyyyy!!!!!!!!!!!!


So the problem was not with R updated version. Indeed, it was problem with Ubuntu new version 22.2. Hence, you need to install Rcurl through terminal first then try by R consol.

Ubuntu does not allow any progrram directly. It reueirs lib files which were missing in updated version. So, first update ubuntu through terminal and then install lib files.


Refernces:

https://stackoverflow.com/questions/52082543/curl-package-not-available-for-several-r-packages

https://stackoverflow.com/questions/7765429/unable-to-install-r-package-in-ubuntu-11-04

https://stackoverflow.com/questions/20671814/non-zero-exit-status-r-3-0-1-xml-and-rcurl

https://stackoverflow.com/questions/72578482/issues-installing-tidyverse-package






The Rise of Generative AI: Transforming Creativity and Innovation

  Introduction Generative AI, a subset of artificial intelligence, has revolutionized the way we approach creativity and problem-solving. By...