Extract one column from tableone [closed] - r

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

Related

Practice Exercise on tidyr Functions [closed]

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)

Scale only certain columns R [closed]

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(.))))

R how to cast string to a vector [closed]

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 6 years ago.
Improve this question
I am still a beginner in R and I can't find an answer to my question:
I use a string:
string1="c('T-shirt', 'Polo', 'Pull')"
And I need my object string1 to be a vector.
You can evaluate the expression in the string using
eval(parse(text=string1))
result:
[1] "T-shirt" "Polo" "Pull"
I am not sure what you want the final output to be. If you want string1 to be a vector of strings, the right syntax should be
string1 <- c("T-shirt", "Polo", "Pull")
Please clarify if you want a different output
You can do by both ways
eval(parse(text=string1))
or
c <- gsub("\\(|\\)|c|'", "", string1)
d <- strsplit(c,",")
e <- d[[1]]
e
This could be done with str_extract without using the eval(parse.
library(stringr)
str_extract_all(string1, "(?<=')[[:alpha:]-]+")[[1]]
#[1] "T-shirt" "Polo" "Pull"
data
string1="c('T-shirt', 'Polo', 'Pull')"

How to pick specific subjects in R [closed]

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
tried to pick up 2 random subjects, but don't know how to do in R
random.subj <- sample(1:max(Data$Id), 2)
rd <- subset(Data$Id, Data$Id==random.subj)
I have a dataset "Data" like
Id
1
1
2
2
3
3
4
4
4
...
Well, in this case random.subj will be a vector of two elements. In that case, doing an equality comparison with == probably isn't want you want because it will just recycle through the shorter list to perform the comparison rather than checking each row for either value as you probably intend.
Also i'm not sure if all your IDs are numerical and sequential. It's better to just take a random sample from the IDs themselves rather than from the index of the IDs.
Fixing the second problem first
random.subj <- sample(Data$Id, 2)
Actually, if you just want two IDs then that's all you need, but if you want the data for those IDs then
rd <- subset(Data, Data$Id %in% random.subj)
is the correct way to extract it.

R Splitting the column into 2 with out changing other columns [closed]

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)]

Resources