0

How to make values in columns 1,2,3,4 appear as values in a single column 1 in which values are placed one below the other? The contents are non numeric. I am unable to install tidy verse package for some reason. Any other way possible to accomplish? My dataframe df looks something like this

df
Person1 Person2 Person3 Doctor Self No Friend No Others
Self Others Doctor
I want the dataframe to be:
df1
Person
Doctor
Friend
Self
Self
No
Others
No
Others
Doc

Sowmya Kannan
  • 75
  • 1
  • 4
  • Please provide a minimal working example to illustrate what you want to do. Check [here](https://stackoverflow.com/help/minimal-reproducible-example) to see how to make a minimal reproducible example. Without that I can only guess what you mean and suggest to have a look at ?reshape – dario Feb 01 '20 at 12:28
  • Check out base R reshape functionality `help(reshape)`. You are trying to reshape from wide to long. – GWD Feb 01 '20 at 12:29
  • 1
    Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – Ronak Shah Feb 01 '20 at 12:38

2 Answers2

0

As a general rule, check this excellent answer on how to make a reproducible example in R. It'll help others to provide answers faster.

You can find a way to get your data in a long format using tidyr (assuming your columns are filled with stirngs as you mentioned).

> df <- data.frame(col1 = c("some", "strings"), col2 = c("more", "strings"), col3 = c("lotof", "strings"))
> df
     col1    col2    col3
1    some    more   lotof
2 strings strings strings

> library(tidyr)
> pivot_longer(df, c(col1, col2, col3))
# A tibble: 6 x 2
  name  value  
  <chr> <fct>  
1 col1  some   
2 col2  more   
3 col3  lotof  
4 col1  strings
5 col2  strings
6 col3  strings

Regarding package installation problems, could you copy the error that pops out and the console output of sessionInfo()

anddt
  • 1,589
  • 1
  • 9
  • 26
-1
data.table::melt()

or

reshape::melt()

Example

library(reshape)
mdata <- melt(mydata, id=c("id","te")) 

Result

d   t   v   value
1   1   x1  5
1   2   x1  3
2   1   x1  6
2   2   x1  2
jyr
  • 690
  • 6
  • 20