如果我们有一个包含数字元素的列表,并且想要检查元素是否大于某个值,则as.numeric可以使用函数。函数的输出将为0/1格式,其中0表示FALSE,1表示TRUE。例如,如果我们有一个名为LIST的列表,则可以按as.numeric(LIST> 2)来检查LIST中的元素是否大于2。
List1<−list(1,2,2,2,4,1,2,5,5,2,3,5,2,2,2,2,5,5,8,9,6,5,5) List1输出结果
[[1]] [1] 1 [[2]] [1] 2 [[3]] [1] 2 [[4]] [1] 2 [[5]] [1] 4 [[6]] [1] 1 [[7]] [1] 2 [[8]] [1] 5 [[9]] [1] 5 [[10]] [1] 2 [[11]] [1] 3 [[12]] [1] 5 [[13]] [1] 2 [[14]] [1] 2 [[15]] [1] 2 [[16]] [1] 2 [[17]] [1] 5 [[18]] [1] 5 [[19]] [1] 8 [[20]] [1] 9 [[21]] [1] 6 [[22]] [1] 5 [[23]] [1] 5
检查List1中的元素是否大于5-
as.numeric(List1>5)输出结果
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0
List2<−list(4,7,5,4,1,2,5,2,3,6,5,7,7,1,1,2,5,5,9,2) List2
[[1]] [1] 4 [[2]] [1] 7 [[3]] [1] 5 [[4]] [1] 4 [[5]] [1] 1 [[6]] [1] 2 [[7]] [1] 5 [[8]] [1] 2 [[9]] [1] 3 [[10]] [1] 6 [[11]] [1] 5 [[12]] [1] 7 [[13]] [1] 7 [[14]] [1] 1 [[15]] [1] 1 [[16]] [1] 2 [[17]] [1] 5 [[18]] [1] 5 [[19]] [1] 9 [[20]] [1] 2
检查List2中的元素是否大于2-
as.numeric(List2>2)输出结果
[1] 1 1 1 1 0 0 1 0 1 1 1 1 1 0 0 0 1 1 1 0