我想在现有数据框中创建一个新列,该列基于同一数据框中另一列中的值并基于特定条件进行填充。
RAVE_ITN_BVAS_ADVIS3$subtype_ANCA_type_abr <-
apply(
RAVE_ITN_BVAS_ADVIS3$Subtype_ANCA_type,
1,
FUN = function(x) {
if (x == "Wegener's Granulomatosis (WG)-PR3") {
return("GPA_PR3")
}
if (x == "Wegener's Granulomatosis (WG)-MPO") {
return("GPA_MPO")
}
if (x == "Microscopic Polyangiitis (MPA)-PR3") {
return("MPA_PR3")
}
if (x == "Microscopic Polyangiitis (MPA)-MPO") {
return("MPA_MPO")
}
}
)
View(RAVE_ITN_BVAS_ADVIS3$Subtype_ANCA_type_abr)
我已经尝试了上面的代码(为格式不正确而道歉,stackoverflow似乎最近改变了一切)。
我不断收到错误消息:
Error in apply(RAVE_ITN_BVAS_ADVIS3$Subtype_ANCA_type, 1, FUN = function(x) { :
dim(X) must have a positive length
在此对您的任何帮助将不胜感激,非常感谢。
答案 0 :(得分:1)
这是一个奇怪的应用场所。 R还有很多其他功能可以提供帮助。我将查找有关此主题的一些教程。关于堆栈溢出有很多类似的问题。
您的apply
的问题是apply
取得一个数据帧,然后遍历每一行。您正在尝试使用向量作为apply函数的输入...并且向量没有行...然后函数中的每个x
都是一个“命名向量”。您可以使用x["Subtype_ANCA_type"]
从命名向量中调用适当的项。但是我不会这样解决问题。只是想解释如何使用Apply,因为这是您提出的问题。
#************************************************************#
# The original question
RAVE_ITN_BVAS_ADVIS3 <- data.frame(
Subtype_ANCA_type = rep(
c("Wegener's Granulomatosis (WG)-PR3",
"Wegener's Granulomatosis (WG)-MPO",
"Microscopic Polyangiitis (MPA)-PR3",
"Microscopic Polyangiitis (MPA)-MPO"),
2
),
stringsAsFactors = FALSE)
RAVE_ITN_BVAS_ADVIS3$subtype_ANCA_type_abr <-
apply(
RAVE_ITN_BVAS_ADVIS3,
1,
FUN = function(x) {
if (x["Subtype_ANCA_type"] == "Wegener's Granulomatosis (WG)-PR3") {
return("GPA_PR3")
}
if (x["Subtype_ANCA_type"] == "Wegener's Granulomatosis (WG)-MPO") {
return("GPA_MPO")
}
if (x["Subtype_ANCA_type"] == "Microscopic Polyangiitis (MPA)-PR3") {
return("MPA_PR3")
}
if (x["Subtype_ANCA_type"] == "Microscopic Polyangiitis (MPA)-MPO") {
return("MPA_MPO")
}
}
)
如果您想手动执行操作(如上述操作),则只需使用[]表示法即可确定将新列数据放置在何处。
#************************************************************#
# Manually add new column for alternative variables
RAVE_ITN_BVAS_ADVIS3 <- data.frame(
Subtype_ANCA_type = rep(
c("Wegener's Granulomatosis (WG)-PR3",
"Wegener's Granulomatosis (WG)-MPO",
"Microscopic Polyangiitis (MPA)-PR3",
"Microscopic Polyangiitis (MPA)-MPO"),
2
),
stringsAsFactors = FALSE)
# For the rows in the dataframe where Subtype_ANCA_type == "something", fill the next column.
RAVE_ITN_BVAS_ADVIS3[RAVE_ITN_BVAS_ADVIS3$Subtype_ANCA_type == "Wegener's Granulomatosis (WG)-PR3" ,"subtype_ANCA_type_abr"] <- "GPA_PR3"
RAVE_ITN_BVAS_ADVIS3[RAVE_ITN_BVAS_ADVIS3$Subtype_ANCA_type == "Wegener's Granulomatosis (WG)-MPO" ,"subtype_ANCA_type_abr"] <- "GPA_MPO"
RAVE_ITN_BVAS_ADVIS3[RAVE_ITN_BVAS_ADVIS3$Subtype_ANCA_type == "Microscopic Polyangiitis (MPA)-PR3","subtype_ANCA_type_abr"] <- "MPA_PR3"
RAVE_ITN_BVAS_ADVIS3[RAVE_ITN_BVAS_ADVIS3$Subtype_ANCA_type == "Microscopic Polyangiitis (MPA)-MPO","subtype_ANCA_type_abr"] <- "MPA_MPO"
RAVE_ITN_BVAS_ADVIS3
# Subtype_ANCA_type subtype_ANCA_type_abr
# 1 Wegener's Granulomatosis (WG)-PR3 GPA_PR3
# 2 Wegener's Granulomatosis (WG)-MPO GPA_MPO
# 3 Microscopic Polyangiitis (MPA)-PR3 MPA_PR3
# 4 Microscopic Polyangiitis (MPA)-MPO MPA_MPO
# 5 Wegener's Granulomatosis (WG)-PR3 GPA_PR3
# 6 Wegener's Granulomatosis (WG)-MPO GPA_MPO
# 7 Microscopic Polyangiitis (MPA)-PR3 MPA_PR3
# 8 Microscopic Polyangiitis (MPA)-MPO MPA_MPO
如果要使用很多这些,可能需要制作一个查找表。您甚至可以使用excel或其他来源在csv文件中创建查找表,并使用read.csv读取表。
#************************************************************#
# Add new column from a lookup table
abv_lookup <- data.frame(
Subtype_ANCA_type = c(
"Wegener's Granulomatosis (WG)-PR3",
"Wegener's Granulomatosis (WG)-MPO",
"Microscopic Polyangiitis (MPA)-PR3",
"Microscopic Polyangiitis (MPA)-MPO"
),
subtype_ANCA_type_abr = c(
"GPA_PR3",
"GPA_MPO",
"MPA_PR3",
"MPA_MPO"
),
stringsAsFactors = FALSE
)
RAVE_ITN_BVAS_ADVIS3 <- data.frame(
Subtype_ANCA_type = rep(
c("Wegener's Granulomatosis (WG)-PR3",
"Wegener's Granulomatosis (WG)-MPO",
"Microscopic Polyangiitis (MPA)-PR3",
"Microscopic Polyangiitis (MPA)-MPO"),
2
),
stringsAsFactors = FALSE)
# Merge the two dataframes together by any common columns (Subtype_ANCA_type)
RAVE_ITN_BVAS_ADVIS3 <- merge(RAVE_ITN_BVAS_ADVIS3,abv_lookup)
RAVE_ITN_BVAS_ADVIS3
# Subtype_ANCA_type subtype_ANCA_type_abr
# 1 Microscopic Polyangiitis (MPA)-MPO MPA_MPO
# 2 Microscopic Polyangiitis (MPA)-MPO MPA_MPO
# 3 Microscopic Polyangiitis (MPA)-PR3 MPA_PR3
# 4 Microscopic Polyangiitis (MPA)-PR3 MPA_PR3
# 5 Wegener's Granulomatosis (WG)-MPO GPA_MPO
# 6 Wegener's Granulomatosis (WG)-MPO GPA_MPO
# 7 Wegener's Granulomatosis (WG)-PR3 GPA_PR3
# 8 Wegener's Granulomatosis (WG)-PR3 GPA_PR3