En el lenguaje de programación R, la lista es una estructura de datos unidimensional que puede contener múltiples elementos de tipo de datos. En este artículo, vamos a crear una lista y agregar datos a la lista y modificar los elementos de la lista.
Creando una lista
La lista se puede crear usando la función list().
Sintaxis :
lista(valor1,valor2,………….,valorn)
donde los valores son las entradas a la lista.
Ejemplo :
R
# vector with names names=c("bobby","pinkey","rohith","gnanu") # vector with marks marks=c(78,90,100,100) # address vector address=c("kakumanu","hyd","hyd","hyd") # pass these vectors as inputs to the list # address vector student=list(names,marks,address) print(student)
Producción:
Agregar datos a la lista
Agregar a una lista significa agregar un valor al último de una lista ya existente. Vamos a agregar dos listas usando la función append()
Sintaxis:
agregar (lista1, lista2)
Ejemplo:
R
# vector with names names=c("bobby","pinkey","rohith","gnanu") # vector with marks marks=c(78,90,100,100) # address vector address=c("kakumanu","hyd","hyd","hyd") # college values college=c("vignan","vignan","vignan","vignan") # pass these vectors as inputs to the list student1=list(student1_names=names,student1_marks=marks, student1_address=address,student1_college=college) # vector with names names=c("ravi","devi","siree","priyank") # vector with marks marks=c(78,90,100,100) # address vector address=c("hyd","hyd","hyd","hyd") # college values college=c("vvit","vvit","vvit","vvit") # pass these vectors as inputs to the list # address vector student2=list(student2_names=names,student2_marks=marks, student2_address=address,student2_college=college) # append list 1 and list 2 print(append(student1,student2))
Producción:
También podemos agregar una sola columna en un marco de datos a otro usando el operador $.
Sintaxis:
agregar (marco de datos 1 $nombre de columna, marco de datos 2 $nombre de columna)
Programa:
R
# vector with names names=c("bobby","pinkey","rohith","gnanu") # vector with marks marks=c(78,90,100,100) # address vector address=c("kakumanu","hyd","hyd","hyd") # college values college=c("vignan","vignan","vignan","vignan") # pass these vectors as inputs to the list student1=list(student1_names=names,student1_marks=marks, student1_address=address,student1_college=college) # vector with names names=c("ravi","devi","siree","priyank") # vector with marks marks=c(78,90,100,100) # address vector address=c("hyd","hyd","hyd","hyd") # college values college=c("vvit","vvit","vvit","vvit") # pass these vectors as inputs to the list # address vector student2=list(student2_names=names,student2_marks=marks, student2_address=address,student2_college=college) # append list 1 - student marks and list 2 - student marks print(append(student1$student1_marks,student2$student2_marks))
Producción:
[1] 78 90 100 100 78 90 100 100
Modificar los elementos de la lista
Modificar una lista significa cambiar la representación inicial de la lista que puede incluir operaciones como cambiar o actualizar valores y eliminar. Podemos modificar los elementos de la lista usando el operador de índice.
operador de índice:
[[]]
Sintaxis :
list_name[[n]]=”nuevo_elemento_nombre
donde n es el valor del índice
Ejemplo:
R
# vector with names names=c("bobby","pinkey","rohith","gnanu") # vector with marks marks=c(78,90,100,100) # address vector address=c("kakumanu","hyd","hyd","hyd") # college values college=c("vignan","vignan","vignan","vignan") # pass these vectors as inputs to the list student1=list(names,marks,address,college) # display 1 st row 2 nd element print(paste("1 st one - 2 nd element is : ",student1[[1]][2])) # modify pinky to gajji student1[[1]][2]="gajji" print(paste("Modified 1 st one - 2 nd element is : ",student1[[1]][2]))
Producción:
[1] "1 st one - 2 nd element is : pinkey" [1] "Modified 1 st one - 2 nd element is : gajji"
También podemos modificar el índice completo a la vez,
Ejemplo:
R
# vector with names names=c("bobby","pinkey","rohith","gnanu") # vector with marks marks=c(78,90,100,100) # address vector address=c("kakumanu","hyd","hyd","hyd") # college values college=c("vignan","vignan","vignan","vignan") # pass these vectors as inputs to the list student1=list(names,marks,address,college) # display 2 nd index print(paste("2 nd index elements are : ",student1[[2]])) # modify the whole content of 2 nd index student1[[2]]=c(45,56,54,45) print(paste("Modified 2 nd index elements are : ",student1[[2]]))
Producción:
La modificación incluye también la eliminación. Podemos eliminar un elemento particular de la lista asignando un índice de lista con el operador NULL.
Sintaxis :
nombre_lista[[número_índice]]=NULO
Ejemplo:
R
# vector with names names=c("bobby","pinkey","rohith","gnanu") # vector with marks marks=c(78,90,100,100) # address vector address=c("kakumanu","hyd","hyd","hyd") # college values college=c("vignan","vignan","vignan","vignan") # pass these vectors as inputs to the list student1=list(names,marks,address,college) # deleting first item IE names vector student1[[1]]=NULL # display list print(student1)
Producción:
Publicación traducida automáticamente
Artículo escrito por sravankumar8128 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA