0% found this document useful (0 votes)
3 views1 page

R Programming Basics and Data Handling

The document provides a comprehensive list of R programming tasks along with corresponding R code and brief explanations for each task. It covers a wide range of topics including data manipulation, statistical analysis, and data visualization. Each entry includes a question, the R code to solve it, and a rubric for correctness.

Uploaded by

121424413014
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

R Programming Basics and Data Handling

The document provides a comprehensive list of R programming tasks along with corresponding R code and brief explanations for each task. It covers a wide range of topics including data manipulation, statistical analysis, and data visualization. Each entry includes a question, the R code to solve it, and a rubric for correctness.

Uploaded by

121424413014
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

S.

No Question R Code Brief Explanation/Rubric

1 Print "Hello, R!" to the console. print("Hello, R!") Correct use ofprint().

2 Assign 10 to a variable named x. x <- 10 Assigns value using<-syntax.

3 Create a numeric vector v with values 5, 10, 15. v <- c(5,10,15) Usesc()function.

4 Find the mean of v. mean(v) Uses correct function.

5 Import "[Link]" into data frame df. df <- [Link]("[Link]") Correct import.

6 Display the first 6 rows of df. head(df) Useshead().

7 Check for missing values in df. sum([Link](df)) [Link]()check.

8 Filter df for sales > 1000. subset(df, sales > 1000) Correctsubset()usage.

9 Add a new column 'profit' = 'revenue' - 'cost'. df$profit <- df$revenue - df$cost Column arithmetic in data frame.

10 Create a barplot of 'sales' column. barplot(df$sales) Simple data visualization.

11 Write function to add two numbers. add <- function(a, b) { a + b } Basic function definition.

12 Use for loop to print numbers 1 to 5. for(i in 1:5) print(i) Loop structure.

13 Find max value in a vector. max(v) Correct use ofmax().

14 Sort a vector in descending order. sort(v, decreasing=TRUE) Sorting logic.

15 Read Excel file "[Link]". library(readxl); df <- read_excel("[Link]") External package use.

16 Count records where gender == "Male". sum(df$gender=="Male") Logical condition.

17 Calculate average score for 'score' column. mean(df$score, [Link]=TRUE) Handling missing values.

18 Plot sales trend by month line graph. plot(df$month, df$sales, type="l") Time series plotting.

19 Create a categorical variable 'grade' from 'score'. df$grade <- ifelse(df$score>80,"A","B") Conditional categorization.

20 Summarize all columns of df. summary(df) Full summary stats.

21 Merge df1 and df2 by 'id'. merged <- merge(df1, df2, by="id") Data frame merging.

22 Remove duplicates in df. df <- unique(df) Handling duplicates.

23 Save df as "[Link]". [Link](df, "[Link]") Exporting data.

24 Draw histogram for 'age'. hist(df$age) Visual exploration.

25 Subset environment data where PM2.5 > 100. subset(df, PM2.5 > 100) Environmental filter.

26 Print data types of all columns. sapply(df, class) Data structure check.

27 Create matrix 3x3 with 1:9. matrix(1:9, nrow=3) Matrix creation.

28 Bind two vectors as columns. cbind(a, b) Data frame creation.

29 Extract 'name' column values with age > 40. df$name[df$age>40] Logical indexing.

30 Replace NA in 'income' with mean. df$income[[Link](df$income)] <- mean(df$income, [Link]=TRUE) Imputation.

31 Generate 10 random normal values. rnorm(10) Simulation in R.

32 Calculate correlation between sales and profit. cor(df$sales, df$profit) Statistical function.

33 Write function to calculate factorial. fact <- function(n) if(n<=1) 1 else n*fact(n-1) Recursion.

34 Rename 'old' column to 'new'. names(df)[names(df)=="old"] <- "new" Data renaming.

35 List all column names in df. colnames(df) Data frame structure.

36 Find number of rows and columns. dim(df) Data dimensions.

37 Filter health data for BMI > 25. subset(df, BMI > 25) Health filter.

38 Apply function over columns to get means. apply(df, 2, mean, [Link]=TRUE) Data aggregation.

39 Remove 'id' column from df. df$id <- NULL Column deletion.

40 Load ggplot2 and make scatter plot sales vs profit. library(ggplot2); ggplot(df, aes(sales, profit)) + geom_point() Package-based plotting.

41 Set working directory to "/data". setwd("/data") File management.

42 Create data frame from vectors a, b. [Link](a, b) Data frame basics.

43 Create list with name, age, city. lst <- list(name="A", age=33, city="Delhi") List structure.

44 Save list object to RData file. save(lst, file="[Link]") Data persistence.

45 Read RData file. load("[Link]") Load data.

46 Convert character to factor. [Link](df$character_col) Factor conversion.

47 Get distinct values of 'city'. unique(df$city) Value extraction.

48 Replace all "NA" strings in df with NA. df[df=="NA"] <- NA Data cleaning.

49 Check if a column contains only digits. all(grepl("^[0-9]+$", df$col)) Regular expression test.

50 Create a dataframe with random business data. [Link](Customer=sample(LETTERS,10), Sales=runif(10,100,1000)) Simulated example.

You might also like