Reputation: 325
I have a 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. I have tried the function below, which works, except that it does not refer to the columnname from the right column
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)
df_exp <- data.frame('criteria1' = c('criteria1= info','', 'criteria1=info', '', 'criteria1 =info'), "criteria2" = c('','', '', 'criteria2 = info', ''), "criteria3" = c('','', '', 'criteria3 = info', 'criteria3 = info'))
Upvotes: 2
Views: 106
Reputation: 73702
I'd go for the column numbers, either 1:ncol(df)
(which is faster) or 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
:
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
Upvotes: 2
Reputation: 13319
Using base
R(adjust spacing before =
as desired):
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::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))
Upvotes: 2