How to create a column containing a string of stars to indicate levels of a factor in a data frame in R
(second question today - must be a bad day)
I have a dataframe with various columns, including a concentration column (numeric), a flag highlighting invalid results (boolean) and a description of the problem (character)
df <- structure(list(x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), rawconc = c(77.4,
52.6, 86.5, 44.5, 167, 16.2, 59.3, 123, 1.95, 181), reason = structure(c(NA,
NA, 2L, NA, NA, NA, 2L, 1L, NA, NA), .Label = c("Fails Acceptance Criteria",
"Poor Injection"), class = "factor"), flag = c("False", "False",
"True", "False", "False", "False", "True", "True", "False", "False"
)), .Names = c("x", "rawconc", "reason", "flag"), row.names = c(NA,
-10L), class = "data.frame")
I can create a column with the numeric level of the reason column
df$level<-as.numeric(df$reason)
df
x rawconc reason flag level
1 1 77.40 <NA> False NA
2 2 52.60 <NA> False NA
3 3 86.50 Poor Injection True 2
4 4 44.50 <NA> False NA
5 5 167.00 <NA> False NA
6 6 16.20 <NA> False NA
7 7 59.30 Poor Injection True 2
8 8 123.00 Fails Acceptance Criteria True 1
9 9 1.95 <NA> False NA
10 10 181.00 <NA> False NA
and here's what I want to do to create a column with 'level' many stars, but it fails
df$stars<-paste(rep("*",df$level)sep="",collap开发者_如何学运维se="")
Error: unexpected symbol in "df$stars<-paste(rep("*",df$level)sep"
df$stars<-paste(rep("*",df$level),sep="",collapse="")
Error in rep("*", df$level) : invalid 'times' argument
rep("*",df$level)
Error in rep("*", df$level) : invalid 'times' argument
df$stars<-paste(rep("*",pmax(df$level,0,na.rm=TRUE)),sep="",collapse="")
Error in rep("*", pmax(df$level, 0, na.rm = TRUE)) :
invalid 'times' argument
It seems that rep needs to be fed one value at a time. I feel that this should be possible (and my gut says 'use lapply' but my apply fu is v. poor)
Any one want to try ?
You could create stars vector as
vstars <- sapply(1L:nlevels(df$reason), function(i) paste(rep("*",i),collapse=""))
vstars
# [1] "*" "**"
And then indexing it with df$reason
(which works because its a factor):
vstars[df$reason]
# [1] NA NA "**" NA NA NA "**" "*" NA NA
For large data.frame
should be much faster then paste
in each row.
I think that you will need an apply-type function. This will work:
df[is.na(df$level),"level"] <- 0
df$level <- sapply(df$level, function(x) paste(rep("*",x),collapse=""))
You would be better using sapply
than lapply
in this instance since it returns a vector instead of a list.
From the help for rep:
If 'times' consists of a single integer, the result consists of the whole input repeated this many times. If 'times' is a vector of the same length as 'x' (after replication by 'each'), the result consists of 'x[1]' repeated 'times[1]' times, 'x[2]' repeated 'times[2]' times and so on.
One problem with using rep
with a vector for the times parameter is that it just returns a vector and it discards instances when times=0. You can see this with this command: rep(rep("*", nrow(df)), times=df$level)
.
精彩评论