Reputation: 5522
I have a dataframe 1:
df1<- structure(list(`gaugelist[1:20, ]` = c(1094000L, 1094000L, 1094000L,
1100600L, 1100600L, 1100600L, 1100600L, 1100600L, 1100600L, 1100600L,
1100600L, 1100600L, 1100600L, 1100600L, 1096000L, 1100600L, 1100600L,
1100600L, 1100600L, 1100600L)), .Names = "gaugelist[1:20, ]", row.names = c(NA,
-20L), class = "data.frame")
And a second list:
df2 <- c("ts.01094000.crest.csv", "ts.01100600.crest.csv")
I want to make another column in df1 based on matching row values:
df1$test <- apply(df1, 1, function(x) df2[which(grepl(x,df2))])
Upvotes: 0
Views: 1303
Reputation: 13118
This worked for me:
df1 <- setNames(df1, "x") # rename column to `x` for brevity
df1 %>% rowwise() %>%
mutate(test = df2[grep(x,df2)[1]])
# Source: local data frame [20 x 2]
# Groups: <by row>
# # A tibble: 20 × 2
# x test
# <int> <chr>
# 1 1094000 ts.01094000.crest.csv
# 2 1094000 ts.01094000.crest.csv
# 3 1094000 ts.01094000.crest.csv
Explanation. You did not explain why you "couldn't make rowwise work", but based on my own experience, you may have gotten this message
Error: incompatible size (0), expecting 1 (the group size) or 1
if you use an expression like df2[which(grepl(x,df2))]
in mutate
, which occurs because one (or more) of the returned values has length 0 (i.e. no match was found in df2
). Using mutate(test = df2[grep(x,df2)[1]])
addresses this problem. In particular, note that df2[grep(x,df2)[1]]
returns one and only one value.
Upvotes: 2