[英]How to refer to a column name in a function
I have a df.我有一个df。 If 'info' appears in a row, I would like a function to take the column-name and and paste it together with the information from the cell with a '=' between them.
如果“信息”连续出现,我希望 function 获取列名并将其与单元格中的信息一起粘贴,并在它们之间使用“=”。 I have tried the function below, which works, except that it does not refer to the columnname from the right column
我已经尝试过下面的 function,它有效,只是它没有引用右列中的列名
df <- data.frame('criteria1' = c('info','1', 'info', '', 'info'), "criteria2" = c('y','3', '', 'info', ''), "criteria3" = c('y','7', '', 'info', 'info')) df[] <- sapply(df, function(x) ifelse(x == 'info', paste(colnames(df)[x], ' = ', x),''))
My expected output is something like this (it does not matter if the columnnames are deleted, it is just the info from the cells that are important)我预期的 output 是这样的(列名是否被删除并不重要,重要的是来自单元格的信息)
df_exp <- data.frame('criteria1' = c('criteria1= info','', 'criteria1=info', '', 'criteria1 =info'), "criteria2" = c('','', '', 'criteria2 = info', ''), "criteria3" = c('','', '', 'criteria3 = info', 'criteria3 = info'))
I'd go for the column numbers, either 1:ncol(df)
(which is faster) or seq(df)
.我将 go 作为列号,或者
1:ncol(df)
(更快)或seq(df)
。 I use the former.我使用前者。
df <- sapply(1:ncol(df), function(x)
ifelse(df[[x]] == 'info', paste(colnames(df)[x], ' = ', df[[x]]),''))
df
# [,1] [,2] [,3]
# [1,] "criteria1 = info" "" ""
# [2,] "" "" ""
# [3,] "criteria1 = info" "" ""
# [4,] "" "criteria2 = info" "criteria3 = info"
# [5,] "criteria1 = info" "" "criteria3 = info"
Another nice way using stack/unstack
:另一个使用
stack/unstack
的好方法:
r <- grep("info", tmp$values)
tmp <- stack(df)
tmp[r, 1] <- apply(tmp[r, 2:1], 1, paste, collapse="=")
tmp[-r, 1] <- "" ## in case you want non-"info" cells cleared
df <- unstack(tmp)
df
# criteria1 criteria2 criteria3
# 1 criteria1=info
# 2
# 3 criteria1=info
# 4 criteria2=info criteria3=info
# 5 criteria1=info criteria3=info
Using base
R(adjust spacing before =
as desired):使用
base
R(根据需要调整=
之前的间距):
use_names <- names(df)
data.frame(Map(function(x,y) ifelse(x=="info", paste0(y,"=",x),""), df, use_names))
criteria1 criteria2 criteria3
1 criteria1=info
2
3 criteria1=info
4 criteria2=info criteria3=info
5 criteria1=info criteria3=info
purrr
: purrr
:
purrr::map2_df(df, names(df), function(x,y) ifelse(x=="info", paste0(y,"=",x),""))
# A tibble: 5 x 3
criteria1 criteria2 criteria3
<chr> <chr> <chr>
1 "criteria1=info" "" ""
2 "" "" ""
3 "criteria1=info" "" ""
4 "" "criteria2=info" "criteria3=info"
5 "criteria1=info" "" "criteria3=info"
Data:数据:
df <- structure(list(criteria1 = c("info", "1", "info", "", "info"),
criteria2 = c("y", "3", "", "info", ""), criteria3 = c("y",
"7", "", "info", "info")), class = "data.frame", row.names = c(NA,
-5L))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.