R Programming Articles

Page 29 of 174

How to check if a vector exists in a list in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 5K+ Views

To check if a vector exists in a list, we can use %in%, and read the vector as list using list function. For example, if we have a list called LIST and a vector called V then we can check whether V exists in LIST using the command LIST %in% list(V).ExampleConsider the below list −List

Read More

How to create a blank column with randomization in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 256 Views

To create a blank column with randomization in an R data frame, we can use sample function and pass the blanks with single space. For example, if we want to create a vector say x that will be added in the data frame can be created by using the command −x

Read More

How to split a data frame using row number in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 2K+ Views

To split a data frame using row number, we can use split function and cumsum function. The split function will split the rows and cumsum function will select the rows. For example, if we have a data frame called df that contains twenty rows then we can split into two data frames at row 11 by using the below command −split(df,cumsum(1:nrow(df)%in%11)).ExampleConsider the below data frame −x1

Read More

How to generate random numbers with sequence and store in a data frame column in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 923 Views

To generate random numbers with sequence and storing them in a data frame column, we can use sample function with seq. For example, if we want to create a data frame column having random sequence of values of size 50 between 1 to 100 by considering every tenth value then we can use the commanddf

Read More

How to remove starting and ending zeros in an R vector?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 720 Views

To remove starting and ending zeros in an R vector, we can use min and max function to access values except 0 and then subsetting with single square brackets. For example, if we have a vector called x then we can remove starting and ending zeros by using the command −x[min(which(x!=0)):max(which(x!=0))]Examplex1

Read More

How to extract data frame columns stored in a list in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 3K+ Views

Suppose we have two frames each having 5 columns that are stored in a list in R and the data that belongs to same columns has some kind of inherent relationship or we want to check whether there exists a relationship between them then we might want to extract those columns. Therefore, we can use lapply function for this extraction. For example, if we have a list called LIST that store two data frames then column 3 of each data frame can be extracted by using the command lapply(LIST, "[", 3).ExampleConsider the below data frames and list of these data ...

Read More

How to set the range for boxplot in base R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 2K+ Views

By default, R considers the vector or the data frame column values. But if we want to set the range for boxplot in base R, we can use ylim argument within the boxplot function. For example, if we have a vector called x that contains values starting from 21 to 50 and we want to have the range in the boxplot starting from 1 to 100 then we can use the commandboxplot(x,ylim=c(1,100))Examplex

Read More

How to extract the last element in an R matrix?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 844 Views

To extract the last element in an R matrix, we can use the length function along with single square brackets that are used for subsetting. For example, if we have a matrix called M as shown below −M1 2 3 4 5 6 7 8 9then we can extract last value of M by using the command M[length(M)].ExampleConsider the below matrix −M1

Read More

How to filter rows by excluding a particular value in columns of the R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 4K+ Views

To filter rows by excluding a particular value in columns of the data frame, we can use filter_all function of dplyr package along with all_vars argument that will select all the rows except the one that includes the passed value with negation. For example, if we have a data frame called df and we want to filter rows by excluding value 2 then we can use the commanddf%>%filter_all(all_vars(.!=2))ExampleConsider the below data frame −x1

Read More

How to find the sum of every n values in R data frame columns?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 3K+ Views

To find the sum of every n values in R data frame columns, we can use rowsum function along with rep function that will repeat the sum for rows. For example, if we have a data frame called df that contains 4 columns each containing twenty values then we can find the column sums for every 5 rows by using the command rowsum(df,rep(1:5,each=4)).ExampleConsider the below data frame −x1

Read More
Showing 281–290 of 1,740 articles
« Prev 1 27 28 29 30 31 174 Next »
Advertisements