英文:
Create a new column based on matching string
问题
我有一个大型数据框,想要根据特定列中的匹配数据创建一个名为Class的新列:是否可以使用循环或其他方法来解决这个问题?
示例数据框如下:
dat <- data.frame(
Function = c("A", "B", "C", "D", "E", "F", "G", "H", "I")
)
输出如下:
dat <- data.frame(
Function = c("A", "C", "F", "D", "E", "I", "G", "H", "B"),
Class = c("Class1","Class1","Class1","Class2","Class2","Class2","Class3","Class3","Class3")
)
英文:
I have a large dataframe and want to create a new column name Class based on matching data present in perticular column:
Is it possible to solve this using loop or other way
The example dataframe is as follows:
dat <- data.frame(
Function = c("A", "B", "C", "D", "E", "F", "G", "H", "I")
)
and the output look like this
dat <- data.frame(
Function = c("A", "C", "F", "D", "E", "I", "G", "H", "B"),
Class= c("Class1","Class1","Class1","Class2","Class2","Class2","Class3","Class3","Class3"))
答案1
得分: 0
# 创建一个名为 `A` 的赋值字典,例如使用 `read.table`,并使用 `merge` 合并数据框。
A <- read.table(text='
A Class1
B Class3
C Class1
D Class2
E Class2
F Class1
G Class3
H Class3
I Class2
')
merge(dat, A)
# Function Class
# 1 A Class1
# 2 B Class3
# 3 C Class1
# 4 D Class2
# 5 E Class2
# 6 F Class1
# 7 G Class3
# 8 H Class3
# 9 I Class2
# 或者可以编写一个包含类和分配函数的 `list`,
lst <- list(Class1=c("A", "C", "F"), Class2=c("D", "E", "I"), Class3=c("B", "G", "H"))
# 并使用 `Vectorize` 函数,以便循环遍历函数以及列表元素。
class_assign <- Vectorize(\(x, lst) names(lst)[sapply(lst, \(a) any(x %in% a))], vectorize.args='x')
dat$Class <- class_assign(dat$Function, lst)
# Function Class
# 1 A Class1
# 2 C Class1
# 3 F Class1
# 4 D Class2
# 5 E Class2
# 6 I Class2
# 7 G Class3
# 8 H Class3
# 9 B Class3
数据:
dat <- structure(list(Function = c("A", "C", "F", "D", "E", "I", "G", "H", "B")), class = "data.frame", row.names = c(NA, -9L))
英文:
Create an assignment dictionary A
, using read.table
for instance, and merge
the data frames.
A <- read.table(text='
A Class1
B Class3
C Class1
D Class2
E Class2
F Class1
G Class3
H Class3
I Class2
')
merge(dat, A)
# Function Class
# 1 A Class1
# 2 B Class3
# 3 C Class1
# 4 D Class2
# 5 E Class2
# 6 F Class1
# 7 G Class3
# 8 H Class3
# 9 I Class2
The other way round you could write a list
with classes and the assigned functions,
lst <- list(Class1=c("A", "C", "F"), Class2=c("D", "E", "I"), Class3=c("B", "G", "H"))
and Vectorize
a small function so it loops over the functions as well as list elements.
class_assign <- Vectorize(\(x, lst) names(lst)[sapply(lst, \(a) any(x %in% a))], vectorize.args='x')
dat$Class <- class_assign(dat$Function, lst)
# Function Class
# 1 A Class1
# 2 C Class1
# 3 F Class1
# 4 D Class2
# 5 E Class2
# 6 I Class2
# 7 G Class3
# 8 H Class3
# 9 B Class3
Data:
dat <- structure(list(Function = c("A", "C", "F", "D", "E", "I", "G",
"H", "B")), class = "data.frame", row.names = c(NA, -9L))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论