R语言 创建、追加和修改列表

R语言 创建、追加和修改列表

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

创建一个列表

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

语法:

list(value1,value2,………….,valuen)

其中值是列表的输入。

例子:

# 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语言中创建、追加和修改列表

将数据追加到列表中

向列表追加数据是指在一个已经存在的列表的最后加入一个值。我们将通过使用append()函数来追加两个列表

语法

append(list1,list2)

例子

# 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语言中创建、追加和修改列表

我们还可以使用$操作符将一个数据框架中的单列追加到另一个数据框架中。

语法

append(dataframe1columnname,dataframe2columnname)

程序

# 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(student1student1_marks,student2student2_marks))

输出

[1]  78  90 100 100  78  90 100 100

修改列表 中的元素

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

索引操作 符。

[[]]

语法:

list_name[[n]]="new_element_name

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

输出

在R语言中创建、追加和修改列表

修改也包括删除。我们可以通过给列表索引分配NULL操作符来删除列表中的特定元素。

语法:

list_name[[index_number]]=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)

输出

在R语言中创建、追加和修改列表

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程