0
這可能相對簡單。我有一個看起來像這樣一個龐大的數據幀:將列表中的值替換爲數字,然後執行算術計算
df1 <- structure(list(place = structure(c(1L, 5L, 1L, 4L), .Label = c("1","2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23","24", "25", "26"), class = "factor"), x = structure(list(c("A", "B", "C", "D", "E"), c("D", "E", "F","G", "H", "I"), c("D", "E", "F", "G", "H"), c("F", "H")), class = "AsIs")), .Names = c("place", "x"), row.names = c(1L, 2L, 3L, 4L), class = "data.frame")
> df1
place x
1 1 A, B, C,....
2 5 D, E, F,....
3 1 D, E, F,....
4 4 F, H
,另一種是對每個列表元素在df1
相應的值:
df2 <- structure(list(x = c('A','B','C','D','E','F','G','H','I','J','K','L','M'), value = c("5.2", "1.8", "2.7","3.8", "5.0","3.2", "4.5","2.4", "3.9", "1.2","2.3","4.3", "3.0")), .Names = c("x", "value"), row.names = c(1L,2L,3L,4L,5L,6L,7L,8L,9L,10L, 11L, 12L, 13L), class = "data.frame")
x value
1 A 5.2
2 B 1.8
3 C 2.7
4 D 3.8
5 E 5.0
6 F 3.2
7 G 4.5
8 H 2.4
9 I 3.9
10 J 1.2
11 K 2.3
12 L 4.3
13 M 3.0
我要替換的元素df1
其對應的值爲df2
(因此A
中的每個df1
應該是5.2
等等),然後執行操作,例如使用這些值的每個地點的平均值x
。謝謝!一步到位
sapply(df1$x, mean)
或者:
很大,然後進行操作,例如作爲每行的平均值? – Lucarno
@LucasMN看到我的編輯。 – Justin
謝謝,賈斯汀! – Lucarno