R 矢量化函数不';不能产生预期的结果

R 矢量化函数不';不能产生预期的结果,r,sapply,R,Sapply,…以下是我在数据方面的问题: rm(list=ls()) clc06_1 <- "111 - Continuous urban fabric" clc06_2 <- "112 - Discontinuous urban fabric" clc06_3 <- "121 - Industrial or commercial units" clc06_4 <- "122 - Road and rail networks and associated land" clc06_5

…以下是我在数据方面的问题:

rm(list=ls())

clc06_1 <- "111 - Continuous urban fabric"
clc06_2 <- "112 - Discontinuous urban fabric"
clc06_3 <- "121 - Industrial or commercial units"
clc06_4 <- "122 - Road and rail networks and associated land"
clc06_5 <- "123 - Port areas"
clc06_6 <- "124 - Airports"
clc06_7 <- "131 - Mineral extraction sites"
clc06_8 <- "132 - Dump sites"
clc06_9 <- "133 - Construction sites"
clc06_10 <- "141 - Green urban areas"
clc06_11 <- "142 - Sport and leisure facilities"
clc06_12 <- "211 - Non-irrigated arable land"
clc06_13 <- "212 - Permanently irrigated land"
clc06_14 <- "213 - Rice fields"
clc06_15 <- "221 - Vineyards"
clc06_16 <- "222 - Fruit trees and berry plantations"
clc06_17 <- "223 - Olive groves"
clc06_18 <- "231 - Pastures"
clc06_19 <- "241 - Annual crops associated with permanent crops"
clc06_20 <- "242 - Complex cultivation patterns"
clc06_21 <- "243 - Land principally occupied by agriculture, with significant areas of natural vegetation"
clc06_22 <- "244 - Agro-forestry areas"
clc06_23 <- "311 - Broad-leaved forest"
clc06_24 <- "312 - Coniferous forest"
clc06_25 <- "313 - Mixed forest"
clc06_26 <- "321 - Natural grasslands"
clc06_27 <- "322 - Moors and heathland"
clc06_28 <- "323 - Sclerophyllous vegetation"
clc06_29 <- "324 - Transitional woodland-shrub"
clc06_30 <- "331 - Beaches, dunes, sands"
clc06_31 <- "332 - Bare rocks"
clc06_32 <- "333 - Sparsely vegetated areas"
clc06_33 <- "334 - Burnt areas"
clc06_34 <- "335 - Glaciers and perpetual snow"
clc06_35 <- "411 - Inland marshes"
clc06_36 <- "412 - Peat bogs"
clc06_37 <- "421 - Salt marshes"
clc06_38 <- "422 - Salines"
clc06_39 <- "423 - Intertidal flats"
clc06_40 <- "511 - Water courses"
clc06_41 <- "512 - Water bodies"
clc06_42 <- "521 - Coastal lagoons"
clc06_43 <- "522 - Estuaries"
clc06_44 <- "523 - Sea and ocean"

# I want to extract the CLC Code nrs which are the 3-digit number in the string:
foo <- function(x) unlist(strsplit(x, " - "))[[1]]

# and vectorize foo. But why is.. 
sapply(ls()[1:44], foo, simplify = T)

# not behaving like..
foo(clc06_9)
rm(list=ls())

clc06_1以下各项应适用于您:

sapply(ls()[1:44], function(x) foo(get(x)), simplify = TRUE)
您可以将该行为与以下内容进行比较:

> ls()[1]
[1] "clc06_1"
> foo(ls()[1])
[1] "clc06_1"
> foo(get(ls()[1]))
[1] "111"
朱巴的回答已经解释了这一点


已经说明了为什么不使用
列表

比较:

mylist <- list(clc06_1 = "111 - Continuous urban fabric",
               clc06_2 = "112 - Discontinuous urban fabric",
               clc06_3 = "121 - Industrial or commercial units",
               clc06_4 = "122 - Road and rail networks and associated land",
               clc06_5 = "123 - Port areas",
               clc06_6 = "124 - Airports")
sapply(mylist, foo)
# clc06_1 clc06_2 clc06_3 clc06_4 clc06_5 clc06_6 
#   "111"   "112"   "121"   "122"   "123"   "124" 

mylist以下内容应该适合您:

sapply(ls()[1:44], function(x) foo(get(x)), simplify = TRUE)
您可以将该行为与以下内容进行比较:

> ls()[1]
[1] "clc06_1"
> foo(ls()[1])
[1] "clc06_1"
> foo(get(ls()[1]))
[1] "111"
朱巴的回答已经解释了这一点


已经说明了为什么不使用
列表

比较:

mylist <- list(clc06_1 = "111 - Continuous urban fabric",
               clc06_2 = "112 - Discontinuous urban fabric",
               clc06_3 = "121 - Industrial or commercial units",
               clc06_4 = "122 - Road and rail networks and associated land",
               clc06_5 = "123 - Port areas",
               clc06_6 = "124 - Airports")
sapply(mylist, foo)
# clc06_1 clc06_2 clc06_3 clc06_4 clc06_5 clc06_6 
#   "111"   "112"   "121"   "122"   "123"   "124" 
mylist
ls()
返回对象名,而不是对象本身。所以你必须做一些类似的事情:

R> vars <- grep("^clc06_", ls(), value=TRUE)
R> sapply(vars, function(v) { foo(get(v)) }, simplify = T)
 clc06_1 clc06_10 clc06_11 clc06_12 clc06_13 clc06_14 clc06_15 clc06_16 
   "111"    "141"    "142"    "211"    "212"    "213"    "221"    "222" 
