List and Factor

an anonymous user · January 23, 2021 · R
u<-c(0,4,1,1,2)
f<-factor(u,levels=0:3)
f
levels(f)<-c("none","more","medium","large")
f
as.numeric(f)
listt<-list("red","green",c(2,5,3),67+7i,654L,TRUE)
listt
# Create a list containing a vector, a matrix and a list.

list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2),
list("green",12.3))

# Give names to the elements in the list.

names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list")

# Show the list.
print(list_data)

# Add element at the end of the list.

list_data[4] <- "New element"

print(list_data[4])

# Remove the last element.

list_data[4] <- NULL

# Print the 4th Element.

print(list_data[4])

# Update the 3rd Element.

list_data[3] <- c("updated element") 
print(list_data[3])
# Create two lists.

list1 <- list(1,2,3)

list2 <- list("Sun","Mon","Tue")

# Merge the two lists.

merged.list <- c(list1,list2)

# Print the merged list.

print(merged.list)
# Create lists.

list1 <- list(1:5)

print(list1)

list2 <-list(10:14)

print(list2)

# Convert the lists to vectors.

v1 <- unlist(list1)

v2 <- unlist(list2)

print(v1)

print(v2)

# Now add the vectors .

result<-v1+v2 
print(result)

Comments

Please sign up or log in to contribute to the discussion.