I want to create a list of vector of column which don't have special characters in columns in data frame.
so in my table I have n numbers of column and I want to have a index of columns doesn't have special characters.
so for example in below data frame the output should be c(3,5)
df <- data.frame(
V1 = c('liam', 'kevin', 'steve'),
v2 = c("--","--","--"),
v3 = c(10, 4, 7),
v4 = c("--","--","--"),
v5=c(22,63,0.1))
i want a output of vector of index of columns doesn't have "--" in columns .
in this dataframe case output should be c(3,5)
Yes right all columns with first columns also c(1,3,5)
You can do this:
which(x = vapply(X = df,
FUN = \(column) !any(grepl(pattern = "--",
x = column,
fixed = TRUE)),
FUN.VALUE = logical(length = 1L)))
Edit (for explanation request by @str_guru)
It operates per column.
grepl
returns a logical vector containing boolean values indicating whether an element contains "--" or not.
any
operates on that, and even if a single one is TRUE
, it returns TRUE
.
!
negates the output of any
, and returns a vector of same length as number of columns.
which
calculates the positions of the columns.
Hope it helps.
PS: The \(column)
notation to define function is pretty new in R. If you have older version, replace it with function(column)
Thanks for explaining , i just want to know how i can apply its giving error
df <- data.frame(
- V1 = c('liam', 'kevin', 'steve'),
- v2 = c("--","--","--"),
- v3 = c(10, 4, 7),
- v4 = c("--","--","--"),
- v5=c(22,63,0.1))
which(x = vapply(X = df,
Error: unexpected '!' in:
"which(x = vapply(X = df,
FUN = (column) !"
x = column,
Error: unexpected ',' in " x = column,"
fixed = TRUE)),
Error: unexpected ')' in " fixed = TRUE)"
FUN.VALUE = logical(length = 1L)))
Error: unexpected ')' in " FUN.VALUE = logical(length = 1L))"
ok, got it function(column) this is working
system
Closed
9
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.