clc06_17 clc06_18 clc06_19  clc06_2 clc06_20 clc06_21 clc06_22 clc06_23 
   "223"    "231"    "241"    "112"    "242"    "243"    "244"    "311" 
clc06_24 clc06_25 clc06_26 clc06_27 clc06_28 clc06_29  clc06_3 clc06_30 
   "312"    "313"    "321"    "322"    "323"    "324"    "121"    "331" 
clc06_31 clc06_32 clc06_33 clc06_34 clc06_35 clc06_36 clc06_37 clc06_38 
   "332"    "333"    "334"    "335"    "411"    "412"    "421"    "422" 
clc06_39  clc06_4 clc06_40 clc06_41 clc06_42 clc06_43 clc06_44  clc06_5 
   "423"    "122"    "511"    "512"    "521"    "522"    "523"    "123" 
 clc06_6  clc06_7  clc06_8  clc06_9 
   "124"    "131"    "132"    "133" 
R>vars sapply(vars,函数(v){foo(get(v))},simplify=T)
clc06_1 clc06_10 clc06_11 clc06_12 clc06_13 clc06_14 clc06_15 clc06_16
"111"    "141"    "142"    "211"    "212"    "213"    "221"    "222" 
clc06_17 clc06_18 clc06_19 clc06_2 clc06_20 clc06_21 clc06_22 clc06_23
"223"    "231"    "241"    "112"    "242"    "243"    "244"    "311" 
clc06\u 24 clc06\u 25 clc06\u 26 clc06\u 27 clc06\u 28 clc06\u 29 clc06\u 3 clc06\u 30
"312"    "313"    "321"    "322"    "323"    "324"    "121"    "331" 
clc06_31 clc06_32 clc06_33 clc06_34 clc06_35 clc06_36 clc06_37 clc06_38
"332"    "333"    "334"    "335"    "411"    "412"    "421"    "422" 
clc06_39 clc06_4 clc06_40 clc06_41 clc06_42 clc06_43 clc06_44 clc06_5
"423"    "122"    "511"    "512"    "521"    "522"    "523"    "123" 
clc06_6 clc06_7 clc06_8 clc06_9
"124"    "131"    "132"    "133" 
但是使用
ls()
选择变量不是一个好主意。您应该将所有的
clc06\uuu
值存储在一个向量中。

ls()
返回对象名称,而不是对象本身。所以你必须做一些类似的事情:

R> vars <- grep("^clc06_", ls(), value=TRUE)
R> sapply(vars, function(v) { foo(get(v)) }, simplify = T)
 clc06_1 clc06_10 clc06_11 clc06_12 clc06_13 clc06_14 clc06_15 clc06_16 
   "111"    "141"    "142"    "211"    "212"    "213"    "221"    "222" 
clc06_17 clc06_18 clc06_19  clc06_2 clc06_20 clc06_21 clc06_22 clc06_23 
   "223"    "231"    "241"    "112"    "242"    "243"    "244"    "311" 
clc06_24 clc06_25 clc06_26 clc06_27 clc06_28 clc06_29  clc06_3 clc06_30 
   "312"    "313"    "321"    "322"    "323"    "324"    "121"    "331" 
clc06_31 clc06_32 clc06_33 clc06_34 clc06_35 clc06_36 clc06_37 clc06_38 
   "332"    "333"    "334"    "335"    "411"    "412"    "421"    "422" 
clc06_39  clc06_4 clc06_40 clc06_41 clc06_42 clc06_43 clc06_44  clc06_5 
   "423"    "122"    "511"    "512"    "521"    "522"    "523"    "123" 
 clc06_6  clc06_7  clc06_8  clc06_9 
   "124"    "131"    "132"    "133" 
R>vars sapply(vars,函数(v){foo(get(v))},simplify=T)
clc06_1 clc06_10 clc06_11 clc06_12 clc06_13 clc06_14 clc06_15 clc06_16
"111"    "141"    "142"    "211"    "212"    "213"    "221"    "222" 
clc06_17 clc06_18 clc06_19 clc06_2 clc06_20 clc06_21 clc06_22 clc06_23
"223"    "231"    "241"    "112"    "242"    "243"    "244"    "311" 
clc06\u 24 clc06\u 25 clc06\u 26 clc06\u 27 clc06\u 28 clc06\u 29 clc06\u 3 clc06\u 30
"312"    "313"    "321"    "322"    "323"    "324"    "121"    "331" 
clc06_31 clc06_32 clc06_33 clc06_34 clc06_35 clc06_36 clc06_37 clc06_38
"332"    "333"    "334"    "335"    "411"    "412"    "421"    "422" 
clc06_39 clc06_4 clc06_40 clc06_41 clc06_42 clc06_43 clc06_44 clc06_5
"423"    "122"    "511"    "512"    "521"    "522"    "523"    "123" 
clc06_6 clc06_7 clc06_8 clc06_9
"124"    "131"    "132"    "133" 
但是使用
ls()
选择变量不是一个好主意。你真的应该把你所有的
clc06\uu
值存储在一个向量中。

get()就是我要找的!然而,将名字列入名单似乎是最直接的方法+谢谢你们两位!get()就是我要找的!然而,将名字列入名单似乎是最直接的方法+谢谢你们两位!