Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have my input. I am using R. I want solution in R
id places present
1234 |new|world|life yes
5111
2012222 |lamp yes
123333 |world11|ness yes
I want output
id places present
1234 new y9970s
1234 world 7655s
1234 life 54644s
5111
2012222 lamp y777s
123333 world11 y998s
123333 ness y99s
I have tried
dt <- data.table(input)
dt=dt[ , list( V3 = unlist( strsplit(as.character( V3),"\\|") ) ) , by = V1]
but the 3rd column is left out. Even if I have multiple columns in that case how will I work
A possible solution with the data.table package:
library(data.table)
dt <- data.table(df)
new.dt <- dt[,strsplit(as.character(places),"|",fixed=TRUE), by=list(id,present)]
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I use the tableone package in r and it is extremely usefull.
I was wandering if someone was able to easily extract one column from it for convenient use.
Lets say just take column 1 and put it in a data frame that has
"Name","Value","Value in parenthesises" columns.
Thanks
(Kindly ignore the numbers here, they are just for demonstration purposes.)
Thanks :)
To do what you seem to want to do you might use str_extract from the stringr package. It works like this:
If this is the kind of data you have in your columns:
data <- c("1234 (567.8)", "4321 (12.34)", "5678 (91.234)")
then install the package and call it:
install.packages("stringr")
library(stringr)
and define regular expressions for what is to go into the columnValueand what is to go into column ValueInParentheses:
df <- data.frame(
Value = str_extract(data, "\\w.*(?=\\()"),
ValueInParentheses = str_extract(data, "(?<=\\()\\w.*(?=\\))")
); df
Value ValueInParenthesis
1 1234 567.8
2 4321 12.34
3 5678 91.234
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Using the first.df data frame, separate the DoB column data into 3 new columns - date, month,year by using the separate() function.I tried last line but it is not giving desired result.
fname <- c("Martina", "Monica", "Stan", "Oscar")
lname <- c("Welch", "Sobers", "Griffith", "Williams")
DoB <- c("1-Oct-1980", "2-Nov-1982", "13-Dec-1979", "27-Jan-1988")
first.df <- data.frame(fname,lname,DoB)
print(first.df)
separate(first.df,DoB,c('date','month','year'),sep = '-')
Moved my comment to an actual answer.
To retain the date column you need to add the remove = FALSE parameter, and to discard one of the separated columns simply add NA instead of a column name. The correct command is then
separate(first.df,DoB,c(NA,'month','year'),sep = '-', remove=FALSE)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a set of 20 column, each contains number value. I would like to have a function in excel or in r or somewhere else to extract the shared values among all the columns.
Several of the online Venn tools can visualize and list among up to 6 columns.
Any tool?
Thanks
in R, we can use intersect with Reduce to get the common values across all the columns
Reduce(intersect, dftest)
data
dftest <- data.frame(col1 = 1:5, col2 = 2:6, col3 = 3:7)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
The community reviewed whether to reopen this question 11 months ago and left it closed:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
Improve this question
How can I scale(x) only certain columns of a dataframe? I have a dataframe with 7 columns and I want to scale only column 3 and 6. The rest should stay as it is.
We can do this with lapply. Subset the columns of interest, loop through them with lapply, assign the output back to the subset of data. Here, we are using c because the outpuf of scale is a matrix with a single column. Using c or as.vector, it gets converted to vector
df[c(3,6)] <- lapply(df[c(3, 6), function(x) c(scale(x)))
Or another option is mutate_at from dplyr
library(dplyr)
df %>%
mutate_at(c(3,6), funs(c(scale(.))))
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
enter image description hereAnyone can help with the suggestion how to covert the following data formate to the one 'Arules' can use.
the dataset named 'df' is like
'Recieptnumber' , 'ProductName'
1 , egg
1 , apple
2 , water
3 , chips
3 , apple
3 , paper
.....
was using table(df) function to create matrix ---thoughts that is spares matrix . but it can not be used in 'Arules' package
any suggestion how to transfer the dataset?
Thank you!
Either use read.transactions() if you have it in a file or use the example from ? transactions:
a_df3 <- data.frame(
TID = c(1,1,2,2,2,3),
item=c("a","b","a","b","c", "b")
)
a_df3
trans4 <- as(split(a_df3[,"item"], a_df3[,"TID"]), "transactions")
trans4
inspect(trans4)