Tuesday, August 6, 2024

List in R language

               List  in R


  1. Creating a list         we use list function to create list 

  2. Assign name  to the list elements with the help of names() function.

  3. Print the list data


How to acces R list elements ? .

  1. Indexing method            ex print(list_data[1])  

  2. Usings names                ex . print(list_data(student))



# creating a list containing  strings , number , vector . and a logical values .


 List_data <- list(“Ram”, “Sham” , c(1,2,3), TRUE , 1.03 )

 

 print(List_data)

 

Output:

[[1]]

[1] "Ram"


[[2]]

[1] "Sham"


[[3]]

[1] 1 2 3


[[4]]

[1] TRUE


[[5]]

[1] 1.03




# creating list containing a vector , a matrix and a list .

  

   List_data1 <- list(c(“ram”,”sham”,”raj” ) , matrix(c(40,60,70,80,30,20), nrow=2), 

                                list(“BCA”,”MCA”,”BSc”))

# Giving names to the elements in the list 

 names(List_data1) <- c(“Students”, “Marks” , “Course”)


#show list

print(List_data1)

#adding element at the end of the list (use append function )


List_data1 <- append(List_data1, "mumbai")  # Appends "mumbai" to the end of the list


#adding data at specific position 


List_data1[4] <- “pune”         


# removing the last element 


  List_data1[5]<- NULL 


# Updating the 3rd Element 

  

  List_data1[3] <- “BCA science”

 print(List_data1[3]


  • create a list of character strings and print it:



# Create a list of character strings

list_pdata <- list("Ram Sharma", "Varma", "Raj Jadhav", "Pratik ")


# Print the list

print(list_pdata)


[[1]]

[1] "Ram Sharma"


[[2]]

[1] "Varma"


[[3]]

[1] "Raj Jadhav"


[[4]]

[1] "Pratik SSFGSSFGDJN"





# Add a new element by assigning it to a new index


 list_pdata[[5]] <- "Another Person"


# Remove the second element list_pdata[[2]] <- NULL


list_pdata <- list_pdata[-2]

# print ing the 3rd element 


print(list_pdata[3]) 



#creatinjg lists 

list1<- list(1:5)

print(list1)

output:

[[1]]

[1] 1 2 3 4 5


 list2 <- list(11:15)

 print(list2)


#convert list to vector 

V1 <- unlist(list1)

V2 <- unlist(list2)

print(v1)

[1] 1 2 3 4 5


 print(V2)

[1] 11 12 13 14 15


#ADDING a vector 


Result <- v1+v2 

Print (result)

[1] 12 14 16 18 20


# merge list 


> MergedList1 <- c(list1, list2)

> print(MergedList1)

[[1]]

[1] 1 2 3 4 5


[[2]]

[1] 11 12 13 14 15


# Define two lists

List1 <- list("Ram", "Sham", c(1, 2, 3))

List2 <- list(TRUE, 1.03, "pune")


#merge two list 

# Method 1: Using c()

MergedList1 <- c(List1, List2)

print(MergedList1)

Output:

[[1]]

[1] "Ram"


[[2]]

[1] "Sham"


[[3]]

[1] 1 2 3


[[4]]

[1] TRUE


[[5]]

[1] 1.03


[[6]]

[1] "pune"





# Method 2: Using append()

MergedList2 <- append(List1, List2)

print(MergedList2)


# Method 3: Using unlist() and list()

MergedList3 <- list(unlist(c(List1, List2)))

print(MergedList3)

Output:

[[1]]

[1] "Ram"  "Sham" "1"    "2"    "3"    "TRUE" "1.03" "pune"


# Method 4: Using Map()

MergedList4 <- Map(c, List1, List2)

print(MergedList4)

Output:

[[1]]

[1] "Ram"  "TRUE"


[[2]]

[1] "Sham" "1.03"


[[3]]

[1] "1"    "2"    "3"    "pune"






2 comments:

  1. Impressive how you've combined SEO-friendly structure with eye-catching Website Designing .

    ReplyDelete
  2. Website Designing that prioritizes loading speed and visual appeal? That's next-level.

    ReplyDelete