📜  在 R 中创建、附加和修改列表

📅  最后修改于: 2022-05-13 01:55:42.054000             🧑  作者: Mango

在 R 中创建、附加和修改列表

在 R 编程语言中,列表是一种一维数据结构,可以容纳多种数据类型元素。在本文中,我们将创建一个列表并将数据附加到列表中并修改列表元素。

创建列表

可以使用 list()函数创建列表。

语法

其中值是列表的输入。



例子 :

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)


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))


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))


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]))


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]]))


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)


输出:

数据附加到列表

附加到列表意味着将值添加到现有列表的最后一个。我们将使用 append()函数附加两个列表

句法:

例子:



电阻

# 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))

输出:

我们还可以使用 $ 运算符将一个数据框中的单列附加到另一个数据框中。

句法:

程序:

电阻

# 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))

输出:

[1]  78  90 100 100  78  90 100 100

修改列表元素

修改列表意味着更改列表的初始表示,包括更改或更新值和删除等操作。我们可以使用索引运算符修改列表元素。



索引运算符:

语法

其中 n 是索引值

例子:

电阻

# 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]))

输出:

[1] "1 st one - 2 nd element is :  pinkey"
[1] "Modified 1 st one - 2 nd element is :  gajji"

我们也可以一次修改整个索引,

例子:



电阻

# 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]]))

输出:

修改也包括删除。我们可以通过使用 NULL运算符分配列表索引来从列表中删除特定元素。

语法

例子:

电阻

# 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)

输出: